Skip to content

Instantly share code, notes, and snippets.

@nnathan
Created April 2, 2017 22:34
Show Gist options
  • Save nnathan/cda8a8bf4aaa89be95e5b41d870cfad7 to your computer and use it in GitHub Desktop.
Save nnathan/cda8a8bf4aaa89be95e5b41d870cfad7 to your computer and use it in GitHub Desktop.
Playing with workqueues in the kernel
#include <linux/init.h>
#include <linux/module.h>
#include <linux/workqueue.h>
#include <linux/proc_fs.h>
/* proc shit */
#define PROCFS_NAME "hellowq"
static struct proc_dir_entry *hellowq_file;
static struct file_operations hellowq_proc_ops;
static char hellowq_buf[1024];
/* wq shit */
static struct workqueue_struct *wq;
static struct job {
int nsecs;
struct work_struct work;
} job;
static ssize_t hellowq_read(struct file *fp, char __user *buf, size_t n, loff_t off) {
pr_info("jobqueue: read called on proc\n");
return 0;
}
static int hellowq_write(struct file *file, const char *buf, unsigned long n, void *data) {
pr_info("jobqueue: write called on proc\n");
return 0;
}
static int __init mod_init(void)
{
pr_info("jobqueue: init\n");
wq = alloc_workqueue(PROCFS_NAME, WQ_UNBOUND | WQ_FREEZABLE, WQ_DFL_ACTIVE);
if (!wq) {
pr_info("jobqueue: failed to allocate workqueue");
return -1;
}
hellowq_proc_ops.read = hellowq_read;
hellowq_file = proc_create_data(PROCFS_NAME, 0666, NULL, &hellowq_proc_ops, NULL);
if (!hellowq_file) {
pr_info("jobqueue: failed to create proc entry");
destroy_workqueue(wq);
return -ENOMEM;
}
hellowq_file->data = hellowq_buf;
hellowq_file->read_proc = hellowq_read;
hellowq_file->write_proc = hellowq_write;
pr_info("jobqueue: loaded\n");
return 0;
}
static void __exit mod_exit(void)
{
remove_proc_entry(PROCFS_NAME, &proc_root);
destroy_workqueue(wq);
pr_info("jobqueue: exit\n");
}
module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("GPL v2");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment