如何通过等待队列实现对进程的阻塞?
当进程要获取某些资源(例如从网卡读取数据)的时候,但资源并没有准备好(例如网卡还没接收到数据),这时候内核必须切换到其他进程运行,直到资源准备好再唤醒进程。
waitqueue (等待队列) 就是内核用于管理等待资源的进程,当某个进程获取的资源没有准备好的时候,可以通过调用
add_wait_queue() 函数把进程添加到 waitqueue 中,然后切换到其他进程继续执行。当资源准备好,由资源提供方通过调用
wake_up() 函数来唤醒等待的进程。
Linux 通过结构体task_struct维护所有运行的线程、进程,不同状态的任务,会由不同的队列进行维护,schedule()函数就负责根据这些状态的变化调度这些任务。
定义、初始化等待队列头(workqueue)
#include <linux/wait.h>
DECLARE_WAIT_QUEUE_HEAD(wq);
wait_queue_head_t wq;
init_waitqueue_head(&wq);
阻塞接口(睡眠)
//wq 为定义的等待队列头,
//condition 为条件表达式,当wake up后,condition为真时,唤醒阻塞的进程,为假时,继续睡眠。
wait_event(wq, condition)
wait_event_timeout(wq, condition, timeout)
wait_event_interruptible(wq, condition)
wait_event_interruptible_timeout(wq, condition, timeout)
wait_event_hrtimeout(wq, condition, timeout)
wait_event_interruptible_hrtimeout(wq, condition, timeout)
wait_event_interruptible_exclusive(wq, condition)
wait_event_interruptible_locked(wq, condition)
wait_event_interruptible_locked_irq(wq, condition)
wait_event_interruptible_exclusive_locked(wq, condition)
wait_event_interruptible_exclusive_locked_irq(wq, condition)
wait_event_killable(wq, condition)
wait_event_lock_irq_cmd(wq, condition, lock, cmd)
wait_event_lock_irq(wq, condition, lock)
wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd)
wait_event_interruptible_lock_irq(wq, condition, lock)
wait_event_interruptible_lock_irq_timeout(wq, condition, lock, timeout)
常用阻塞接口(睡眠)
wait_event(wq, condition); //不可中断的睡眠,条件一直不满足,会一直睡眠。
wait_event_timeout(wq, condition, timeout);//不可中断睡眠,当超过指定的timeout(单位是jiffies)时间,不管有没有wake up,还是条件没满足,都要唤醒进程,此时返回的是0。在timeout时间内条件满足返回值为timeout或者1;
wait_event_interruptible(wq, condition); //可被信号中断的睡眠,被信号打断唤醒时,返回负值-ERESTARTSYS;wake up时,条件满足的,返回0。除了wait_event没有返回值,其它的都有返回,有返回值的一般都要判断返回值。
解除阻塞接口(唤醒)
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
常用的解除阻塞接口(唤醒)
wake_up(x); //一次只能唤醒挂在这个等待队列头上的一个进程
wake_up_nr(x, nr); //一次唤起nr个进程(等待在同一个wait_queue_head_t有很多个)
wake_up_all(x); //一次唤起所有等待在同一个wait_queue_head_t上所有进程
wake_up_interruptible(x); //对应wait_event_interruptible版本的wake up
wake_up_interruptible_sync(x); //保证wake up的动作原子性,wake_up这个函数,很有可能函数还没执行完,就被唤起来进程给抢占了,这个函数能够保证wak up动作完整的执行完成。
cdev_hello.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
wait_queue_head_t rwq,wwq; //等待队列
int flag = 0; //0 empty 1 full
#define KMAX_LEN 32
char kbuf[KMAX_LEN+1] = "kernel_data";
#define NEWCHRDEV_CNT 1 /* 设备号个数 */
#define NEWCHRDEV_NAME "hello" /* 名字 */
/* newchrdev设备结构体 */
struct newchr_dev{
dev_t devid; /* 设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
};
struct newchr_dev chr_hello; /* 设备hello */
/*
* @description : 打开设备
* @param - inode : 传递给驱动的inode
* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量
* 一般在open的时候将private_data指向设备结构体。
* @return : 0 成功;其他 失败
*/
static int hello_open (struct inode *inode, struct file *filep)
{
printk("hello_open()\n");
return 0;
}
/*
* @description : 关闭/释放设备
* @param - filp : 要关闭的设备文件(文件描述符)
* @return : 0 成功;其他 失败
*/
static int hello_release (struct inode *inode, struct file *filep)
{
printk("hello_release()\n");
return 0;
}
/*
* @description : 从设备读取数据
* @param - filp : 要打开的设备文件(文件描述符)
* @param - buf : 返回给用户空间的数据缓冲区
* @param - size : 要读取的数据长度
* @param - pos : 相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t hello_read (struct file *filp, char __user *buf, size_t size, loff_t *pos)
{
int error;
wait_event_interruptible(&wq, flag == 1); //r堵塞睡眠,可打断
if(size > strlen(kbuf))
{
size = strlen(kbuf);
}
if(copy_to_user(buf,kbuf, size))
{
error = -EFAULT;
return error;
}
flag = 0;
wake_up_interruptible(&wwq); //唤醒w
return size;
}
/*
* @description : 向设备写数据
* @param - filp : 设备文件,表示打开的文件描述符
* @param - buf : 要写给设备写入的数据
* @param - size : 要写入的数据长度
* @param - pos : 相对于文件首地址的偏移
* @return : 写入的字节数,如果为负值,表示写入失败
*/
static ssize_t hello_write (struct file *filp, const char __user *buf, size_t size, loff_t *pos)
{
int error;
wait_event_interruptible(wwq, flag == 0);//w堵塞睡眠,可打断
if(size > KMAX_LEN)
{
size = KMAX_LEN;
}
memset(kbuf,0,sizeof(kbuf));
if(copy_from_user(kbuf, buf, size))
{
error = -EFAULT;
return error;
}
flag = 0;
wake_up_interruptible(&rwq); //唤醒r
printk("kernel kbuf: %s\n",kbuf);
return size;
}
/* 设备操作函数 */
static struct file_operations chr_hello_ops = {
.owner = THIS_MODULE,
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write,
};
/*
* @description : 驱动入口函数
* @param : 无
* @return : 0 成功;其他 失败
*/
static int hello_init(void)
{
int result = 0;
printk("chrdev_hello init!\r \n");
printk("kernel kbuf: %s\n",kbuf);
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (chr_hello.major) { /* 定义了设备号 */
chr_hello.devid = MKDEV(chr_hello.major, 0);
/* 据定义设备号申请注册 */
result = register_chrdev_region(chr_hello.devid, NEWCHRDEV_CNT, NEWCHRDEV_NAME);
if(result < 0){
printk("register_chrdev fail \n");
goto out_err_1;
}
} else { /* 没有定义设备号,自动分配*/
result = alloc_chrdev_region(&chr_hello.devid, 0, NEWCHRDEV_CNT, NEWCHRDEV_NAME); /* 申请设备号 */
if(result < 0){
printk("alloc_chrdev_region fail \n"); //自动分配设备号错误
goto out_err_1;
}
chr_hello.major = MAJOR(chr_hello.devid); /* MAJOR宏获取分配号的主设备号 */
chr_hello.minor = MINOR(chr_hello.devid); /* MINOR宏获取分配号的次设备号 */
}
printk("chr_hello major=%d,minor=%d\r\n",chr_hello.major, chr_hello.minor);
/* 2、初始化cdev */
chr_hello.cdev.owner = THIS_MODULE;
cdev_init(&chr_hello.cdev, &chr_hello_ops);
/* 3、添加一个cdev */
cdev_add(&chr_hello.cdev, chr_hello.devid, NEWCHRDEV_CNT);
/* 4、创建类 */
chr_hello.class = class_create(THIS_MODULE, NEWCHRDEV_NAME);
if (IS_ERR(chr_hello.class)) {
printk(KERN_ERR "class_create() failed\n");
result = PTR_ERR(chr_hello.class);
goto out_err_2;
}
/* 5、创建设备 */
chr_hello.device = device_create(chr_hello.class, NULL, chr_hello.devid, NULL, NEWCHRDEV_NAME);
if (IS_ERR(chr_hello.device)) {
printk(KERN_ERR "device_create() failed\n");
result = PTR_ERR(chr_hello.device);
goto out_err_3;
}
/* 等待队列初始化 */
init_waitqueue_head(&rwq);
init_waitqueue_head(&wwq);
return 0;
//释放已申请的资源返回
out_err_3:
device_destroy(chr_hello.class, chr_hello.devid); /* 删除device */
out_err_2:
class_destroy(chr_hello.class); /* 删除class */
unregister_chrdev_region(chr_hello.devid, NEWCHRDEV_CNT); /* 注销设备号 */
cdev_del(&chr_hello.cdev);/* 删除cdev */
out_err_1:
return result;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static void hello_exit(void)
{
printk("chrdev_hello exit!\r \n");
/* 注销字符设备驱动 */
device_destroy(chr_hello.class, chr_hello.devid); /* 删除device */
class_destroy(chr_hello.class); /* 删除class */
unregister_chrdev_region(chr_hello.devid, NEWCHRDEV_CNT); /* 注销设备号 */
cdev_del(&chr_hello.cdev);/* 删除cdev */
return;
}
//modinfo name.ko
MODULE_LICENSE("GPL"); //遵循GPL协议
MODULE_AUTHOR("CJX");
MODULE_DESCRIPTION("Just for Demon");
module_init(hello_init);
module_exit(hello_exit);
//cat proc/devices