Skip to content

Instantly share code, notes, and snippets.

@yagihiro yagihiro/sample.c
Last active Jan 2, 2018

What would you like to do?
workqueue sample on linux kernel
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/workqueue.h>
static void mykmod_work_handler(struct work_struct *w);
static struct workqueue_struct *wq = 0;
static DECLARE_DELAYED_WORK(mykmod_work, mykmod_work_handler);
static unsigned long onesec;
static void
mykmod_work_handler(struct work_struct *w)
pr_info("mykmod work %u jiffies\n", (unsigned)onesec);
static int __devinit mykmod_init(void)
onesec = msecs_to_jiffies(1000);
pr_info("mykmod loaded %u jiffies\n", (unsigned)onesec);
if (!wq)
wq = create_singlethread_workqueue("mykmod");
if (wq)
queue_delayed_work(wq, &mykmod_work, onesec);
return 0;
static void __devexit mykmod_exit(void)
if (wq)
pr_info("mykmod exit\n");

This comment has been minimized.

Copy link

commented Nov 15, 2013

Don't forget to cancel the possible work in the queue (cancel_delayed_work_sync) when you close the workqueue, otherwise your kernel will crash.


This comment has been minimized.

Copy link

commented Apr 7, 2014

You are a lifesaver, arxchruncher, I had spent an hour trying to figure out why my kernel crashed.


This comment has been minimized.


This comment has been minimized.

Copy link
Owner Author

commented May 17, 2017

Great work! @cirosantilli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.