From b68f8c769c688b182fc112f1e7c20fcb56a78eb7 Mon Sep 17 00:00:00 2001 From: Arturs Artamonovs Date: Mon, 4 Dec 2023 22:37:17 +0000 Subject: Kernel thread creation process --- md/notes/kernel/create_thread.md | 139 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) (limited to 'md/notes/kernel') 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 /* Needed by all modules */ +#include + +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 + +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 /* Needed by all modules */ +#include +#include +#include +#include +#include +#include +#include + + +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 -- cgit v1.2.3