I am new to kernel module programming and for my work I need to write a multithreaded kernel module. So I tried some primary uses of kernel threads. I wrote the following. It is supposed to print 1 in one thread and 2 in another thread, both 10 times.
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/udp.h>
#include <linux/mm.h>
#include <linux/init.h>
#include <linux/kthread.h>
struct task_struct *task1;
struct task_struct *task2;
static void thread_func(void* data)
{
int *n;
n = (int *)data;
int i = 0;
while(i < 10){
printk("%d\n", *n);
i++;
}
//do_exit();
}
static int t_start(void)
{
printk("Module starting ... ... ..\n");
int *p1, *p2;
int one = 1, two = 2;
p1 = &one;
p2 = &two;
task1 = kthread_run(&thread_func, (void*)p1, "thread_func_1");
task2 = kthread_run(&thread_func, (void*)p2, "thread_func_2");
return 0;
}
static void t_end (void)
{
printk("Module terminating ... ... ...\n");
kthread_stop(task1);
kthread_stop(task2);
}
module_init(t_start);
module_exit(t_end);
MODULE_AUTHOR("Md. Taufique Hussain");
MODULE_DESCRIPTION("Testing kernel threads");
MODULE_LICENSE("GPL");
But I am facing following problems. -
- First thread is printing all ten 1s and then second thread is executing. I wanted to run these two in interleaved manner.
- First thread is printing all 1s OK but second is not printing 2s. It is printing 0s. Probably parameter is not passing to second thread properly.
- When I am inserting module it is running but when I am removing module the machine hangs
What are the problems? How can I solve them.