-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkthread_mod.c
More file actions
40 lines (32 loc) · 804 Bytes
/
kthread_mod.c
File metadata and controls
40 lines (32 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/module.h>
static struct task_struct *ktask;
static int print_messages(void *data)
{
while (1) {
if (kthread_should_stop())
return 0;
pr_info("Printing something, and sleeping for 1 second...\n");
ssleep(1);
}
return 0;
}
static int kthread_mod_init(void)
{
ktask = kthread_run(print_messages, 0, "print-messages");
if (IS_ERR(ktask)) {
pr_err("Error while starting print-messages kthread: %ld\n", PTR_ERR(ktask));
return PTR_ERR(ktask);
}
return 0;
}
static void kthread_mod_exit(void)
{
pr_info("Stopping the print-messages kthread...\n");
kthread_stop(ktask);
}
module_init(kthread_mod_init);
module_exit(kthread_mod_exit);
MODULE_DESCRIPTION("Example of kthread and sleeping");
MODULE_LICENSE("GPLv2");