Linux Kernel Multithreading

Source: Internet
Author: User

In driver development, several kernel threads are often started to execute some operations throughout the driver's lifecycle, such as the control thread of the USB driver, waiting for the SCSI command. If there is no command, you can sleep, if there is a command, wake up the thread and parse and execute related commands. There is also a scanning thread in the USB driver. If a new device is connected to the USB bus, the scanning process will be started and the CPU resources will sleep normally.

There are three common kernel thread creation methods: kernel_thread, kthread_create, and kthread_run. To use these functions or macros, You need to include the following header files:

# Include <Linux/sched. h> // wake_up_process ()

# Include <Linux/kthread. h> // kthread_ceate (), kthread_run ()

# Include <err. h> // is_err (), ptr_err ()

 

The kernel threads created by these methods must be able to discard CPU resources by themselves, that is, do not generate an endless loop and do not actively call the scheduel () function; otherwise, the CPU will remain busy, because the kernel process/thread cannot be preemptible, he must be able to give up resources on his own initiative, regardless of the way through.

1. header file

# Include <Linux/sched. h> // wake_up_process ()

# Include <Linux/kthread. h> // kthread_create (), kthread_run ()

# Include <err. h> // is_err (), ptr_err ()

2. Implementation

2.1 create thread

During module initialization, you can create threads. Use the following functions and macro definitions:

Struct task_struct * kthread_create (INT (* threadfn) (void * data ),

Void * data,

Const char namefmt [],...);

# Define kthread_run (threadfn, Data, namefmt ,...)\

({\

Struct task_struct * _ k \

= Kthread_create (threadfn, Data, namefmt, ##_ _ va_args __);\

If (! Is_err (_ k ))\

Wake_up_process (_ k );\

_ K ;\

})

For example:

Static struct task_struct * test_task;

Static int test_init_module (void)

{

Int err;

Test_task = kthread_create (test_thread, null, "test_task ");

If (is_err (test_task )){

Printk ("unable to start kernel thread. \ n ");

Err = ptr_err (test_task );

Test_task = NULL;

Return err;

}

Wake_up_process (test_task );
Return 0;
}

Module_init (test_init_module );

2.2 thread Functions

Complete the required business logic work in the thread function. The main framework is as follows:

Int threadfunc (void * Data ){

...

While (1 ){

Set_current_state (task_uninterruptible );

If (kthread_should_stop () break;

If () {// The condition is true

// Perform business processing

}

Else {// The condition is false.

// Let the CPU run other threads and be rescheduled within the specified time

Schedule_timeout (HZ );

}

}

...

Return 0;

}

2.3 end thread

When the module is detached, it can end the running of the thread. Use the following functions:

Int kthread_stop (struct task_struct * k );

For example:

Static void test_cleanup_module (void)

{

If (test_task ){

Kthread_stop (test_task );

Test_task = NULL;

}

}

Module_exit (test_cleanup_module );

3. Notes

(1) when calling the kthread_stop function, the thread function cannot have ended. Otherwise, the kthread_stop function will keep waiting.

(2) The thread function must allow the CPU to run other threads. At the same time, thread functions must be rescheduled. In the example program, this is done through the schedule_timeout () function.

4. Performance Testing

You can use the top command to view the CPU usage of threads (including kernel threads. The command is as follows:

Top-P thread number

Run the following command to find the thread Number:

PS aux | grep thread name


Run the following command to display all kernel threads:
PS afx


Note: The thread name is specified by the third parameter of the kthread_create function.

 

**************************************** **************************************** *********************

1. Use kthread_create to create a thread:
Struct task_struct * kthread_create (INT (* threadfn) (void * data ),

Void * data,
Const char * namefmt ,...);
This function can input a thread name in a certain format like printk.
After a thread is created, it does not run immediately. Instead, it needs to pass the task_struct pointer returned by kthread_create () to wake_up_process () and then run the thread through this function.
2. Of course, there is also a function to create and start a thread: kthread_run
Struct task_struct * kthread_run (INT (* threadfn) (void * data ),
Void * data,
Const char * namefmt ,...);
3. once started, the thread will continue to run unless the thread actively calls the do_exit function or other processes call the kthread_stop function to end the running of the thread.
Int kthread_stop (struct task_struct * thread );
Kthread_stop () sends a signal to the thread.
If a thread function is processing a very important task, it will not be interrupted. Of course, if a thread function never returns and does not check the signal, it will never stop.
Reference: Kernel threads made easy

 

Code

When kthread_stop is executed, the target thread must not exit; otherwise, oops will occur. The cause is easy to understand. When the target thread exits, its corresponding task structure becomes invalid. If kthread_stop references this invalid task structure, an error will occur.

To avoid this problem, make sure that the thread does not exit, as shown in the Code:

Thread_func ()

{

// Do your work here

// Wait to exit

While (! Thread_could_stop ())

{

Wait ();

}

}

Exit_code ()

{

Kthread_stop (_ task); // sends a signal to the task to notify it to exit

}

This kind of exit mechanism is gentle, and everything is under the control of thread_func (). When a thread exits, it can easily release resources, rather than being inexplicably "stabbed ".

 

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.