-
-
Save sandeepbhuyan/d847ce501ff026b80cdb0db698ddc25a to your computer and use it in GitHub Desktop.
a simple example of work_queue in Linux kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2015 Song Yang @ ittraining | |
* | |
* All rights reserved. | |
* This program is free to use, but the ban on selling behavior. | |
* Modify the program must keep all the original text description. | |
* | |
* 保留所有權利。 | |
* 本程式可任意使用,但是禁止販售行為。 | |
* 修改程式時必須保留所有原有文字說明。 | |
* | |
* Email: [email protected] | |
* Blog : http://blog.ittraining.com.tw | |
*******************************************************************************/ | |
#include <linux/module.h> | |
#include <linux/init.h> | |
#include <linux/interrupt.h> | |
MODULE_LICENSE("Dual BSD/GPL"); | |
static void my_tasklet_handler(unsigned long flag); | |
DECLARE_TASKLET(my_tasklet, my_tasklet_handler, 0); | |
static void my_tasklet_handler(unsigned long flag) | |
{ | |
tasklet_disable(&my_tasklet); | |
printk("my_tasklet run: do what the tasklet want to do....\n"); | |
tasklet_enable(&my_tasklet); | |
} | |
static int hello_tasklet_init(void) | |
{ | |
printk("module init start. \n"); | |
printk("Hello tasklet!\n"); | |
tasklet_schedule(&my_tasklet); | |
printk("module init end.\n"); | |
return 0; | |
} | |
static void hello_tasklet_exit(void) | |
{ | |
tasklet_kill(&my_tasklet); | |
printk("Goodbye, tasklet!\n"); | |
} | |
module_init(hello_tasklet_init); | |
module_exit(hello_tasklet_exit); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2015 Song Yang @ ittraining | |
* | |
* All rights reserved. | |
* This program is free to use, but the ban on selling behavior. | |
* Modify the program must keep all the original text description. | |
* | |
* 保留所有權利。 | |
* 本程式可任意使用,但是禁止販售行為。 | |
* 修改程式時必須保留所有原有文字說明。 | |
* | |
* Email: [email protected] | |
* Blog : http://blog.ittraining.com.tw | |
*******************************************************************************/ | |
#include <linux/init.h> | |
#include <linux/module.h> | |
#include <linux/interrupt.h> | |
#include <linux/delay.h> | |
MODULE_LICENSE("Dual BSD/GPL"); | |
struct work_struct workq; | |
void my_workqueue_handler(struct work_struct *work) | |
{ | |
printk("hello work queue: START my_workqueue_handler() \n"); | |
msleep(3000); /* sleep */ | |
printk("hello work queue: END my_workqueue_handler() \n"); | |
} | |
static int my_init(void) | |
{ | |
int ret = 0; | |
printk("hello work queue: START module_init() \n"); | |
INIT_WORK(&workq, my_workqueue_handler); | |
schedule_work(&workq); | |
printk("hello work queue: END module_init() \n"); | |
return 0; | |
} | |
static void my_exit(void) | |
{ | |
flush_scheduled_work(); | |
printk("my driver removed.\n"); | |
} | |
module_init(my_init); | |
module_exit(my_exit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment