How to create some threads running tha same function

Kristof Provost kristof at
Thu Sep 15 10:29:39 EDT 2011

On 2011-09-15 22:20:03 (+0800), Parmenides <mobile.parmenides at> 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.


More information about the Kernelnewbies mailing list