字符设备驱动-同步互斥阻塞

我们想来达到一个目的:同一时刻,只能有一个应用程序打开/dev/buttons

一般的,我们想达到目的会想到下面这种方法:
static int canopen = 1;
static int sixth_drv_open(struct inode *inode,struct file *file)
{
    if(--canopen != 0)
    {
        canopen++;
        return -EBUSY;
    }
}
static int sixth_drv_close (struct inode *inode, struct file *file)
{
    canopen++;
}

假设程序A来调用,那么进入open函数,canopen = 0,if条件不成立;如果A没有释放,程序B来调用时候,canopen = -1,if条件成立,return -EBUSY;

原则上是可以实现,但是我们进行–canopen操作实际上转化程汇编使用三条指令(读->改->写)执行的,由于Linux是多任务编程的。当我们程序A进行–canopen的读取后,程序B刚好进行那么就有可能发生程序AB都能调用驱动!看下图分析:

这里写图片描述

下面来介绍三种解决上面bug的方法

一、原子操作:

原子操作指的是在执行过程中不会被别的代码路径所中断的操作。
常用原子操作函数举例:

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。

Makefile

KERN_DIR = /work/system/linux-2.6.22.6

all:
    make -C $(KERN_DIR) M=`pwd` modules 

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean
    rm -rf modules.order

obj-m += sixth_drv.o

驱动函数:sixth_drv.c

#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/fs.h>  
#include <linux/init.h>  
#include <linux/delay.h>  
#include <linux/irq.h>
#include <asm/uaccess.h>  
#include <asm/irq.h>  
#include <asm/io.h>  
#include <asm/arch/regs-gpio.h>  
#include <asm/hardware.h>
#include <linux/poll.h>

static struct fasync_struct *button_async;
static struct class *sixthdrv_class;
static struct class_device *sixthdrv_class_dev;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,s3c24xx_sixth_read将它清0 */
static volatile int ev_press = 0;

volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;

struct pin_desc{
    unsigned int pin;
    unsigned int key_val;
};

/* 键值: 按下时,0x01、0x02、0x03 */
/* 键值: 松开时,0x81、0x82、0x83 */
static unsigned char key_val;

struct pin_desc pin_desc[3] = {
    {S3C2410_GPF0,0X01},
    {S3C2410_GPF2,0X02},
    {S3C2410_GPG3,0X03},
};
atomic_t canopen = ATOMIC_INIT(1);//定义原子变量canopen并初始化为1

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);
        *gpfdat |= ((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        /* 按下 */
        key_val = pindesc->key_val;
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    ev_press = 1;                /* 表示中断发生了 */
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    kill_fasync (&button_async, SIGIO, POLL_IN);
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int sixth_drv_open(struct inode *inode,struct file *file)
{
    if(!atomic_dec_and_test(&canopen))
    {
        atomic_inc(&canopen); 
        return -EBUSY;

    }
    /* 配置GPF0,2、GPG3为中断引脚 */
    request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]);
    request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]);
    request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]);
    /* 配置GPF4、5、6为输入引脚 */
    *gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
    *gpfcon |=  ((1<<4*2) | (1<<5*2) | (1<<6*2));
    return 0;
}

static ssize_t sixth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    //看用户需要读取的空间,和这里的是否相同
    if(count != 1)
        return -EINVAL;
    /* 如果无按键动作发生,则进行休眠状态 */
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq,ev_press);

    /* 如果有按键动作发生,则返回按键的值 */
    copy_to_user(buf,&key_val,1);
    ev_press = 0;

    return 1;
}

static int sixth_drv_close (struct inode *inode, struct file *file)
{
    atomic_inc(&canopen); 
    free_irq(IRQ_EINT0,  &pin_desc[0]);
    free_irq(IRQ_EINT2,  &pin_desc[1]);
    free_irq(IRQ_EINT11, &pin_desc[2]);
    return 0;

}

static unsigned int sixth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;

    poll_wait(file, &button_waitq, wait);

    if (ev_press)
        mask |= POLLIN | POLLRDNORM;   
    return mask;
}

static int sixth_drv_fasync (int fd, struct file *filp, int on)
{
    printk("driver: sixth_drv_fasync\n");
    return fasync_helper (fd, filp, on, &button_async);
}

static struct file_operations sixth_drv_fops = {
    .owner   = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open    = sixth_drv_open,
    .read    = sixth_drv_read,
    .release = sixth_drv_close,
    .poll    = sixth_drv_poll,
    .fasync  = sixth_drv_fasync,
};

int major; 

static int sixth_drv_init(void)
{
        major = register_chrdev(0,"sixth_drv",&sixth_drv_fops);
        sixthdrv_class = class_create(THIS_MODULE,"sixthdrv");
        sixthdrv_class_dev = class_device_create(sixthdrv_class,NULL,MKDEV(major,0),NULL,"buttons");
        gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
        gpfdat = gpfcon + 1;
        gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
        gpgdat = gpgcon + 1;
        return 0;
}

static int sixth_drv_exit(void)
{
        unregister_chrdev(major,"sixth_drv");
        class_device_unregister(sixthdrv_class_dev);
        class_destroy(sixthdrv_class);
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
}

module_init(sixth_drv_init);
module_exit(sixth_drv_exit);
MODULE_LICENSE("GPL");

驱动测试函数:sixthdrvtest.c

#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stdio.h>  
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

/* 
 * sixthdrvtest
 */
int fd;

void czg_signal_handler(int signum)
{
    unsigned char key_val = 0;
    read(fd,&key_val,1);
    printf("key_val: 0x%x\n",key_val);
}

int main(int argc, char **argv)
{
    int ret;
    int oflags;
    signal(SIGIO,czg_signal_handler);
    fd = open("/dev/buttons",O_RDWR);
    if(fd < 0)
    {
        printf("can't open!\n");
        return -1;
    }
    fcntl(fd,F_SETOWN,getpid()); // 告诉内核,发给谁
    oflags = fcntl(fd,F_GETFL);
    fcntl(fd,F_SETFL,oflags | FASYNC); // 改变fasync标记,
    //最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
    //然后当按键按下时候,在irqreturn_t buttons_irq中断处理中调用kill_fasync
    while(1)
    {
        sleep(1000);
    }
    return 0;
}
测试:
make
arm-linux-gcc -o sixthdrvtest sixthdrvtest.c 
cp sixthdrvtest sixth_drv.ko /work/nfs_root/czg
insmod sixth_drv.ko
./sixthdrvtest &

这里写图片描述
这里写图片描述

二、信号量:

信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。当获取不到信号量时,进程进入休眠等待状态

//定义信号量
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);
int down_interruptible(struct semaphore * sem); 
int down_trylock(struct semaphore * sem);
//释放信号量
void up(struct semaphore * sem);

代码只需要在上面的驱动程序sixth_drv.c稍做修改

这里写图片描述
这里写图片描述
这里写图片描述

static DECLARE_MUTEX(button_lock);     //定义互斥锁
/* 获 取 信 号 量 */
down(&button_lock);
/* 释放信号量 */
up(&button_lock);

这里写图片描述
这里写图片描述
这里写图片描述

三、阻塞:

阻塞操作:

是指在执行设备操作时若不能获得资源则挂起进程,直到满足可操作的条件后再进行操作。被挂起的进程进入休眠状态,被从调度器的运行队列移走,直到等待的条件被满足。

非阻塞操作:

进程在不能进行设备操作时并不挂起,它或者放弃,或者不停地查询,直至可以进行操作为止。

这里写图片描述

代码在上面的驱动程序sixth_drv.c稍做修改

这里写图片描述
这里写图片描述

static int sixth_drv_open(struct inode *inode,struct file *file)
{
    if (file->f_flags &  O_NONBLOCK)
    {
        if (down_trylock(&button_lock))
            return -EBUSY;
    }
    else
    {
        /* 获 取 信 号 量 */
        down(&button_lock);
    }
}
/*************************************************/
static ssize_t sixth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    //看用户需要读取的空间,和这里的是否相同
    if(count != 1)
        return -EINVAL;
    if(file->f_flags & O_NONBLOCK)
    {
        if(!ev_press)
            return -EAGAIN;
    }
    else
    {
    /* 如果无按键动作发生,则进行休眠状态 */
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq,ev_press);
    }
}
阻塞:

驱动测试代码:sixthdrvtest.c

#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stdio.h>  
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

/* 
 * sixthdrvtest
 */
int fd;

void czg_signal_handler(int signum)
{
    unsigned char key_val = 0;
    read(fd,&key_val,1);
    printf("key_val: 0x%x\n",key_val);
}

int main(int argc, char **argv)
{
    int ret;
    int oflags;
    unsigned char key_val = 0;
    //signal(SIGIO,czg_signal_handler);
    fd = open("/dev/buttons",O_RDWR);
    if(fd < 0)
    {
        printf("can't open!\n");
        return -1;
    }
    //fcntl(fd,F_SETOWN,getpid()); // 告诉内核,发给谁
    //oflags = fcntl(fd,F_GETFL);
    //fcntl(fd,F_SETFL,oflags | FASYNC); // 改变fasync标记,
    //最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
    //然后当按键按下时候,在irqreturn_t buttons_irq中断处理中调用kill_fasync
    while(1)
    {
        read(fd,&key_val,1);
        printf("key_val: 0x%x\n",key_val);
        //sleep(1000);
    }
    return 0;
}

这里写图片描述

非阻塞:

驱动测试代码:sixthdrvtest.c

#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>  
#include <stdio.h>  
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>

/* 
 * sixthdrvtest
 */
int fd;

void czg_signal_handler(int signum)
{
    unsigned char key_val = 0;
    read(fd,&key_val,1);
    printf("key_val: 0x%x\n",key_val);
}

int main(int argc, char **argv)
{
    int ret;
    int oflags;
    unsigned char key_val = 0;
    //signal(SIGIO,czg_signal_handler);
    fd = open("/dev/buttons",O_RDWR | O_NONBLOCK);
    if(fd < 0)
    {
        printf("can't open!\n");
        return -1;
    }
    //fcntl(fd,F_SETOWN,getpid()); // 告诉内核,发给谁
    //oflags = fcntl(fd,F_GETFL);
    //fcntl(fd,F_SETFL,oflags | FASYNC); // 改变fasync标记,
    //最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
    //然后当按键按下时候,在irqreturn_t buttons_irq中断处理中调用kill_fasync
    while(1)
    {
        ret = read(fd,&key_val,1);
        printf("key_val: 0x%x,ret = %d\n",key_val,ret);
        sleep(4);
    }
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

种瓜大爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值