1. 工作队列是一种将任务推后执行的方式,它把推后的任务交由一个内核线程去执行。这样中断的下半部会在进程上下文执行,他允许重新调度甚至睡眠。每个被推后的任务叫做“工作”,由这些工作组成的队列称为工作队列。

2. Linux内核使用struct workqueue_struct来描述一个工作队列

struct workqueue_struct {
struct cpu_workqueue_struct *cpu_wq;
struct list_head list;
const char *name;
int singlethread;
int freezeable; /* Freeze threads during suspend */
int rt;
};

3. Linux内核使用struct work_struct来描述一个工作项

struct work_struct {
atomic_long_t data;
struct list_head entry;
work_func_t func;
};

  其中,work_func_t原型为

typedef void (*work_func_t)(struct work_struct *work);

4. 在驱动程序中使用工作队列有两种方式

(1)共享工作队列:

  在Linux系统中,内核为了方便用户编程,已经默认实现了一个所有进程都可以使用的工作队列(其对应的内核线程是kevent线程,其在Linux启动时创建,该线程被创建之后就处于sleep状态,当我们使用schedule_work函数时,才会唤醒该线程。当工作队列上的所有节点被执行完毕,该线程又会处于休眠状态,直到schedule_work再次被调用)。

① 编写自己的work_struct工作函数

② 定义自己的work_struct结构体

③ 初始化work_struct结构体,把工作函数地址赋值给work_struct->func

③在适当位置使用schedule_work函数完成向系统工作队列添加自己的work_struct

(2)自定义工作队列

①创建工作队列:create_workqueue

#define create_workqueue(name) __create_workqueue((name), 0, 0, 0)

②创建工作:INIT_WORK

#define INIT_WORK(_work, _func)                        \
do { \
static struct lock_class_key __key; \
\
(_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, );\
INIT_LIST_HEAD(&(_work)->entry); \
PREPARE_WORK((_work), (_func)); \
} while ()

③提交工作:queue_work

int queue_work(struct workqueue_struct *wq, struct work_struct *work)
{
int ret; ret = queue_work_on(get_cpu(), wq, work);
put_cpu(); return ret;
}

5. 简单示例:

① 共享工作队列

#include <linux/init.h>
#include <linux/module.h> struct workqueue_struct *my_wq;
struct work_struct *work1;
struct work_struct *work2; MODULE_LICENSE("GPL"); void work1_func(struct work_struct *work)
{
printk("this is work1->\n");
} void work2_func(struct work_struct *work)
{
printk("this is work2->\n");
} int init_que(void)
{
//1. 创建工作1
work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work1, work1_func); //2. 挂载(提交)工作1
schedule_work(work1); //3. 创建工作2
work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work2, work2_func); //4. 挂载(提交)工作2
schedule_work(work2); return ;
} void clean_que()
{ } module_init(init_que);
module_exit(clean_que);

②自定义工作队列

#include <linux/init.h>
#include <linux/module.h> struct workqueue_struct *my_wq;
struct work_struct *work1;
struct work_struct *work2; MODULE_LICENSE("GPL"); void work1_func(struct work_struct *work)
{
printk("This is work1->\n");
} void work2_func(struct work_struct *work)
{
printk("This is work2->\n");
} int init_que(void)
{
//1. 创建工作队列
my_wq = create_workqueue("my_que"); //2. 创建工作
work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work1, work1_func); //3. 挂载(提交)工作
queue_work(my_wq,work1); //2. 创建工作
work2 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
INIT_WORK(work2, work2_func); //3. 挂载(提交)工作
queue_work(my_wq,work2); return ;
} void clean_que()
{ } module_init(init_que);
module_exit(clean_que);

注:事实上,printk函数是不可重入函数,需要加锁机制来保护。

Linux中断分层--工作队列的更多相关文章

  1. Linux中断分层--软中断和tasklet

    1. Linux中断分层 (1)上半部:当中断发生时,它进行相应的硬件读写,并“登记”该中断.通常由中断处理程序充当上半部.(一般情况下,上半部不可被打断) (2)下半部:在系统空闲的时候,对上半部“ ...

  2. Linux中断分层技术

    一.中断嵌套  当系统正在执行某中断处理函数时,又产生了一个新的中断,这就叫做中断嵌套.当中断为慢速中断时,新的中断会取代当前中断,即当前中断没有执行完就结束 了:当中断为快速中断时,新的终端就不会产 ...

  3. Linux设备驱动之中断支持及中断分层

    快速中断:在开启快速中断时,其他中断不会打断快速中断. 多个中断共享一个中断号. 中断行为受到限制: 1.不能使用可能引起阻塞的函数 2.不能使用可能引起调度的函数 中断注册:request_irq( ...

  4. Linux中断管理 (3)workqueue工作队列

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  5. Linux中断管理 (3)workqueue工作队列【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8659988.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...

  6. Linux中断管理 (1)Linux中断管理机制

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  7. Linux中断管理 (1)Linux中断管理机制【转】

    转自:https://www.cnblogs.com/arnoldlu/p/8659981.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...

  8. linux中断与异常

    看了<深入理解linux内核>的中断与异常,简单总结了下,如果有错误,望指正! 一 什么是中断和异常 异常又叫同步中断,是当指令执行时由cpu控制单元产生的,之所以称之为异常,是因为只有在 ...

  9. 驱动: 中断【1】linux中断流程

    通常情况下,当一个给定的中断处理程序正在执行时,所有其他的中断都是打开的,所以这些不同中断线上的其他中断都能被处理,但当前中断总是被禁止的. 将中断处理切为两个部分或两半.中断处理程序上半部(top ...

随机推荐

  1. Django框架 之 Auth用户认证

    Django框架 之 Auth用户认证 浏览目录 auth模块 user对象 一.auth模块 1 from django.contrib import auth django.contrib.aut ...

  2. javascript总结17:javascript 函数简介

    1 释义:函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 2 格式:通过 function  关键字. function test(){ alert("您好"); } ...

  3. Java 可变字符串StringBuilder/StringBuffer的区别

    public class StringBuilder_and_StringBuffer { private static long SystemTime(){ return System.curren ...

  4. svm的第一个实例

    用的数据集是uci机器学习库的数据 ‘iris.data’ from sklearn import svm import csv from sklearn.model_selection import ...

  5. Activity和Fragment的生命周期

  6. C# File类常用方法

    File 类 提供用于创建.复制.删除.移动和打开文件的静态方法,并协助创建 FileStream 对象. 1. File.Exists ——  确定指定的文件是否存在. public static ...

  7. C# 读取Text文本,写入Text文本

    //读取 private void showMess() { this.dataGridViewX2.Rows.Clear(); //将车辆信息一行行添加到datagreatview 里面 Strea ...

  8. 今天遇到的传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确的解决方案

    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确.参数 3 ("@UserName"): 数据类型 0xE7 的数据长度或元数据长度无效. 今天在做数据同步的时候遇 ...

  9. ARKit入门

    ARKit介绍 ARKit是iOS11引入的一个全新的框架,使用Visual Inertial Odometry(VIO,视觉惯性里程计)来精确跟踪现实世界中的真实场景.相比其它设备平台,ARKit中 ...

  10. session相关

    判断session是否已失效: HttpSession session=request.getSession(false); getSession(boolean)相比于getSession()更安全 ...