首页 > 其他分享 >等待队列

等待队列

时间:2023-12-19 20:58:32浏览次数:29  
标签:struct 队列 class char include 等待 hello

等待队列

什么是等待队列
等待队列是内核实现阻塞和唤醒的内核机制。等待队列以循环链表为基础结构,链表头和链表项分别为等待队列头和等待队列元素。整个等待队列由等待队列头进行管理。
等待队列头使用结构体 wait_queue _head_t来表示, 等待队列头就是一个等待队列的头部,这个结构体定义在文件include/linux/wait.h里面,结构体内容如下:

struct wait queue head {
  spinlock_t lock; //自旋锁
    struct list_head task_list; //链表头  
}

typedef struct wait_queue_head wait_queue_head_t;

定义并初始化等待队列头
方法一:
(1) 定义一个等待队列头:
wait_queue head t test_wq; //定义一个等待队列的头
初始化等待队列头:
(2)
可以使用 init waitqueue head 函数初始化等待队列头,函数原型如下:

方法二:
使用宏 DECLARE_WAIT_QUEUE_HEAD 来一次性完成等待队列头的定义和初始化。
原型:
DECLARE_WAIT_QUEUE_HEAD(wait_queue_head_t q);


#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>

#include <linux/version.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h> 
#include <linux/wait.h>

#define DEVICE_NAME "poll_dev"
#define CLASS_NAME "poll_class"

/*初始化一个队列头*/
static DECLARE_WAIT_QUEUE_HEAD(read_wq);
static struct class *char_class;
static struct device *char_device;

int major;
int flag = 0;
char mes_buf[30];


int hello_open (struct inode *inode, struct file *file){
	pr_info("hello_open\n");
	return 0;
}

ssize_t hello_read(struct file *file, char __user *buf, size_t size, loff_t * loff_t){
	if(file->f_flags & O_NONBLOCK){
		if(flag == 0)
		return -EAGAIN;
	}
  /*进入休眠状态*/
	wait_event_interruptible(read_wq, flag);

	if(copy_to_user(buf, mes_buf, strlen(mes_buf)) != 0)
		return -EFAULT;
	return strlen(mes_buf);

}
ssize_t hello_write (struct file *file, const char __user *buf, size_t size, loff_t *loff_t){
	if(copy_from_user(mes_buf, buf, size) != 0){
		printk(KERN_INFO "hello_write: copy_from_user error\n");
		return -EFAULT;
	}

	printk(KERN_INFO "hello_write: %s\n", mes_buf);
   /*设置条件后,唤醒休眠任务*/
			flag = 1;
     
		wake_up_interruptible(&read_wq);

		return size;
	

}
int hello_close(struct inode *inode, struct file *file){

	printk(KERN_INFO "hello_close\n");

	return 0;

}



static struct file_operations file_fops = {
	.owner = THIS_MODULE,
	.open = hello_open,
	.release = hello_close,
	.read = hello_read,
	.write = hello_write,


};

static int __init hello_init(void){ 
    major = register_chrdev(0, DEVICE_NAME, &file_fops);
    if(major < 0){
        printk("key_irq_init register_chrdev failed\n");
        return major;
    }
    char_class = class_create(THIS_MODULE, CLASS_NAME);
    if(IS_ERR(char_class)){
        unregister_chrdev(major, DEVICE_NAME);
        printk("key_irq_init class_create failed\n");
        return PTR_ERR(char_class);
    }
    char_device = device_create(char_class, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
    if(IS_ERR(char_device)){
        class_destroy(char_class);
        unregister_chrdev(major, DEVICE_NAME);
        printk("key_irq_init device_create failed\n");
        return PTR_ERR(char_device);
    }


    printk("key_irq_init success\n");
    return 0;

}
    


static void __exit hello_exit(void){
    unregister_chrdev(major, DEVICE_NAME);
    device_destroy(char_class, MKDEV(major, 0));
    class_destroy(char_class);
    printk("key_irq_exit success\n");
}



module_init(hello_init);	/*指定设备驱动入口函数*/
module_exit(hello_exit);	/*指定设备驱动出口函数*/
MODULE_LICENSE("GPL");


标签:struct,队列,class,char,include,等待,hello
From: https://www.cnblogs.com/zxz-FINE/p/17914053.html

相关文章

  • Netty使用CompletableFuture实现异步串行队列
    一、前言CompletableFuture是JDK1.8提供的一种更加强大的异步编程的api。它实现了Future接口,也就是Future的功能特性CompletableFuture也有。它也实现了CompletionStage接口,CompletionStage接口定义了任务编排的方法,执行某一阶段,可以向下执行后续阶段。CompletableFuture相比于Futu......
  • 08.强制等待与隐式等待
    为什么要添加等待避免页面未渲染完成后操作,导致的报错直接等待解决方案:在报错的元素操作之前添加等待原理:强制等待,线程休眠一定时间演练环境:雪球apptime.sleep(3)隐式等待问题:难以确定元素加载的具体等待时间。解决方案:针对于寻找元素的这个动作,使用隐式等待......
  • 数据结构 —— 线性表、栈、队列
    一、算法复杂度 【2011】设n是描述问题规模的非负整数,下面的程序片段时间复杂度是()x=2;while(x<n/2)x=2*x;AO(log2(n))  BO(n) CO(nlog2(n)) DO(n^2) 答案:A解析:x=2^i=n/2i=log2(n/2) 【2012】求整数n(n>=0)的阶乘的算法......
  • 消息队列
    首先使用消息队列前,我们需要知道,消息队列是用来发送、接收数据的一个容器,简单的说:我们在某宝上买东西,这中间有一个快递的过程,而大多数情况下,我本人选择将我买的东西寄到某个代收点,派送员只需要按照我的要求将东西放到代收点就可以了,之后我有时间了才自己去取。消息队列就类似于这......
  • 一文讲透消息队列RocketMQ实现消费幂等
    这篇文章,我们聊聊消息队列中非常重要的最佳实践之一:消费幂等。1基础概念消费幂等是指:当出现RocketMQ消费者对某条消息重复消费的情况时,重复消费的结果与消费一次的结果是相同的,并且多次消费并未对业务系统产生任何负面影响。例如,在支付场景下,消费者消费扣款消息,对一笔订单......
  • 消息队列和事件循环
    每个渲染进程都有一个主线程,并且主线程非常繁忙,既要处理DOM,又要计算样式,还要处理布局,同时还需要处理JavaScript任务以及各种输入事件。要让这么多不同类型的任务在主线程中有条不紊地执行,这就需要一个系统来统筹调度这些任务,这个统筹调度系统就是消息队列和事件循环系统。但并不......
  • 【合并排序链表】分治/优先队列
    合并两个排序链表模拟维护一个合并链表,每次添加两个排序链表中较小val的节点即可模拟代码publicListNodemergeTwo(ListNodea,ListNodeb){if(a==null)returnb;if(b==null)returna;ListNodeans=newListNode(0);Lis......
  • 记录rabbitMQ的广播队列的错误使用导致未能正确广播的问题
    背景说明:有3个服务S1、S2、S3现在服务S1需要发布消息到广播交换机E,并建立了两个普通队列Q1,Q2,将其绑定到广播交换机E上服务S2和服务S3同时监听队列Q1,Q2本意是,服务S1通过广播交换机E把消息同时推送给服务S2和S3后面测试时,同事发现,服务S2和服务S3都只接收到了部分消息,而不是全......
  • 清空ActiveMQ中的Scheduled延时队列
    要清空ActiveMQ中的Scheduled延时队列,可以执行以下步骤:停止ActiveMQ服务器。在ActiveMQ数据存储目录中找到存储延时消息的目录。该目录的默认位置是<activemq_home>/data/localhost/Scheduled.删除该目录下的所有文件,这将清空延时队列中的消息。启动ActiveMQ服务器。请注意......
  • Java中的消息队列(MQ)应用实践
    摘要:本文将介绍Java中消息队列(MQ)的概念、应用场景以及如何使用Java中的消息队列进行实践。我们将探讨如何使用Java消息队列实现异步通信、解耦和流量削峰等常见需求,并通过实际案例展示其应用。一、引言在分布式系统中,消息队列(MQ)是一种常见的中间件技术,用于实现异步通信和解耦。通过......