Description of several problems with the kernel_thread function in Linux Kernel

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.