Linux内核线程,定时器,工作队列编程实例:
实例大概过程如下:
一.初始化
1.创建一个工作队列test_wq
2.创建一个定时器timer
3.创建一个内核线程
二.工作过程
1.定时器每隔500ms调用do_htimer
2.定时器触发工作队列调用do_work
3.工作队列触发睡眠的内核线程(唤醒睡眠线程)
三.结束
1.停止内核线程
2.取消定时器
3.停止工作队列工作
4.关闭工作队列
###############内核模块代码##############
#include <linux/init.h>
#include <linux/workqueue.h>
#include <linux/hrtimer.h>
#include <linux/module.h>
#include <linux/kthread.h>
//#define THREAD
#define MS 5000L
#define NS MS*1E6L
static struct test_wq {
#ifdef THREAD
struct workqueue_struct *wq;
#endif
struct work_struct w;
struct hrtimer timer;
int flag;
struct task_struct *thread;
wait_queue_head_t wqh;
} test_wq;
static void do_work(struct work_
实例大概过程如下:
一.初始化
1.创建一个工作队列test_wq
2.创建一个定时器timer
3.创建一个内核线程
二.工作过程
1.定时器每隔500ms调用do_htimer
2.定时器触发工作队列调用do_work
3.工作队列触发睡眠的内核线程(唤醒睡眠线程)
三.结束
1.停止内核线程
2.取消定时器
3.停止工作队列工作
4.关闭工作队列
###############内核模块代码##############
#include <linux/init.h>
#include <linux/workqueue.h>
#include <linux/hrtimer.h>
#include <linux/module.h>
#include <linux/kthread.h>
//#define THREAD
#define MS 5000L
#define NS MS*1E6L
static struct test_wq {
#ifdef THREAD
struct workqueue_struct *wq;
#endif
struct work_struct w;
struct hrtimer timer;
int flag;
struct task_struct *thread;
wait_queue_head_t wqh;
} test_wq;
static void do_work(struct work_