按键驱动程序(同步互斥阻塞)

27 篇文章 0 订阅
5 篇文章 0 订阅
    同样的,在 按键驱动程序(中断方式) 的基础之上添加互斥阻塞机制。当设备被一个程序打开时,存在被另一个程序打开的可能, 如果两个或多个程序同时对设备文件进行写操作,或者读设备文件都会出现同步的问题 。本程序是通过 获取信号量“锁住”打开程序,直到释放了信号量,另外的程序才能打开程序 。(另外的方法还有用 原子操作 维护设备被打开的计数等)。获得信号量有可以用int  down_trylock (struct semaphore *sem)尝试获得信号量,立即返回。void  down (struct semaphore *sem)获得信号量, 获得不到就休眠等待直到可以获得信号量 。可以在驱动程序的 open函数中调用获得信号量 根据文件打开标志O_NONBLOCK是否存在来决定是否是非阻塞或阻塞方式打开 ,如果是 非阻塞 则应用 down_trylock() 获得信号量, 一旦获取不到立马返回错误值 否则应用down() 或其它如int  down_interruptible (struct semaphore *sem)等。 在设备文件被关闭时 即在close函数中应该释放掉信号量,以便其它程序使用 ,用到的函数是 void up (struct semaphore *sem)。
 
详细驱动程序如下:

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/cdev.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/device.h>
  11. #include <linux/sched.h>
  12. #include <linux/gpio.h>

  13. #define SIXDEV MKDEV(250, 0)
  14. struct cdev sixdrv_cdev;

  15. static struct class *sixdrv_class;

  16. struct button {
  17.     int irq;
  18.     char *name;
  19.     int pin;
  20.     int val;
  21. };

  22. static volatile int pressed = 0;
  23. static unsigned char key_val;

  24. struct fasync_struct *button_async;

  25. DECLARE_WAIT_QUEUE_HEAD(button_wqh);

  26. /* 六个按键的相关定义整合到结构体 */
  27. static struct button buttons[6] = {
  28.         {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1},
  29.         {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2},
  30.         {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3},
  31.         {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4},
  32.         {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5},
  33.         {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},
  34. };


  35. static DECLARE_MUTEX(button_lock);

  36. /* 中断处理函数 */
  37. static irqreturn_t sixdrv_intr(int irq, void *data)
  38. {
  39.     struct button *buttonp;
  40.     int val;
  41.     
  42.     buttonp = (struct button*)data;

  43.     val = s3c2410_gpio_getpin(buttonp->pin);

  44.     
  45.     if (!val) {/* 按下按键*/
  46.         key_val = buttonp->val;    
  47.     } else { /* 释放按键*/
  48.         key_val = buttonp->val | 0x10; //将第4位置1,做标记
  49.     }

  50.     pressed = 1; //此处改变按下标志,以使队列不继续睡眠
  51.     wake_up_interruptible(&button_wqh);
  52.     
  53.     return IRQ_RETVAL(IRQ_HANDLED);
  54. }

  55. static int sixdrv_open(struct inode * inode, struct file * file)
  56. {
  57.     int i=6;
  58.      if (file->f_flags & O_NONBLOCK) {
  59.         if(down_trylock(&button_lock))
  60.             return -EBUSY;
  61.         } else {
  62.            down(&button_lock);
  63.         }
  64.     while(i--){
  65.         request_irq(buttons[i].irq, &sixdrv_intr, IRQ_TYPE_EDGE_BOTH,
  66.                     buttons[i].name, &buttons[i]);
  67.     }
  68.     return 0;
  69.     
  70. }

  71. static ssize_t sixdrv_read(struct file *file, char __user *user, size_t size,loff_t*o)
  72. {
  73.     int sz = sizeof(key_val) ;
  74.     
  75.     if (sz != size) {
  76.         return -EINVAL;
  77.     }
  78.     
  79.     if (file->f_flags & O_NONBLOCK)
  80.     {
  81.         if (!pressed)
  82.             return -EAGAIN;
  83.     }
  84.     else
  85.     {
  86.         /* 如果没有按键动作, 休眠 */
  87.         wait_event_interruptible(button_wqh, pressed);
  88.     }
  89.     copy_to_user(user, &key_val, sz);

  90.     /* 重新清除按下标志 */
  91.     pressed = 0;

  92.     return sz;
  93. }

  94. static int sixdrv_close(struct inode *inode, struct file *file)
  95. {
  96.     int i=6;
  97.     
  98.     while(i--) {
  99.         free_irq(buttons[i].irq, &buttons[i]);
  100.     }
  101.     up(&button_lock);
  102.     return 0;
  103. }

  104. static struct file_operations sixdrv_ops = {
  105.     .owner = THIS_MODULE,
  106.     .open = sixdrv_open,
  107.     .read = sixdrv_read,
  108.     .release = sixdrv_close,
  109. };
  110. static int sixdrv_init(void)
  111. {
  112.     int ret;
  113.     int devt = SIXDEV;
  114.     
  115.     ret = register_chrdev_region(devt, 1, "sixdrv");
  116.     if (ret) {
  117.         printk(KERN_ERR "Unable to register minors for sixdrv\n");
  118.         goto fail;
  119.     }
  120.     sixdrv_class = class_create(THIS_MODULE, "sixdrv_class");
  121.     if (IS_ERR(sixdrv_class)) {
  122.         printk(KERN_ERR "can't register device class\n");
  123.         return PTR_ERR(sixdrv_class);
  124.     }
  125.     device_create(sixdrv_class, NULL, devt, NULL, "buttons");
  126.     cdev_init(&sixdrv_cdev, &sixdrv_ops);
  127.     ret = cdev_add(&sixdrv_cdev, devt, 1);
  128.     if (ret < 0)
  129.         goto fail_cdev;
  130.     return 0;
  131.     
  132. fail_cdev:
  133.     class_unregister(sixdrv_class);
  134.     device_destroy(sixdrv_class, devt);
  135.     cdev_del(&sixdrv_cdev);
  136. fail:
  137.     unregister_chrdev_region(devt, 1);
  138.     
  139.     return 0;
  140. }

  141. static void sixdrv_exit(void)
  142. {
  143.     class_unregister(sixdrv_class);
  144.     device_destroy(sixdrv_class, SIXDEV);
  145.     cdev_del(&sixdrv_cdev);
  146.     unregister_chrdev_region(SIXDEV, 1);
  147. }

  148. module_init(sixdrv_init);
  149. module_exit(sixdrv_exit);
  150. MODULE_LICENSE("GPL");
应用程序如下:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <poll.h>
  6. #include <signal.h>

  7. int main(int argc, char *argv[])
  8. {
  9.     int fd, cnt=1;
  10.     unsigned char val;
  11.     
  12.     fd = open("/dev/buttons", O_RDWR);
  13.     //fd = open("/dev/buttons", O_RDWR|O_NONBLOCK);
  14.     if (fd < 0)
  15.     {
  16.         printf("Can not open device\n");
  17.         return -1;
  18.     }

  19.     while(1)
  20.     {
  21.         read(fd, &val, sizeof(unsigned char));
  22.         printf("cnt:%d, val:0x%x\n", cnt++, val);
  23.     }
  24.     
  25.     return 0;
  26. }
运行情况:
1.执行./six_app &命令后台运行应用程序时,用ps查看进程号为724的进程处于S状态,即休眠状态,在等待中断发生。
 
 
2.再次运行一个./six_app &,发现新的进程为726,处于D状态,即不可中断状态,在等待724释放信号量
 
 
3.如果运行kill -9 726发现该进程是杀不死的,因为此时./six_app处于不可中断状态,即阻塞状态,阻塞状态虽然保护了文件同时不被好几个程序读写,但也会经常发生锁死状况,因为如果进程724的./six_app如果一直不释放锁或者信号量的话,那么进程726的./six_app将一直阻塞下去。

4.当用kill -9 724杀死724进程时,726处于S状态,等待中断发生。
 
点击图片上下翻页
 
应用程序说明:上面的运行程序基于阻塞方式打开open("/dev/buttons", O_RDWR);
当调用非阻塞方式打开时open("/dev/buttons", O_RDWR|O_NONBLOCK);获得信号量并且在read后,if (!pressed)如果没有按键按下,会立即返回,所以会在while(1)里面打印出很多信息。这个实验不上图。
 
更多同步互斥阻塞知识请在互联网或者相关书籍中搜索,定会找到更详细内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值