• 字符设备之poll机制


    poll机制作用:相当于一个定时器。时间到了还没有资源就唤醒进程。


    主要用途就是:进程设置一段时间用来等待资源,假设时间到了资源还没有到来,进程就立马从睡眠状态唤醒不再等待。当然这仅仅是使用于这段时间以后资源对于该进程已经没用的情况。

    内核中poll机制的实现过程:
    sys_poll函数在include/linux/syscalls.h中声明

    //函数定义前加宏asmlinkage ,表示这些函数通过堆栈而不是通过寄存器传递參数。 
    asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout);
    在系统调用表archarmkernelcalls.S中调用

    CALL(sys_poll) //系统调用跳转表的一项

    关于系统调用表的初始化在arch/arm/kernel/entry-common.S中

    .equ NR_syscalls,0  //将NR_syscalls初始化为0
    #define CALL(x) .equ NR_syscalls,NR_syscalls+1  //将CALL(x) 定义为: NR_syscalls = NR_syscalls  + 1
    #include "calls.S"//将calls.S的内容包进来,CALL(x)上面已经有了定义,这里就相当于运行了多次NR_syscalls++,最后就统计了系统调用的个数。并对NR_syscalls进行了4的倍数对齐。这一招,特么好厉害!

    #undef CALL //撤销CALL宏定义 #define CALL(x) .long x //对CALL又一次进行宏定义。也是4字节对齐 arch/arm/kernel/entry-common.S中: sys_syscall: bic scno, r0, #__NR_OABI_SYSCALL_BASE cmp scno, #__NR_syscall - __NR_SYSCALL_BASE cmpne scno, #NR_syscalls @ check range stmloia sp, {r5, r6} @ shuffle args movlo r0, r1 movlo r1, r2 movlo r2, r3 movlo r3, r4 ldrlo pc, [tbl, scno, lsl #2] b sys_ni_syscall

    终于sys_poll()函数,就相当于以下的函数:在fs/select.c文件里,SYSCALL_DEFINE3是有3个參数的系统调用的宏定义
    SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,long, timeout_msecs)
    {
    	......
    	
    	ret = do_sys_poll(ufds, nfds, to);//调用
    	
    	......
    }
    好,看看应用层调用poll函数时的底层驱动运行线路
    【app:】
    poll();
    【kernel: 】
    sys_poll
    	do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,struct timespec *end_time)
    		poll_initwait(&table);
    			init_poll_funcptr(&table->pt, __pollwait);-->pt->qproc = __pollwait; //初始化qproc函数指针,让他指向__pollwait函数
    		do_poll(nfds, head, &table, end_time);
    			for(;;)
    			{
    				for (; pfd != pfd_end; pfd++) //查询多个驱动程序
    				{
    					if (do_pollfd(pfd, pt))  -> mask = file->f_op->poll(file, pwait);return mask;
    					{ //do_pollfd函数相当于调用驱动里面的xxx_poll函数,以下另外再进行分析,返回值mask非零。count++,记录等待事件发生的进程数
    						count++;
    						pt = NULL;
    					}
    				}
    				
    				if (count || timed_out) //若count不为0(有等待的事件发生了)或者timed_out不为0(有信号发生或超时),则推出休眠
    					break;
    				
    				//上述条件不满足以下開始进入休眠,若有等待的事件发生了,超时或收到信号则唤醒
    				poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)
    			}
    驱动里边的xxx_poll()函数分析
    xxx_poll(struct file *file, poll_table *wait)
    	poll_wait(file, &xxxx_waitq, wait);
    //////////////////////////////////////////////////////////////////
    static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
    {
    	if (p && wait_address)
    		p->qproc(filp, wait_address, p); //调用之前poll_initwait()函数设置的函数qproc即__pollwait,__pollwait函数仅仅是把当前进程挂到等待队列,仅仅是add_wait_queue(wait_address, &entry->wait);不进入休眠
    }

    測试驱动程序:poll_dev.c

    #include <linux/delay.h>
    #include <linux/irq.h>
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <linux/module.h>
    #include <linux/device.h> 		//class_create
    #include <mach/regs-gpio.h>		//S3C2440_GPF1 
    #include <mach/hardware.h>
    #include <linux/interrupt.h>	//wait_event_interruptible
    #include <linux/fs.h>
    #include <linux/poll.h>			//poll
    
    /* 定义并初始化等待队列头 */
    static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
    
    static struct class *buttondev_class;
    static struct device *buttons_device;
    
    static struct pin_desc{
    	unsigned int pin;
    	unsigned int key_val;
    };
    
    static struct pin_desc pins_desc[4] = {
    		{S3C2410_GPF1,0x01}, //S3C2410_GPF1是对GPF1引脚这样的“设备”的编号dev_id
    		{S3C2410_GPF4,0x02},
    		{S3C2410_GPF2,0x03},
    		{S3C2410_GPF0,0x04},
    }; 
    static int ev_press = 0;
    
    static unsigned char key_val;
    int major;
    
    /* 中断处理函数 */
    static irqreturn_t handle_irq(int irq, void *dev_id)
    {
    	struct pin_desc *irq_pindesc = (struct pin_desc *)dev_id;//
    	unsigned int pinval;
    	
    	pinval = s3c2410_gpio_getpin(irq_pindesc->pin);//获取按键值:有按键按下返回按键值0
    	/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
    	/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
    	if(pinval)
    	{
    		/* 松开 */
    		key_val = 0x80 | (irq_pindesc->key_val);
    	}
    	else
    	{
    		/* 按下 */
    		key_val = irq_pindesc->key_val;
    	}
    
    	ev_press = 1;							/* 表示中断已经发生 */
    	wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    	return IRQ_HANDLED;
    }
    
    static int buttons_dev_open(struct inode * inode, struct file * filp)
    {
    	/*  K1-EINT1,K2-EINT4,K3-EINT2,K4-EINT0
      	 *  配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚
      	 *  IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH
    	 */
    	request_irq(IRQ_EINT1, handle_irq, IRQ_TYPE_EDGE_FALLING, "K1",&pins_desc[0]);
    	request_irq(IRQ_EINT4, handle_irq, IRQ_TYPE_EDGE_FALLING, "K2",&pins_desc[1]);
    	request_irq(IRQ_EINT2, handle_irq, IRQ_TYPE_EDGE_FALLING, "K3",&pins_desc[2]);
    	request_irq(IRQ_EINT0, handle_irq, IRQ_TYPE_EDGE_FALLING, "K4",&pins_desc[3]);
    	return 0;
    }
    
    static int buttons_dev_close(struct inode *inode, struct file *file)
    {
    	free_irq(IRQ_EINT1,&pins_desc[0]);
    	free_irq(IRQ_EINT4,&pins_desc[1]);
    	free_irq(IRQ_EINT2,&pins_desc[2]);
    	free_irq(IRQ_EINT0,&pins_desc[3]);
    	return 0;
    }
    
    static ssize_t buttons_dev_read(struct file *file, char __user *user, size_t size,loff_t *ppos)
    {
    	if (size != 1)
    			return -EINVAL;
    	//wait_event_interruptible(button_waitq, ev_press);//使用poll机制。这里就不须要再推断要不要进入睡眠了。

    copy_to_user(user, &key_val, 1); /* 将ev_press清零 */ ev_press = 0; return 1; } //////////////////////////关键点/////////////////////////////////////// static unsigned int buttons_dev_poll(struct file *file, poll_table *wait) //该函数一旦被调用就触发poll机制 { unsigned int mask = 0; /* 该函数,仅仅是将进程挂在button_waitq队列上。而不是马上休眠 */ poll_wait(file, &button_waitq, wait); /*** * 如果进程还poll在上面这一函数里边。尚未超时,如果此时有中断到来,中断处理程序将ev_press置位,然后唤醒休眠队列上相应的进程 ***/ /* 进程唤醒之后,立刻往下运行。唤醒的可能原因:超时/中断处理 */ if(ev_press) { mask |= POLLIN | POLLRDNORM; /* 有数据可读 */ } /* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */ return mask; } /////////////////////////////////////////////////////////////////////////////// /* File operations struct for character device */ static const struct file_operations buttons_dev_fops = { .owner = THIS_MODULE, .open = buttons_dev_open, .read = buttons_dev_read, .release = buttons_dev_close, .poll = buttons_dev_poll, }; /* 驱动入口函数 */ static int buttons_dev_init(void) { /* 主设备号设置为0表示由系统自己主动分配主设备号 */ major = register_chrdev(0, "buttons_dev", &buttons_dev_fops); /* 创建buttondev类 */ buttondev_class = class_create(THIS_MODULE, "buttondev"); /* 在buttondev类下创建buttons设备,供应用程序打开设备*/ buttons_device = device_create(buttondev_class, NULL, MKDEV(major, 0), NULL, "buttons");// return 0; } /* 驱动出口函数 */ static void buttons_dev_exit(void) { unregister_chrdev(major, "buttons_dev"); device_unregister(buttons_device); //卸载类下的设备 class_destroy(buttondev_class); //卸载类 } /* 模块载入和卸载函数的修饰 */ module_init(buttons_dev_init); module_exit(buttons_dev_exit); MODULE_AUTHOR("CLBIAO"); MODULE_DESCRIPTION("Just for Demon"); MODULE_LICENSE("GPL"); //遵循GPL协议


    測试应用程序:app_poll.c

    /* 文件的编译指令是arm-linux-gcc -static -o app_poll app_poll.c */
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <poll.h>
    
    /* fourth_test
     */ 
    int main(int argc ,char *argv[])
    
    {
    	int fd;
    	unsigned char key_val;
    	struct pollfd fds;
    	int ret;
    
    	fd = open("/dev/buttons",O_RDWR);
    	if (fd < 0)
    	{
    		printf("open error
    ");
    	}
    	fds.fd = fd;//查询的文件
    	fds.events = POLLIN; //期待收到poll_in值。表示有数据
    	while(1)
    	{
    		/* A value of 0 indicates  that the call timed out and no file descriptors were ready
    		 * poll函数返回0时。表示5s时间到了,而这段时间里。没有事件发生"数据可读"
    		 */
    		ret = poll(&fds,1,5000);
    		if(ret == 0)
    		{
    			printf("time out
    ");
    		}
    
    		else	/* 假设没有超时,则读出按键值 */
    		{
    			read(fd,&key_val,1);
    			printf("key_val = 0x%x
    ",key_val);
    		} 	
    	}
    	return 0;
    }
    
    
    測试结果:



    小结:poll流程图


















  • 相关阅读:
    MybatisPlus常用操作和配置
    C# Socket编程 同步以及异步通信
    PostgreSQL学习手册(PL/pgSQL过程语言)
    PostgreSQL学习手册(客户端命令<一>)
    PostgreSQL学习手册(服务器配置)
    PostgreSQL学习手册(系统视图)
    PostgreSQL学习手册(客户端命令<二>)
    PostgreSQL学习手册(角色和权限)
    PostgreSQL学习手册(数据库管理)
    PostgreSQL学习手册(系统表)
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6806439.html
Copyright © 2020-2023  润新知