内核等待队列的作用为把当前线程放入等待队列,挂起当前线程,等待其他地方唤醒之后继续执行。
使用流程:
1 声明:
wait_queue_head_t wq;
2 初始化:
init_waitqueue_head(&wq);
3 将当前线程加入等待队列,当前线程被阻塞挂起,等待条件满足.
如条件满足,则继续往下执行。
r = wait_event_interruptible(wq, flag != 0);
上面等待条件为flag不为0,
继续执行条件为 :其他线程调用唤醒并且flag不为0。
4 唤醒
wake_up_interruptible(&wq);
以下为完整的示例代码
字符驱动代码:
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/wait.h>
#define MY_MAJOR 403
#define MY_MAX_MINORS 1
wait_queue_head_t wq;
int flag = 0;
//https://linux-kernel-labs.github.io/refs/heads/master/labs/kernel_api.html#spinlock
//https://linux-kernel-labs.github.io/refs/heads/master/labs/kernel_api.html#mutex
//sudo mknod /dev/mycdev001 c 403 0
struct my_device_data {
struct cdev cdev;
};
struct my_device_data devs[MY_MAX_MINORS];
/*
*
*https://linux-kernel-labs.github.io/refs/heads/master/labs/device_drivers.html#ioctl-1
* https://embetronicx.com/tutorials/linux/device-drivers/ioctl-tutorial-in-linux/
** This function will be called when we write IOCTL on the Device file
*/
static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
pr_info("my_ioctl\n");
pr_info("my_ioctl %d \n", cmd);
switch (cmd) {
case 0:
pr_info("0 wait_event_interruptible\n");
int r = wait_event_interruptible(wq, flag != 0);
pr_info("0 wait_event_interruptible: %d\n", r);
pr_info("0 wait_event_interruptible 2 \n");
flag = 0;
break;
case 1:
flag = 1;
pr_info("1 start to wake_up 0 now \n");
wake_up_interruptible(&wq);
break;
default:
pr_info("Default\n");
break;
}
return 0;
}
const struct file_operations my_fops = { .owner = THIS_MODULE,
.unlocked_ioctl = my_ioctl };
static __init int hello_2_init(void) {
int err;
int i;
pr_info("a3 init_module\n");
err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
"my_device_driver");
if (err != 0) {
/* report error */
return err;
}
for (i = 0; i < MY_MAX_MINORS; i++) {
/* initialize devs[i] fields */
cdev_init(&devs[i].cdev, &my_fops);
cdev_add(&devs[i].cdev, MKDEV(MY_MAJOR, i), 1);
}
init_waitqueue_head(&wq);
return 0;
}
static void __exit hello_2_exit(void) {
int i;
pr_info("a3 cleanup_module\n");
for (i = 0; i < MY_MAX_MINORS; i++) {
/* release devs[i] fields */
cdev_del(&devs[i].cdev);
}
unregister_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS);
}
module_init( hello_2_init);
module_exit( hello_2_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c driver");
应用测试代码:通过ioctl发送 指令0和1。
以下以默认阻塞模式打开,由于我们在驱动中写了 cmd为0的时候,会阻塞当前线程,所以我们的线程1会被阻塞挂起,只打印了10之后就等待线程2的执行。
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
/**
*
*/
static fd;
void* calls(void *ptr) {
printf("1 0\n");
ioctl(fd, 0, 100);
printf("1 0\n");
return NULL;
}
void* calls2(void *ptr) {
printf("2 0\n");
ioctl(fd, 1, 100);
printf("2 1\n");
return NULL;
}
int main() {
fd = open("/dev/mycdev001", O_RDWR, 0);
if (fd < 0) {
printf("error open");
exit(0);
}
pthread_t thread;
pthread_t thread2;
pthread_create(&thread, NULL, calls, NULL);
sleep(4);
pthread_create(&thread2, NULL, calls2, NULL);
pthread_join(thread, NULL);
pthread_join(thread2, NULL);
printf("main exit\n");
close(fd);
return EXIT_SUCCESS;
}
Make文件:
obj-m += wait_queue.o
PWD := $(CURDIR)
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
rm *.ko *.mod *.mod.o *.o
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
a:
sudo rmmod wait_queue
sudo insmod wait_queue.ko