Linux驱动之 等待队列waitqueue

如何通过等待队列实现对进程的阻塞?

当进程要获取某些资源(例如从网卡读取数据)的时候,但资源并没有准备好(例如网卡还没接收到数据),这时候内核必须切换到其他进程运行,直到资源准备好再唤醒进程。
    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
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Linux waitqueue 是一种 Linux 内核中的同步机制,它用于控制进程的执行顺序,使进程之间能够协调进行。 它通过让一个进程在等待另一个进程完成某个操作时进入睡眠状态,并在另一个进程完成操作后唤醒等待的进程。这样,它可以避免进程在不同步的情况下同时进行某些操作,从而减少系统资源的浪费。 因此,waitqueue 机制是 Linux 内核中常用的一种机制,它可以保证系统的正确性和高效性。 ### 回答2: 在Linux操作系统中,waitqueue是一种用于进程或线程等待的机制。 当一个进程或线程需要等待某个条件满足时,它可以使用waitqueue机制来挂起自己的执行。等待队列waitqueue)是一个数据结构,用于维护等待某个事件发生的进程或线程的列表。 当一个条件被满足时,比如某个共享资源变为可用,就会唤醒等待该条件的进程或线程。唤醒的过程是通过使用wake_up函数来实现的。 当一个进程或线程需要等待条件满足时,它会调用wait_event函数,将自己加入到等待队列中,并将自己标记为等待状态。之后,该进程或线程就会进入睡眠状态,并且由调度器决定运行其他进程或线程。 当条件满足时,比如共享资源变为可用,唤醒该条件的进程或线程的时候,会调用wake_up函数来唤醒等待的进程或线程。被唤醒的进程或线程会从wait_event的调用处继续执行,并继续执行后续逻辑。 需要注意的是,使用waitqueue机制需要配合锁机制使用,以避免竞态条件的产生。在加入等待队列和唤醒过程中,需要对共享资源进行加锁保护,以防止并发访问导致的数据不一致性。 总之,waitqueueLinux中一种用于进程或线程等待的机制,它通过等待队列来管理等待某个条件满足的进程或线程,并通过唤醒函数来唤醒等待的进程或线程。它是实现同步和互斥的重要工具之一,能够实现进程或线程之间的协作与同步。 ### 回答3: Linux中的waitqueue等待队列)是一种用于进程调度的机制。它允许一个或多个进程阻塞并等待某个特定条件的满足。 waitqueue是一个数据结构,类似于一个队列,用于存储等待某个条件满足的进程。当一个进程等待某个条件时,它会将自己添加到waitqueue中,并进入睡眠状态。 在Linux内核中,waitqueue通常与锁(如spinlock或mutex)结合使用。当一个进程需要等待某个条件时,它需要先获取锁,在锁的保护下将自己添加到waitqueue中,然后释放锁并进入睡眠状态。当条件满足时,另一个进程会获取相同的锁,唤醒等待waitqueue中的进程。 waitqueue的实现依赖于内核调度器。当一个进程被唤醒时,它会从睡眠状态返回到可运行状态,并进入内核调度器的调度队列等待分配CPU执行。 waitqueue提供了一种线程同步的机制,使得进程可以等待某个条件满足而不需要忙等待。它在很多Linux内核中的子系统中广泛使用,如设备驱动、文件系统等。 总结来说,waitqueueLinux内核中用于进程调度的一种机制,它允许一个或多个进程等待某个条件的满足。它借助锁和睡眠状态实现进程的阻塞和唤醒,依赖于内核调度器进行进程的调度。waitqueue在提供进程同步、避免忙等待等方面发挥了重要作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程一时爽Cxx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值