【Linux驱动】阻塞型I/O(二+并发控制)

承接上文,这里继续学习linux内核驱动并发控制阻塞型I/O。
废话不多说,直接看代码,基础接口函数请自行查阅相关资料,比如《LDD》。
另外并发控制信号量和linux应用层的信号量概念和原理是差不多的,在内核态使用有所差别而已。
驱动code:wqlkp.c
关键词: init_waitqueue_head()、wait_event_interruptible()、wake_up_interruptible()
sema_init()、down_interruptible()、up()

#include <linux/module.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>

MODULE_LICENSE("Dual BSD/GPL");

#define DEBUG_SWITCH 1
#if DEBUG_SWITCH
    #define P_DEBUG(fmt, args...) printk("<1>" "<kernel>[%s]"fmt,__FUNCTION__, ##args)
#else
    #define P_DEBUG(fmt, args...) printk("<7>" "<kernel>[%s]"fmt,__FUNCTION__, ##args)
#endif

#define DEV_SIZE 20//方便测试写进程阻塞
#define WQ_MAJOR 230

struct wq_dev{
    char kbuf[DEV_SIZE];//缓冲区
    dev_t devno;//设备号
    unsigned int major;
    struct cdev wq_cdev;
    unsigned int cur_size;//可读可写的数据量
    struct semaphore sem;//信号量
    wait_queue_head_t r_wait;//读等待队列
    wait_queue_head_t w_wait;//写等待队列
};

//struct wq_dev *wq_devp;

int wq_open(struct inode *inodep, struct file *filp)
{
    struct wq_dev *dev;
    dev = container_of(inodep->i_cdev, struct wq_dev, wq_cdev);
    filp->private_data = dev;
    printk(KERN_ALERT "open is ok!\n");
    return 0;
}

int wq_release(struct inode *inodep, struct file *filp)
{
    printk(KERN_ALERT "release is ok!\n");
    return 0;
}

static ssize_t wq_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
    struct wq_dev *dev = filp->private_data;

    P_DEBUG("read data...\n");

    if(down_interruptible(&dev->sem))//获取信号量
    {
        P_DEBUG("enter read down_interruptible\n");
        return -ERESTARTSYS;
    }
    P_DEBUG("read first down\n");
    while(dev->cur_size == 0){//无数据可读,进入休眠lon
        up(&dev->sem);//释放信号量,不然写进程没有机会来唤醒(没有获得锁)
        if(filp->f_flags & O_NONBLOCK)//检查是否是阻塞型I/O
            return -EAGAIN;
        P_DEBUG("%s reading:going to sleep\n", current->comm);
        if(wait_event_interruptible(dev->r_wait, dev->cur_size != 0))//休眠等待被唤醒
        {
            P_DEBUG("read wait interruptible\n");
            return -ERESTARTSYS;
        }
        P_DEBUG("wake up r_wait\n");
        if(down_interruptible(&dev->sem))//获取信号量
            return -ERESTARTSYS;
    }

    //数据已就绪
    P_DEBUG("[2]dev->cur_size is %d\n", dev->cur_size);
    if(dev->cur_size > 0)
        count = min(count, dev->cur_size);

    //从内核缓冲区赋值数据到用户空间,复制成功返回0
    if(copy_to_user(buf, dev->kbuf, count))
    {
        up(&dev->sem);
        return -EFAULT;
    }   
    dev->cur_size -= count;//可读数据量更新
    up(&dev->sem);
    wake_up_interruptible(&dev->w_wait);//唤醒写进程
    P_DEBUG("%s did read %d bytes\n", current->comm, (unsigned int)count);
    return count;
}

static ssize_t wq_write(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
    struct wq_dev *dev = filp->private_data;
    //wait_queue_t my_wait;
    P_DEBUG("write is doing\n");    
    if(down_interruptible(&dev->sem))//获取信号量
    {
        P_DEBUG("enter write down_interruptible\n");
        return -ERESTARTSYS;
    }
// init_wait(&my_wait);
// add_wait_queue(&dev->w_wait, &my_wait);
    P_DEBUG("write first down\n");
    while(dev->cur_size == DEV_SIZE){//判断空间是否已满

        up(&dev->sem);//释放信号量
        if(filp->f_flags & O_NONBLOCK)
            return -EAGAIN;
        P_DEBUG("writing going to sleep\n");
        if(wait_event_interruptible(dev->w_wait, dev->cur_size < DEV_SIZE))
            return -ERESTARTSYS;
    // __set_current_state(TASK_INTERRUPTIBLE);//设置当前进程状态
    // up(&dev->sem);//释放信号量
    // P_DEBUG("befor schedule\n");
    // schedule();//进程调度,当前进程进入休眠
    // if(signal_pending(current))//检查当前进程是否有信号处理,返回不为0表示有信号处理
    // return -EAGAIN;
    // P_DEBUG("after schedule\n");
        if(down_interruptible(&dev->sem))//获取信号量
            return -ERESTARTSYS;
    }
    if(count > DEV_SIZE - dev->cur_size)
        count = DEV_SIZE - dev->cur_size;

    if(copy_from_user(dev->kbuf, buf, count))//数据复制
        return -EFAULT;
    dev->cur_size += count;//更新数据量
    P_DEBUG("write %d bytes , cur_size:[%d]\n", count, dev->cur_size);
    P_DEBUG("kbuf is [%s]\n", dev->kbuf);
    up(&dev->sem);
    wake_up_interruptible(&dev->r_wait);//唤醒读进程队列
    //__set_current_state(TASK_RUNNING);
    return count;
}

struct file_operations wq_fops = {
    .open = wq_open,
    .release = wq_release,
    .write = wq_write,
    .read = wq_read,
};

struct wq_dev my_dev;

static int __init wq_init(void)
{
    int result = 0;
    my_dev.cur_size = 0;
    my_dev.devno = MKDEV(WQ_MAJOR, 0);
    //设备号分配
    if(WQ_MAJOR)
        result = register_chrdev_region(my_dev.devno, 1, "wqlkp");
    else
    {
        result = alloc_chrdev_region(&my_dev.devno, 0, 1, "wqlkp");
        my_dev.major = MAJOR(my_dev.devno);
    }
    if(result < 0)
        return result;

    cdev_init(&my_dev.wq_cdev, &wq_fops);//设备初始化
    my_dev.wq_cdev.owner = THIS_MODULE;
    sema_init(&my_dev.sem, 1);//信号量初始化
    init_waitqueue_head(&my_dev.r_wait);//等待队列初始化
    init_waitqueue_head(&my_dev.w_wait);

    result = cdev_add(&my_dev.wq_cdev, my_dev.devno, 1);//设备注册
    if(result < 0)
    {
        P_DEBUG("cdev_add error!\n");
        goto err;
    }
    printk(KERN_ALERT "hello kernel\n");
    return 0;

err:
    unregister_chrdev_region(my_dev.devno,1);
}

static void __exit wq_exit(void)
{
    cdev_del(&my_dev.wq_cdev);
    unregister_chrdev_region(my_dev.devno, 1);
}

module_init(wq_init);
module_exit(wq_exit);

一开始read和write函数在最后没有释放信号量,导致运行读写进程时出现死锁,我用的最多的调试方式是printk。
回顾上面的实现,设备缓冲用的是一个普通的数组,理论上更好的方式应该是用一个循环队列,海康面试的时候就问了这个,读写文件时,读写指针是怎么变化的。
通常,我们应该在一个驱动程序中使用同种方法,如同上面程序那样。
但是对于休眠还有其余方法,程序中注释掉的休眠方式是其中一种(本例中运行时有写些许问题..),《LDD》上阐述的是另一种休眠方法,其实就是分解wait_event_interruptible()函数,我们看下他的源码(linux2.6.18;linux/wait.h)

/**
 * wait_event_interruptible - sleep until a condition gets true
 * @wq: the waitqueue to wait on
 * @condition: a C expression for the event to wait for
 *
 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 * @condition evaluates to true or a signal is received.
 * The @condition is checked each time the waitqueue @wq is woken up.
 *
 * wake_up() has to be called after changing any variable that could
 * change the result of the wait condition.
 *
 * The function will return -ERESTARTSYS if it was interrupted by a
 * signal and 0 if @condition evaluated to true.
 */
#define wait_event_interruptible(wq, condition) \
({                                  \
    int __ret = 0;                          \
    if (!(condition))                       \
        __wait_event_interruptible(wq, condition, __ret);   \
    __ret;                              \
})

#define __wait_event_interruptible_timeout(wq, condition, ret) \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                          \
        prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);  \
        if (condition)                      \
            break;                      \
        if (!signal_pending(current)) {             \
            ret = schedule_timeout(ret);            \
            if (!ret)                   \
                break;                  \
            continue;                   \
        }                           \
        ret = -ERESTARTSYS;                 \
        break;                          \
    }                               \
    finish_wait(&wq, &__wait);                  \
} while (0)

简单分析一下:
1、DEFINE_WAIT(__wait);//建立并初始化一个等待队列入口,其等效于:wait_queue_t __wait; init_wait(&__wait);
2、prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);//将等待队列入口添加到队列中,并设置进程的状态
3、schedule_timeout(ret);//进程调度变种程序,调度其余程序运行
4、finish_wait(&wq, &__wait);//这个函数内部会调用__set_current_state(TASK_RUNNING);跳出for循环后,就得设置当前进程为可运行态
看了上面的wait_event_interruptible()函数实现,相信你应该知道进程休眠是怎么回事了,进程休眠在等待队列中添加了一个wait_queue_t结构体(prepare_to_wait),这样可以在wq_read、wq_write等函数中直接根据条件进入休眠。

用户态验证程序:

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

int main(void)
{
    char buf[20];
    int fd;
    int ret;

    fd = open("/dev/wqlkp", O_RDWR);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }

    read(fd, buf, 10);
    printf("<app>buf is [%s]\n", buf);

    close(fd);
    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
    char buf[20];
    int fd;
    int ret;

    fd = open("/dev/wqlkp", O_RDWR);
    if(fd < 0)
    {
        perror("open");
        return -1;
    }

    write(fd, "wen qian", 10);

    close(fd);
    return 0;
}

整个需要注意的地方就是处理休眠唤醒以及信号量并发控制的问题,要确保你的程序不会出现死锁,或者都等待对待唤醒的状态。

技术交流,永无止境,如有错误,欢迎指正。

你可能感兴趣的:(并发,linux,IO,kernel)