From 8fff875529ea821d06a24567e1cbd08f589255d5 Mon Sep 17 00:00:00 2001 From: Klagarge Date: Tue, 31 Mar 2026 21:06:44 +0200 Subject: [PATCH] feat(lab02): add ex06 --- doc/lab-01_02.typ | 2 ++ src/01-skeleton/s02e06-thread.c | 42 +++++++++++++++++++++++++++++++++ src/01-skeleton/skeleton.c | 11 +++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/01-skeleton/s02e06-thread.c diff --git a/doc/lab-01_02.typ b/doc/lab-01_02.typ index 9814459..5d156ac 100644 --- a/doc/lab-01_02.typ +++ b/doc/lab-01_02.typ @@ -369,6 +369,8 @@ registers[0] = ioremap(CHIP_ID_BASE_ADDR, 0x1000); ] ) +Easy exercice, a thread in the kernet is a `struct task_struct*` that can be created with `kthread_run` + //------------------- // Exercise 7: Sleeping //------------------- diff --git a/src/01-skeleton/s02e06-thread.c b/src/01-skeleton/s02e06-thread.c new file mode 100644 index 0000000..59082b5 --- /dev/null +++ b/src/01-skeleton/s02e06-thread.c @@ -0,0 +1,42 @@ +#include // needed by all modules +#include // needed for macros +#include // needed for debugging + + +#include +#include + +static struct task_struct* sample_thread; + +#define DELAY_S 5 + +int thread_skeletonThread (void* data) { + + pr_info("Thread started\n"); + + while (!kthread_should_stop()) { + pr_info("PING!\n"); + ssleep(DELAY_S); + } + + return 0; +} + +void thread_init(void) { + pr_info("Initialize kernel thread\n"); + + sample_thread = kthread_run(thread_skeletonThread, NULL, "The Machine that goes"); + if (IS_ERR(sample_thread)) { + pr_err("Failed to create kernel thread\n"); + return; + } + + pr_info("Kernel thread initialized\n"); + +} + +void thread_exit(void) { + pr_info("Exiting kernel thread\n"); + kthread_stop(sample_thread); + pr_info("Kernel thread exited\n"); +} diff --git a/src/01-skeleton/skeleton.c b/src/01-skeleton/skeleton.c index e2b690d..1346214 100644 --- a/src/01-skeleton/skeleton.c +++ b/src/01-skeleton/skeleton.c @@ -6,6 +6,7 @@ #include "s02e02-parameters.c" #include "s02e04-dynamic_allocation.c" #include "s02e05-io_memory_mapped.c" +#include "s02e06-thread.c" static int __init skeleton_init(void) { @@ -27,6 +28,11 @@ static int __init skeleton_init(void) { pr_info("--------------------\n"); + // Lab02 - Exercise 6: Kernel thread + thread_init(); + + pr_info("--------------------\n"); + pr_info("Linux module skeleton loaded\n"); return 0; } @@ -44,6 +50,11 @@ static void __exit skeleton_exit(void) { pr_info("--------------------\n"); + // Lab02 - Exercise 6: Kernel thread + thread_exit(); + + pr_info("--------------------\n"); + pr_info ("Linux module skeleton unloaded\n"); }