同一时刻许多进程均可以打开中断/poll/异步通知 的驱动设备,如果想在同一时刻只有一个进程可以打开设备,就要用到原子操作或者信号量
先看不是原子操作的后果:
/***************************************************************
* filename: six_drv.c
* description: 通过异步机制获取按键值
* author: xyc
* create time: 2014/6/6
* version: 1
* modify info:
****************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irqreturn.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm-arm/io.h>
#include <asm-arm/uaccess.h>
static struct class *sixdrv_class;
static struct class_device *sixdrv_class_dev;
static struct fasync_struct *button_q;
static unsigned char key_val;
atomic_t v = ATOMIC_INIT(1);
static DECLARE_WAIT_QUEUE_HEAD(buttons_waitq);
struct pin_desc {
unsigned int pin;
unsigned int key_val;
};
/*
*按下:0x01, 0x02
*松开:0x81, 0x82
*/
static struct pin_desc pins_desc[2] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
};
static volatile unsigned int condition= 0;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc= (struct pin_desc * )dev_id;
unsigned int pinval;
/*读取引脚值*/
pinval = s3c2410_gpio_getpin(pindesc->pin);
/*确定键值*/
if(pinval){
/*松开*/
key_val = 0x80 | pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
else{
/*按下*/
key_val = pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
#if 0
/*按键按下或松开中断发生将condition = 1使得满足wait_event_interruptible唤醒的条件*/
condition = 1;
/*唤醒休眠的进程./six_drv_test */
wake_up_interruptible(&buttons_waitq);
#endif
/*驱动用kill_fasync发信号,button_q结构体由fasync调用驱动six_drv_fasync
*fasync_helper初始化
*/
kill_fasync(&button_q, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int canopen =1; /*第一步:先定义并初始化canopen =1*/
static int six_drv_open(struct inode *inode, struct file *file)
{
#if 0
if (!atomic_dec_and_test(&v)){
atomic_inc(&v);
return -1;
}
#endif
if(--canopen !=0){ /*第二步:自减,若是第一次打开,就执行安装中断注册函数*/
canopen++; /* 若不是第一次,则恢复canopen的值,即canopen++*/
return -1;
}
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);
return 0;
}
static ssize_t six_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
if(nbytes != 1)
return EINVAL;
/*没有按键按下时,./six_drv_test 进程休眠,*/
// wait_event_interruptible(buttons_waitq, condition);
/*有按键按下或松开时,结束休眠,并将键值传给应用程序*/
copy_to_user(buf, &key_val, 1);
/*当下一次应用调用read时,condition = 0使得./six_drv_test 进程休眠*/
// condition = 0;
return 1; /* 这里是5,应用read返回值=5 */
}
static int six_drv_release(struct inode * inode, struct file * file)
{
//atomic_inc(&v);
canopen++; /*第三步:关闭设备时,第一次打开设备的canopen值需要恢复为1*/
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
return 0;
}
static int six_drv_fasync(int fd, struct file *file, int on)
{
int result;
result = fasync_helper(fd, file, on, &button_q);
return (result);
}
static const struct file_operations six_drv_fops = {
.owner = THIS_MODULE,
.read = six_drv_read,
.open = six_drv_open,
.release = six_drv_release,
.fasync = six_drv_fasync,
};
int major;
static int __init six_drv_init(void)
{
major = register_chrdev(0, "six_drv", &six_drv_fops);
sixdrv_class = class_create(THIS_MODULE, "six_drv");
sixdrv_class_dev = class_device_create(sixdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
return 0;
}
static void __exit six_drv_exit(void)
{
/* 卸载驱动程序 */
unregister_chrdev(major, "six_drv");
class_device_unregister(sixdrv_class_dev);
class_destroy(sixdrv_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(six_drv_init);
module_exit(six_drv_exit);
MODULE_LICENSE("GPL");
注意注释的一二三步中的if(--canopen !=0)语句,此语句包含canopen从内存中读值,修改值,写值,如果进程A第一次去读canopen =1,这时切换到进程B了读到canopen =1,
然后B就可以打开设备,打开完了进程切换回进程A,这时将canopen 自减使canopen =0,写回canopen内存地址,此时A进程也可打开此设备,违背了在同一时刻只有一个进程打开设备的本意,如果将canopen的自减,修改,写回三步视为一个整体,中间不可切换即是1个原子操作,则不会出现2个进程同一时刻打开同一设备的问题,
1. 原子操作
原子操作指的是在执行过程中不会被别的代码路径所中断的操作。
常用原子操作函数举例:
atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初始化为0
atomic_read(atomic_t *v); //返回原子变量的值
void atomic_inc(atomic_t *v); //原子变量增加1
void atomic_dec(atomic_t *v); //原子变量减少1
int atomic_dec_and_test(atomic_t *v); //自减操作后测试其是否为0,为0则返回true,否则返回false。
按照上面canopen的步骤,利用原子操作来改写此程序:
/***************************************************************
* filename: six_drv.c
* description: 通过异步机制获取按键值
* author: xyc
* create time: 2014/6/6
* version: 1
* modify info:
****************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irqreturn.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm-arm/io.h>
#include <asm-arm/uaccess.h>
static struct class *sixdrv_class;
static struct class_device *sixdrv_class_dev;
static struct fasync_struct *button_q;
static unsigned char key_val;
atomic_t v = ATOMIC_INIT(1); /*第一步:定义并初始化原子变量v=1*/
static DECLARE_WAIT_QUEUE_HEAD(buttons_waitq);
struct pin_desc {
unsigned int pin;
unsigned int key_val;
};
/*
*按下:0x01, 0x02
*松开:0x81, 0x82
*/
static struct pin_desc pins_desc[2] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
};
static volatile unsigned int condition= 0;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc= (struct pin_desc * )dev_id;
unsigned int pinval;
/*读取引脚值*/
pinval = s3c2410_gpio_getpin(pindesc->pin);
/*确定键值*/
if(pinval){
/*松开*/
key_val = 0x80 | pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
else{
/*按下*/
key_val = pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
#if 0
/*按键按下或松开中断发生将condition = 1使得满足wait_event_interruptible唤醒的条件*/
condition = 1;
/*唤醒休眠的进程./six_drv_test */
wake_up_interruptible(&buttons_waitq);
#endif
/*驱动用kill_fasync发信号,button_q结构体由fasync调用驱动six_drv_fasync
*fasync_helper初始化
*/
kill_fasync(&button_q, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int six_drv_open(struct inode *inode, struct file *file)
{
/*第二步:原子变量自减,并测试*/
if (!atomic_dec_and_test(&v)){
atomic_inc(&v);
return -1;
}
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);
return 0;
}
static ssize_t six_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
if(nbytes != 1)
return EINVAL;
/*没有按键按下时,./six_drv_test 进程休眠,*/
// wait_event_interruptible(buttons_waitq, condition);
/*有按键按下或松开时,结束休眠,并将键值传给应用程序*/
copy_to_user(buf, &key_val, 1);
/*当下一次应用调用read时,condition = 0使得./six_drv_test 进程休眠*/
// condition = 0;
return 1; /* 这里是5,应用read返回值=5 */
}
static int six_drv_release(struct inode * inode, struct file * file)
{
atomic_inc(&v);/*恢复原子变量值为1*/
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
return 0;
}
static int six_drv_fasync(int fd, struct file *file, int on)
{
int result;
result = fasync_helper(fd, file, on, &button_q);
return (result);
}
static const struct file_operations six_drv_fops = {
.owner = THIS_MODULE,
.read = six_drv_read,
.open = six_drv_open,
.release = six_drv_release,
.fasync = six_drv_fasync,
};
int major;
static int __init six_drv_init(void)
{
major = register_chrdev(0, "six_drv", &six_drv_fops);
sixdrv_class = class_create(THIS_MODULE, "six_drv");
sixdrv_class_dev = class_device_create(sixdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
return 0;
}
static void __exit six_drv_exit(void)
{
/* 卸载驱动程序 */
unregister_chrdev(major, "six_drv");
class_device_unregister(sixdrv_class_dev);
class_destroy(sixdrv_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(six_drv_init);
module_exit(six_drv_exit);
MODULE_LICENSE("GPL");
测试代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/*sixdrvtest
*/
int fd;
void my_signal_func(int signum)
{
unsigned char key_val;
printf("signum = %d\n", signum);
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
int O_FLAGS;
signal( SIGIO, my_signal_func); /*安装信号处理函数*/
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
return -1;
}
fcntl( fd, F_SETOWN, getpid());
O_FLAGS = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, O_FLAGS|O_ASYNC);
while (1)
{
sleep(1000);
}
return 0;
}
由测试结果可知:对于原子操作,同一时刻只能有一个进程(比如A)打开同一设备,若另外的进程B试图打开此设备,则six_drv_open直接返回-1,应用的open返回-1则此进程退出,若想进程A打开设备时,此时A进程为S状态,进程B此时试图去打开,此时B进程为D状态,当A进程退出时,将进程B唤醒,进程B这时可以自动打开设备,则需要用到信号量
2. 信号量
信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。
当获取不到信号量时,进程进入休眠等待状态(D状态)。
定义信号量
struct semaphore sem;
初始化信号量
void sema_init (struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem);//初始化为0
①static DECLARE_MUTEX(button_lock); //定义互斥锁
获得信号量
②void down(struct semaphore * sem);
//只有第一个打开文件的进程PID状态为S,其它都为D状态,kill -9 PID只能杀死S状态的进程,D状态杀死不了的,不过,你杀死S状态后,
//后面紧挨PID进程的D状态变成了S状态,即可间接杀死
int down_interruptible(struct semaphore * sem);
//所有试图打开文件的进程状态都为S,可以使用kill -9 PID杀死任一进程,还有一点疑问的几个文件同时打开,然后我杀了中间几个进程,获得的结果完全不一样了,按下打印了两次,且两次的PID都不同,松开一样,用down没有这样的问题
int down_trylock(struct semaphore * sem);
释放信号量
③void up(struct semaphore * sem);
驱动源代码:
/***************************************************************
* filename: six_drv.c
* description: 通过异步机制获取按键值
* author: xyc
* create time: 2014/6/6
* version: 1
* modify info:
****************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irqreturn.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm-arm/io.h>
#include <asm-arm/uaccess.h>
static struct class *sixdrv_class;
static struct class_device *sixdrv_class_dev;
static struct fasync_struct *button_q;
static unsigned char key_val;
//atomic_t v = ATOMIC_INIT(1);
static DECLARE_MUTEX(button_lock);/*第一步:定义并初始化信号量button_lock*/
static DECLARE_WAIT_QUEUE_HEAD(buttons_waitq);
struct pin_desc {
unsigned int pin;
unsigned int key_val;
};
/*
*按下:0x01, 0x02
*松开:0x81, 0x82
*/
static struct pin_desc pins_desc[2] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
};
static volatile unsigned int condition= 0;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc= (struct pin_desc * )dev_id;
unsigned int pinval;
/*读取引脚值*/
pinval = s3c2410_gpio_getpin(pindesc->pin);
/*确定键值*/
if(pinval){
/*松开*/
key_val = 0x80 | pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
else{
/*按下*/
key_val = pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
#if 0
/*按键按下或松开中断发生将condition = 1使得满足wait_event_interruptible唤醒的条件*/
condition = 1;
/*唤醒休眠的进程./six_drv_test */
wake_up_interruptible(&buttons_waitq);
#endif
/*驱动用kill_fasync发信号,button_q结构体由fasync调用驱动six_drv_fasync
*fasync_helper初始化
*/
kill_fasync(&button_q, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int six_drv_open(struct inode *inode, struct file *file)
{
#if 0
/*第二步:原子变量自减,并测试*/
if (!atomic_dec_and_test(&v)){
atomic_inc(&v);
return -1;
}
#endif
/*第二步:A进程获得信号量,若A可获得则A进入S状态,等待按键发生
*若此时进程B想获得信号量,因为A正在打开此设备即还没有释放信号量
*,所以B进入D状态,等到A关闭了设备,释放了信号量,此时会通知B
*由D状态转为S状态,获得信号量并打开此设备
*/
down(&button_lock);
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);
return 0;
}
static ssize_t six_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
if(nbytes != 1)
return EINVAL;
/*没有按键按下时,./six_drv_test 进程休眠,*/
// wait_event_interruptible(buttons_waitq, condition);
/*有按键按下或松开时,结束休眠,并将键值传给应用程序*/
copy_to_user(buf, &key_val, 1);
/*当下一次应用调用read时,condition = 0使得./six_drv_test 进程休眠*/
// condition = 0;
return 1; /* 这里是5,应用read返回值=5 */
}
static int six_drv_release(struct inode * inode, struct file * file)
{
//atomic_inc(&v);/*恢复原子变量值为1*/
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
/*第三步:关闭设备时,释放相应的信号量*/
up(&button_lock);
return 0;
}
static int six_drv_fasync(int fd, struct file *file, int on)
{
int result;
result = fasync_helper(fd, file, on, &button_q);
return (result);
}
static const struct file_operations six_drv_fops = {
.owner = THIS_MODULE,
.read = six_drv_read,
.open = six_drv_open,
.release = six_drv_release,
.fasync = six_drv_fasync,
};
int major;
static int __init six_drv_init(void)
{
major = register_chrdev(0, "six_drv", &six_drv_fops);
sixdrv_class = class_create(THIS_MODULE, "six_drv");
sixdrv_class_dev = class_device_create(sixdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
return 0;
}
static void __exit six_drv_exit(void)
{
/* 卸载驱动程序 */
unregister_chrdev(major, "six_drv");
class_device_unregister(sixdrv_class_dev);
class_destroy(sixdrv_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(six_drv_init);
module_exit(six_drv_exit);
MODULE_LICENSE("GPL");
测试代码与上面的原子操作相同,不同的是同时打开多个测试进程,测试进程均存在(ps可现实所以打开的测试进程),第一个打开的测试进程为S状态,其它的为D状态,按键可获得按键值并打印,若将S状态的进程杀掉,紧接着的测试进程有D状态转换为S状态,按键同样可获得按键值并打印
上面的均是open为阻塞操作,即是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。
非阻塞操作
进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。
fd = open("...", O_RDWR | O_NONBLOCK); //flags加了O_NONBLOCK
源码:
/***************************************************************
* filename: six_drv.c
* description: 通过异步机制获取按键值
* author: xyc
* create time: 2014/6/6
* version: 1
* modify info:
****************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irqreturn.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <asm-arm/io.h>
#include <asm-arm/uaccess.h>
static struct class *sixdrv_class;
static struct class_device *sixdrv_class_dev;
static struct fasync_struct *button_q;
static unsigned char key_val;
//atomic_t v = ATOMIC_INIT(1);
static DECLARE_MUTEX(button_lock);
static DECLARE_WAIT_QUEUE_HEAD(buttons_waitq);
struct pin_desc {
unsigned int pin;
unsigned int key_val;
};
/*
*按下:0x01, 0x02
*松开:0x81, 0x82
*/
static struct pin_desc pins_desc[2] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
};
static volatile unsigned int condition= 0;
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc= (struct pin_desc * )dev_id;
unsigned int pinval;
/*读取引脚值*/
pinval = s3c2410_gpio_getpin(pindesc->pin);
/*确定键值*/
if(pinval){
/*松开*/
key_val = 0x80 | pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
else{
/*按下*/
key_val = pindesc->key_val ;
//printk("kernel key_val = 0x%x\n", key_val);
}
/*按键按下或松开中断发生将condition = 1使得满足wait_event_interruptible唤醒的条件*/
condition = 1;
/*唤醒休眠的进程./six_drv_test */
wake_up_interruptible(&buttons_waitq);
/*驱动用kill_fasync发信号,button_q结构体由fasync调用驱动six_drv_fasync
*fasync_helper初始化
*/
// kill_fasync(&button_q, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int six_drv_open(struct inode *inode, struct file *file)
{
#if 0
if (!atomic_dec_and_test(&v)){
atomic_inc(&v);
return -1;
}
#endif
if(file->f_flags & O_NONBLOCK){
if(down_trylock(&button_lock))
return -EBUSY;
}
else{
down(&button_lock);
}
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);
return 0;
}
static ssize_t six_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
if(nbytes != 1)
return EINVAL;
if(file->f_flags & O_NONBLOCK){
if(!condition)
return -EAGAIN;
}
else{
/*没有按键按下时,./six_drv_test 进程休眠,*/
wait_event_interruptible(buttons_waitq, condition);
}
/*有按键按下或松开时,结束休眠,并将键值传给应用程序*/
copy_to_user(buf, &key_val, 1);
/*当下一次应用调用read时,condition = 0使得./six_drv_test 进程休眠*/
condition = 0;
return 1; /* 这里是5,应用read返回值=5 */
}
static int six_drv_release(struct inode * inode, struct file * file)
{
// atomic_inc(&v);
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
up(&button_lock);
return 0;
}
static int six_drv_fasync(int fd, struct file *file, int on)
{
int result;
result = fasync_helper(fd, file, on, &button_q);
return (result);
}
static const struct file_operations six_drv_fops = {
.owner = THIS_MODULE,
.read = six_drv_read,
.open = six_drv_open,
.release = six_drv_release,
.fasync = six_drv_fasync,
};
int major;
static int __init six_drv_init(void)
{
major = register_chrdev(0, "six_drv", &six_drv_fops);
sixdrv_class = class_create(THIS_MODULE, "six_drv");
sixdrv_class_dev = class_device_create(sixdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");
return 0;
}
static void __exit six_drv_exit(void)
{
/* 卸载驱动程序 */
unregister_chrdev(major, "six_drv");
class_device_unregister(sixdrv_class_dev);
class_destroy(sixdrv_class);
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(six_drv_init);
module_exit(six_drv_exit);
MODULE_LICENSE("GPL");
测试源码:
</pre><pre name="code" class="cpp">#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/*sixdrvtest
*/
int fd;
void my_signal_func(int signum)
{
unsigned char key_val;
printf("signum = %d\n", signum);
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
int O_FLAGS,ret=0;
unsigned char key_val=0;
//signal( SIGIO, my_signal_func); /*安装信号处理函数*/
fd = open("/dev/buttons", O_RDWR|O_NONBLOCK);
if (fd < 0)
{
printf("can't open!, fd = %d\n", fd);
return -1;
}
// fcntl( fd, F_SETOWN, getpid());
// O_FLAGS = fcntl(fd, F_GETFL);
// fcntl(fd, F_SETFL, O_FLAGS|O_ASYNC);
while (1)
{
ret = read(fd, &key_val, 1);
printf("key_val = 0x%x, ret = %d\n", key_val, ret);
sleep(5);
}
return 0;
}
没用异步,/dev/buttons用非阻塞方式打开的话,直接读取按键值,若有中断发生,则read立刻返回按键值,若没有中断发生,read立刻的返回-1,若此时有另一进程A正在打开/dev/buttons,则此时的进程B调用six_drv_open直接返回-EBUSY,open返回-EBUSY,此时进程B退出,sleep(5)是为了5s中调用一次 非阻塞read且打印值key_val值,而不是在串口中不停打印
测试:
# ./sixdrvtest &
# key_val = 0x0, ret = -1
# ./sixdrvtest &
# can't open!, fd = -1
# ./sixdrvtest &
[2] - Done(255) ./sixdrvtest
# can't open!, fd = -1
# ./sixdrvtest &
[3] - Done(255) ./sixdrvtest
# can't open!, fd = -1
key_val = 0x0, ret = -1
[2] + Done(255) ./sixdrvtest
#
# ps
PID Uid VSZ Stat Command
1 0 3092 S init
2 0 SW< [kthreadd]
3 0 SWN [ksoftirqd/0]
4 0 SW< [watchdog/0]
5 0 SW< [events/0]
6 0 SW< [khelper]
55 0 SW< [kblockd/0]
56 0 SW< [ksuspend_usbd]
59 0 SW< [khubd]
61 0 SW< [kseriod]
73 0 SW [pdflush]
74 0 SW [pdflush]
75 0 SW< [kswapd0]
76 0 SW< [aio/0]
710 0 SW< [mtdblockd]
745 0 SW< [kmmcd]
762 0 SW< [rpciod/0]
770 0 3096 S -sh
786 0 1312 S ./sixdrvtest
790 0 3096 R ps
# key_val = 0x0, ret = -1
key_val = 0x0, ret = -1
key_val = 0x81, ret = 1
key_val = 0x82, ret = 1
key_val = 0x82, ret = -1
key_val = 0x82, ret = -1
key_val = 0x82, ret = -1
key_val = 0x82, ret = -1
key_val = 0x82, ret = -1
若open以阻塞方式打开,同时sleep(5)屏蔽掉,因为调用read时,若没产生按键就会休眠,此时产生按键的话,会唤醒six_drv_read中的buttons_waitq,并返回按键值给应用的read
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
/*sixdrvtest
*/
int fd;
void my_signal_func(int signum)
{
unsigned char key_val;
printf("signum = %d\n", signum);
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
int O_FLAGS,ret=0;
unsigned char key_val=0;
//signal( SIGIO, my_signal_func); /*安装信号处理函数*/
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!, fd = %d\n", fd);
return -1;
}
// fcntl( fd, F_SETOWN, getpid());
// O_FLAGS = fcntl(fd, F_GETFL);
// fcntl(fd, F_SETFL, O_FLAGS|O_ASYNC);
while (1)
{
ret = read(fd, &key_val, 1);
printf("key_val = 0x%x, ret = %d\n", key_val, ret);
// sleep(5); //屏蔽掉,因为read没有按键产生,six_drv_read会休眠
}
return 0;
}
测试结果:
# ./sixdrvtest &
# ./sixdrvtest &
# ./sixdrvtest &
# ./sixdrvtest &
# ps
PID Uid VSZ Stat Command
1 0 3092 S init
2 0 SW< [kthreadd]
3 0 SWN [ksoftirqd/0]
4 0 SW< [watchdog/0]
5 0 SW< [events/0]
6 0 SW< [khelper]
55 0 SW< [kblockd/0]
56 0 SW< [ksuspend_usbd]
59 0 SW< [khubd]
61 0 SW< [kseriod]
73 0 SW [pdflush]
74 0 SW [pdflush]
75 0 SW< [kswapd0]
76 0 SW< [aio/0]
710 0 SW< [mtdblockd]
745 0 SW< [kmmcd]
762 0 SW< [rpciod/0]
770 0 3096 S -sh
792 0 1308 S ./sixdrvtest
793 0 1308 D ./sixdrvtest
794 0 1308 D ./sixdrvtest
795 0 1308 D ./sixdrvtest
796 0 3096 R ps
# key_val = 0x1, ret = 1
key_val = 0x81, ret = 1
key_val = 0x1, ret = 1
key_val = 0x1, ret = 1
key_val = 0x1, ret = 1
key_val = 0x81, ret = 1
key_val = 0x2, ret = 1
key_val = 0x82, ret = 1
key_val = 0x2, ret = 1
key_val = 0x82, ret = 1
key_val = 0x2, ret = 1
key_val = 0x82, ret = 1
key_val = 0x1, ret = 1
key_val = 0x81, ret = 1
#
# kill -9 792
# ps
PID Uid VSZ Stat Command
1 0 3092 S init
2 0 SW< [kthreadd]
3 0 SWN [ksoftirqd/0]
4 0 SW< [watchdog/0]
5 0 SW< [events/0]
6 0 SW< [khelper]
55 0 SW< [kblockd/0]
56 0 SW< [ksuspend_usbd]
59 0 SW< [khubd]
61 0 SW< [kseriod]
73 0 SW [pdflush]
74 0 SW [pdflush]
75 0 SW< [kswapd0]
76 0 SW< [aio/0]
710 0 SW< [mtdblockd]
745 0 SW< [kmmcd]
762 0 SW< [rpciod/0]
770 0 3096 S -sh
793 0 1308 S ./sixdrvtest
794 0 1308 D ./sixdrvtest
795 0 1308 D ./sixdrvtest
797 0 3096 R ps
[1] Killed ./sixdrvtest
#
# key_val = 0x1, ret = 1
key_val = 0x81, ret = 1
key_val = 0x1, ret = 1
key_val = 0x1, ret = 1
key_val = 0x81, ret = 1
key_val = 0x2, ret = 1
key_val = 0x82, ret = 1
key_val = 0x2, ret = 1
key_val = 0x82, ret = 1
由此看出非阻塞方式,若没有按键按下,打印的值为key_val的初始值0,且5s打印一次,不断打印,若有按键按下,就打印出按键的值,然后继续打印key_val前5s获得的值,
若为阻塞,只有按键产生了,read才会返回键值并打印,平时read进入wait_event_interruptible睡眠中,
其有一个缺点:机械按键发生了抖动,需要去抖