-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathkthread_module.c
67 lines (50 loc) · 1.24 KB
/
kthread_module.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#define N_THR 4
static struct task_struct *threads[N_THR];
DEFINE_SPINLOCK(critical_section);
static int thread_fn(void *ptr)
{
while (!kthread_should_stop()) {
pr_info("Kthread example: in thread %d\n", (int)ptr);
spin_lock(&critical_section);
msleep_interruptible(200);
spin_unlock(&critical_section);
msleep_interruptible(800);
}
return 0;
}
int thread_init(void)
{
char thread_format[] = "thread %d";
int i;
pr_info("Kthread example: in init\n");
for (i = 0; i < N_THR; i++)
threads[i] = kthread_run(thread_fn,
(void *)i, thread_format, i);
return 0;
}
void thread_cleanup(void)
{
int ret = 1;
int i;
for (i = 0; i < N_THR; i++) {
if (threads[i]) {
ret = kthread_stop(threads[i]);
if (!ret)
pr_info("Kthread example: thread %d stopped\n", i);
}
}
}
module_init(thread_init);
module_exit(thread_cleanup);
MODULE_AUTHOR("Vadym Mishchuk");
MODULE_DESCRIPTION("Kthread example module");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");