linux设备驱动之工作队列学习(workqueue)
来源:互联网 发布:薛飞杜宪事件 知乎 编辑:程序博客网 时间:2024/06/13 12:07
工作的定义
在线程函数内设置了测试点如下
如果设置了PF_NOFREEZE这个flag,那么系统挂起时候这个进程不会被挂起。
主要函数
#define create_workqueue(name) __create_workqueue((name), 0, 0)
#define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
#define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
以上三个宏调用__create_workqueue函数定义
extern struct workqueue_struct *__create_workqueue(const char *name,int singlethread, int freezeable);
释放创建的工作列队资源
void destroy_workqueue(struct workqueue_struct *wq)
延时调用指定工作列队的工作
queue_delayed_work(struct workqueue_struct *wq,struct delay_struct *work, unsigned long delay)
取消指定工作列队的延时工作
cancel_delayed_work(struct delay_struct *work)
将工作加入工作列队进行调度
queue_work(struct workqueue_struct *wq, struct work_struct *work)
等待列队中的任务全部执行完毕。
void flush_workqueue(struct workqueue_struct *wq);
主要测试代码
void myfunc(struct work_struct*ws);
struct workqueue_struct *wqueue;
DECLARE_WORK(mywork,myfunc);
void myfunc(struct work_struct*ws)
{
}
在模块加载是执行
测试结果
main current->pid
myfunc 1 current->pid 1016
myfunc 2 current->pid 1016
myfunc 3 current->pid 1016
ps
....
1016 root
可见函数运行在pid为1016的进程中,ps查看进程名为myqueue/0.
实例说明:
1.使用自定义工作队列
//=========
#include>linux /kernel.h>
#include>linux/module.h>
#include>linux/proc_fs.h>
#include>linux/workqueue.h>
#include>linux/sched.h>
#include>linux/init.h>
#include>linux/interrupt.h>
#include>linux/delay.h>
struct workqueue_struct *test_wq;
struct delayed_work test_dwq;
void delay_func(struct work_struct *work);
void delay_func(struct work_struct *work)
{
}
static int __init example_init(void)
{
}
static void __exit example_exit(void)
{
}
module_init(example_init);
module_exit(example_exit);
MODULE_LICENSE("GPL");
运行结果:
kernel: Create Workqueue successful!
kernel: first ret=1!
kernel: Example:ret= 1,i=0
kernel: Example:ret= 1,i=1
kernel: Example:ret= 1,i=2
kernel: second ret=0!
kernel: Goodbay! ret=1
kernel: Create Workqueue successful!
说明将任务添加到工作队列后,如果工作队列还在执行该任务,则queue_delayed_work()返回1,否则返回0,如上实例所述;
主线程delay_wq将任务添加到工作队列后,使得工作队列在延迟delay后执行函数delay_func(),而delay_wq线程继续执行;
2.使用内核工作队列实例
#include>linux/module.h>
#include>linux/init.h>
#include>linux/kernel.h>
#include>linux/net.h>
#include>net/sock.h>
#include>linux/in.h>
#include>linux/types.h>
#include>linux/kthread.h>
#include>linux/wait.h>
#include>linux/skbuff.h>
#include>linux/string.h>
#include>asm-generic/unaligned.h>
#include>linux/sysctl.h>
#include>linux/netfilter.h>
#include>linux/netfilter_ipv4.h>
#include>asm/checksum.h>
#include>linux/ip.h>
#include>linux/workqueue.h>
#define
static
static
static
{
}
static
{
}
static
{
}
module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");
*********************************************************************************
以前内核里对这个函数是这样定义的 #define INIT_WORK(_work, _func, _data),可以理解为INIT_WORK会在你定义的_work工作队列里面增加一个工作任务,该任务就是_func。_func这个任务会需要一些数据作为参数,这个参数就是通过_data传递的。
- linux设备驱动之工作队列学习(workqueue)
- linux设备驱动下的工作队列(workqueue)
- 字符设备驱动之Buttons-中断上下部(工作队列-workqueue)
- linux内核知识之工作队列(workqueue)
- linux 工作队列(workqueue)
- linux 工作队列workqueue
- Linux设备驱动之工作队列
- linux工作队列 - workqueue总览
- linux驱动学习之工作队列使用
- linux驱动学习之工作队列机制
- [Linux API]linux 工作队列workqueue
- linux学习之workqueue
- Linux工作队列workqueue实现分析
- 如何使用Linux工作队列workqueue
- Linux工作队列workqueue实现分析
- Linux 设备驱动 Edition 3) ---- workqueue
- 设备驱动-------工作队列
- 工作队列:workqueue
- 过滤器
- jq实现div左右飞起折叠并且div可以拉升
- 设计模式学习之旅
- Android Jni使用案例
- 贾扬清:希望Caffe成为深度学习领域的Hadoop
- linux设备驱动之工作队列学习(workqueue)
- IntelliJ IDEA Play2.x 控制台乱码
- Spark在美团的实践
- 【学习笔记】配置C3P0连接池
- 轻量web服务器开发日记06-request结构体的实现
- sqlplus登录\连接命令、sqlplus命令的使用大全
- linux centos jdk安装
- SpringMVC中使用Interceptor拦截器详解
- 内存访问