summaryrefslogtreecommitdiffstats
path: root/md/notes/kernel/create_thread.md
blob: 7f38c7ca18d61b35ee1919a482cff60575d223d6 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
title:Create threads
keywords:kernel,linux,threads

# Create threads

## Starting point

__Makefile__
```Makefile
obj-m += create_thread.o

KDIR ?= /lib/modules/$(shell uname -r)/build

all:
	make -C $(KDIR) M=$(PWD) modules

clean:
	make -C $(KDIR) M=$(PWD) clean
```

__create_thread.c__
```c
#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>

int create_thread_init( void )
{
	printk(KERN_DEBUG "Hello Thread!\n");
	return 0;
}

void create_thread_exit( void )
{
	printk(KERN_DEBUG "Exit Thread!\n");
}

module_init( create_thread_init );
module_exit( create_thread_exit );

MODULE_LICENSE("GPL");
```

## Create empty thread

Lets create kernel thread that will start and exit straight away


```c
#include <linux/delay.h>

static struct task_struct *task;

static int run_task(void *data) {
	int i=0;
	while(!kthread_should_stop())
	{
		pr_info("run task%d\n", i++);
		msleep(1000);
	} 
	return 0; 
}

int create_thread_init( void ) {
	...
	task = kthread_create(run_task, NULL, "special task");
	if(task) 
	{
		wake_up_process(task);
	} else {
		printk(KERN_ERR "Cannot create kthread\n"); 
	}
	...
}

```

## Kernel threads states
State of kernel thread  - include/linux/sched.h

```c
/* Used in tsk->state: */
#define TASK_RUNNING			0x00000000
#define TASK_INTERRUPTIBLE		0x00000001
#define TASK_UNINTERRUPTIBLE		0x00000002
#define __TASK_STOPPED			0x00000004
#define __TASK_TRACED			0x00000008
/* Used in tsk->exit_state: */
#define EXIT_DEAD			0x00000010
#define EXIT_ZOMBIE			0x00000020
#define EXIT_TRACE			(EXIT_ZOMBIE | EXIT_DEAD)
/* Used in tsk->state again: */
#define TASK_PARKED			0x00000040
#define TASK_DEAD			0x00000080
#define TASK_WAKEKILL			0x00000100
#define TASK_WAKING			0x00000200
#define TASK_NOLOAD			0x00000400
#define TASK_NEW			0x00000800
#define TASK_RTLOCK_WAIT		0x00001000
#define TASK_FREEZABLE			0x00002000
#define __TASK_FREEZABLE_UNSAFE	       (0x00004000 * IS_ENABLED(CONFIG_LOCKDEP))
#define TASK_FROZEN			0x00008000
#define TASK_STATE_MAX			0x00010000

#define TASK_ANY			(TASK_STATE_MAX-1)
```


## List of kernel thread functions used

| Function name | Note |
|---|---|
|  |  |
| wake_up_proces |  |
| kthread_create |  |



## Final result

__create_thread.c__
```c
#include <linux/module.h>  /* Needed by all modules */
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/err.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <linux/delay.h>


static struct task_struct *task;

static int run_task(void *data) {
	int i=0;
	while(!kthread_should_stop())
	{
		pr_info("run task %d\n", i++);
		msleep(1000);
	} 
	return 0; 
}

int create_thread_init( void )
{
	printk(KERN_DEBUG "Hello Thread!\n");

	task = kthread_create(run_task, NULL, "special task");
	if(task) 
	{
		wake_up_process(task);
	} else {
		printk(KERN_ERR "Cannot create kthread\n"); 
	}

	return 0;
}

void create_thread_exit( void )
{
	printk(KERN_DEBUG "Exit Thread!\n");
}

module_init( create_thread_init );
module_exit( create_thread_exit );

MODULE_LICENSE("GPL");
```

## Links


https://www.linuxtoday.com/blog/kernel-threads/  
http://www.embeddedlinux.org.cn/EssentialLinuxDeviceDrivers/final/ch03lev1sec1.html  
https://embetronicx.com/tutorials/linux/device-drivers/linux-device-drivers-tutorial-kernel-thread  
https://elixir.bootlin.com/linux/v6.1.21/source/include/linux/sched.h  
https://elixir.bootlin.com/linux/v6.1.21/source/kernel/sysctl.c  
https://girishjoshi.io/post/creating-linux-kernel-threads/  
https://tuxthink.blogspot.com/2011/02/kernel-thread-creation-1.html  
https://elixir.bootlin.com/linux/v6.1.21/source/include/linux/kthread.h#L27