summaryrefslogtreecommitdiff
path: root/md/notes
diff options
context:
space:
mode:
Diffstat (limited to 'md/notes')
-rw-r--r--md/notes/kernel/create_thread.md139
1 files changed, 139 insertions, 0 deletions
diff --git a/md/notes/kernel/create_thread.md b/md/notes/kernel/create_thread.md
index 87830b1..7f38c7c 100644
--- a/md/notes/kernel/create_thread.md
+++ b/md/notes/kernel/create_thread.md
@@ -3,6 +3,78 @@ 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
@@ -32,13 +104,80 @@ State of kernel thread - include/linux/sched.h
#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