1. is the kernel thread created only in kernel thred?
2. Cannot the kernel thread be killed?
3. Use the kernel_thread function of the kernel thread created in the module to obtain the current-> mm value. Why is it not 0?
4.
First look at a piece of code:
# Include <Linux/kernel. h>
# Include <Linux/module. h>
Static pid_t thread_pid;
Static struct completion thread_exited;
Static int Noop (void * dummy)
{
Daemonize ("mythread ");
Allow_signal (sigterm );
While (! Signal_pending (current )){
/* Do something else */
Schedule ();
}
Complete_and_exit (& thread_exited, 1 );
}
Static int test_init (void)
{
Init_completion (& thread_exited );
Thread_pid = kernel_thread (Noop, null, clone_kernel | sigchld );
Return 0;
}
Static void test_exit (void)
{
Kill_proc (thread_pid, sigterm, 1 );
Wait_for_completion (& thread_exited );
}
Module_init (test_init );
Module_exit (test_exit );
Answer:
1. As shown in the preceding example, the creation of the kernel thread does not have to be in the kernel thread.
2. After the module on insmod, killall mythread can certainly kill this kernel thread.
3. From the output of the printk function above, we can see that current-> MM is indeed 0 and there is no doubt.
But what is the reason why mm is not 0?
The daemonize () function is not called in the kernel thread. This function is named after the kernel thread and will be mounted to the kthread queue. When the output of MM is not 0, the output mm points to the insmod mm.
Summary:
Kernel_thread is created. In the execution function, you must use daemonize to release the resource and mount it to init.
Kthread_create is a positive creation function, which does not need to call daemonize. All created kernel threads are attached to the kthread.