Files
linux-kernel-module-cheat/kernel_module/workqueue_cheat.c
2017-05-15 09:36:40 +01:00

45 lines
934 B
C

/*
Usage:
insmod /workqueue_cheat.ko
# dmesg => worker
rmmod workqueue_cheat
Creates a separate thread. So init_module can return, but some work will still get done.
Can't call this just workqueue.c because there is already a built-in with that name:
https://unix.stackexchange.com/questions/364956/how-can-insmod-fail-with-kernel-module-is-already-loaded-even-is-lsmod-does-not
Bibliography:
- https://www.ibm.com/developerworks/library/l-tasklets/
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
MODULE_LICENSE("GPL");
static struct workqueue_struct *queue;
static void work_func(struct work_struct *work)
{
printk(KERN_INFO "worker\n");
}
DECLARE_WORK(work, work_func);
int init_module(void)
{
queue = create_singlethread_workqueue("myworkqueue");
queue_work(queue, &work);
return 0;
}
void cleanup_module(void)
{
/* Waits for jobs to finish. */
destroy_workqueue(queue);
}