Hi, I will try to test how to create kernel threads and have write a kernel module which creates a number of kernel threads running the same function. But the results is somewhat confusing. #include <linux/kernel.h> #include <linux/kthread.h> #include <linux/delay.h> #define MAX_KTHREAD 2 struct task_struct *ktask[MAX_KTHREAD]; static int my_kthread(void *data) { int nr = *(int *)data; while (!kthread_should_stop()){ ssleep(1); printk(KERN_ALERT "This is mythread[%d].\n", nr); } return 0; } static int kthread_init(void) { int i; for (i = 0; i < MAX_KTHREAD; i++){ ktask[i] = kthread_run(my_kthread, &i, "mythread[%d]", i); } return 0; } static void kthread_exit(void) { int i; for (i = 0; i < MAX_KTHREAD; i++){ if (ktask[i]){ kthread_stop(ktask[i]); ktask[i] = NULL; } } } module_init(kthread_init); module_exit(kthread_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Shakespeare"); MODULE_DESCRIPTION("This is a test program of kthread."); The messages on the screen are: This is mythread[-929820448]. This is mythread[1]. This is mythread[-929820448]. This is mythread[1]. This is mythread[-929820448]. ... ... ... I wonder why the first thread's number is not zero rather than -929820448. Furthermore, when running again with MAX_KTHREAD == 3, the messages are: This is mythread[1]. This is mythread[2]. This is mythread[1]. This is mythread[1]. This is mythread[2]. This is mythread[1]. This is mythread[1]. This is mythread[2]. ... ... ... There should be 3 threads running, but only two of them appear. The first thread get lost.
On 2011-09-15 22:20:03 (+0800), Parmenides <mobile.parmenides@gmail.com> wrote:
I will try to test how to create kernel threads and have write a kernel module which creates a number of kernel threads running the same function. But the results is somewhat confusing.
static int kthread_init(void) { int i;
for (i = 0; i < MAX_KTHREAD; i++){ ktask[i] = kthread_run(my_kthread, &i, "mythread[%d]", i); } return 0; }
You're passing the address of a stack variable (i) as data pointer. That's not right, because as soon as you exit the kthread_init function the data may be overwritten. The fact that it works in some cases is pure coincidence. Regards, Kristof
On Thu, Sep 15, 2011 at 7:59 PM, Kristof Provost <kristof@sigsegv.be> wrote:
On 2011-09-15 22:20:03 (+0800), Parmenides <mobile.parmenides@gmail.com> wrote:
I will try to test how to create kernel threads and have write a> }
<snip>
You're passing the address of a stack variable (i) as data pointer. That's not right, because as soon as you exit the kthread_init function the data may be overwritten.
Also, even if you declare i as global to solve this issue, you would not get what you expect because all the threads you create get the _same_ address pointing to the same variable i. This variable will keep on incrementing in the background till all your threads are created and thus your threads will print the current value of i which is not what you expect. You can pass the variable i as a _value_ directly instead of passing a pointer to solve this problem. Kaustubh
participants (3)
-
Kaustubh Ashtekar -
Kristof Provost -
Parmenides