Compare commits
2 Commits
02-KernelM
...
02-KernelM
| Author | SHA1 | Date | |
|---|---|---|---|
|
8fff875529
|
|||
|
e4089d2e05
|
@@ -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
|
||||
//-------------------
|
||||
|
||||
@@ -49,7 +49,9 @@ void dynAlloc_exit(void) {
|
||||
e = list_entry(list_unique_elements.next, struct element, node);
|
||||
pr_info ("delete element %d: %s\n", e->unique_number, e->text);
|
||||
list_del(&e->node);
|
||||
kfree(e);
|
||||
if (e != 0) {
|
||||
kfree(e);
|
||||
}
|
||||
}
|
||||
|
||||
pr_info("Memory allocated for dynamic allocation and linked list freed\n");
|
||||
|
||||
42
src/01-skeleton/s02e06-thread.c
Normal file
42
src/01-skeleton/s02e06-thread.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <linux/module.h> // needed by all modules
|
||||
#include <linux/init.h> // needed for macros
|
||||
#include <linux/kernel.h> // needed for debugging
|
||||
|
||||
|
||||
#include <linux/kthread.h>
|
||||
#include <linux/delay.h>
|
||||
|
||||
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");
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user