偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.
全部博文(1747)
分类: LINUX
2009-02-06 17:27:29
#include #include #include #include #include #include MODULE_LICENSE("GPL"); #define MAJOR_NUM 254 static ssize_t globalvar_read(struct file *, char *, size_t, loff_t*); static ssize_t globalvar_write(struct file *, const char *, size_t, loff_t*); struct file_operations globalvar_fops = { read: globalvar_read, write: globalvar_write, }; static int global_var = 0; static struct semaphore sem; static wait_queue_head_t outq; static int flag = 0; static int __init globalvar_init(void) { int ret; ret = register_chrdev(MAJOR_NUM, "globalvar", &globalvar_fops); if (ret) { printk("globalvar register failure"); } else { printk("globalvar register success"); init_MUTEX(&sem); init_waitqueue_head(&outq); } return ret; } static void __exit globalvar_exit(void) { int ret; ret = unregister_chrdev(MAJOR_NUM, "globalvar"); if (ret) { printk("globalvar unregister failure"); } else { printk("globalvar unregister success"); } } static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off) { //等待数据可获得 if (wait_event_interruptible(outq, flag != 0)) { return - ERESTARTSYS; } if (down_interruptible(&sem)) { return - ERESTARTSYS; } flag = 0; if (copy_to_user(buf, &global_var, sizeof(int))) { up(&sem); return - EFAULT; } up(&sem); return sizeof(int); } static ssize_t globalvar_write(struct file *filp, const char *buf, size_t len,loff_t *off) { if (down_interruptible(&sem)) { return - ERESTARTSYS; } if (copy_from_user(&global_var, buf, sizeof(int))) { up(&sem); return - EFAULT; } up(&sem); flag = 1; //通知数据可获得 wake_up_interruptible(&outq); return sizeof(int); } module_init(globalvar_init); module_exit(globalvar_exit); |
#include #include #include #include main() { int fd, num; fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR); if (fd != - 1) { while (1) { read(fd, &num, sizeof(int)); //程序将阻塞在此语句,除非有针对globalvar的输入 printf("The globalvar is %d\n", num); //如果输入是0,则退出 if (num == 0) { close(fd); break; } } } else { printf("device open failure\n"); } } |
#include #include #include #include main() { int fd, num; fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR); if (fd != - 1) { while (1) { printf("Please input the globalvar:\n"); scanf("%d", &num); write(fd, &num, sizeof(int)); //如果输入0,退出 if (num == 0) { close(fd); break; } } } else { printf("device open failure\n"); } } |
static ssize_t globalvar_read(struct file *filp, char *buf, size_t len, loff_t *off) { //获取信号量:可能阻塞 if (down_interruptible(&sem)) { return - ERESTARTSYS; } //等待数据可获得:可能阻塞 if (wait_event_interruptible(outq, flag != 0)) { return - ERESTARTSYS; } flag = 0; //临界资源访问 if (copy_to_user(buf, &global_var, sizeof(int))) { up(&sem); return - EFAULT; } //释放信号量 up(&sem); return sizeof(int); } |
即交换wait_event_interruptible(outq, flag !=
0)和down_interruptible(&sem)的顺序,这个驱动程序将变得不可运行。实际上,当两个可能要阻塞的事件同时出现时,即两
个wait_event或down摆在一起的时候,将变得非常危险,死锁的可能性很大,这个时候我们要特别留意它们的出现顺序。当然,我们应该尽可能地避
免这种情况的发生!
+还有一个与设备阻塞与非阻塞访问息息相关的论题,即select和poll,select和poll的本质一样,前者在BSD
Unix中引入,后者在System
V中引入。poll和select用于查询设备的状态,以便用户程序获知是否能对设备进行非阻塞的访问,它们都需要设备驱动程序中的poll函数支持。
驱动程序中poll函数中最主要用到的一个API是poll_wait,其原型如下:
void poll_wait(struct file *filp, wait_queue_heat_t *queue, poll_table * wait); |
static unsigned int globalvar_poll(struct file *filp, poll_table *wait) { unsigned int mask = 0; poll_wait(filp, &outq, wait); //数据是否可获得? if (flag != 0) { mask |= POLLIN | POLLRDNORM; //标示数据可获得 } return mask; } |
int select(int numfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); |
struct timeval { int tv_sec; /* seconds */ int tv_usec; /* microseconds */ }; |
#include #include #include #include #include #include #include main() { int fd, num; fd_set rfds; struct timeval tv; fd = open("/dev/globalvar", O_RDWR, S_IRUSR | S_IWUSR); if (fd != - 1) { while (1) { //查看globalvar是否有输入 FD_ZERO(&rfds); FD_SET(fd, &rfds); //设置超时时间为5s tv.tv_sec = 5; tv.tv_usec = 0; select(fd + 1, &rfds, NULL, NULL, &tv); //数据是否可获得? if (FD_ISSET(fd, &rfds)) { read(fd, &num, sizeof(int)); printf("The globalvar is %d\n", num); //输入为0,退出 if (num == 0) { close(fd); break; } } else printf("No data within 5 seconds.\n"); } } else { printf("device open failure\n"); } } |