Linux-driven core multithreading (ii)

Source: Internet
Author: User

This digest from http://www.cnblogs.com/zhuyp1015/archive/2012/06/11/2545702.html

Core multithreading is used in the project, I am not familiar with, encountered a very embarrassing problem, causing the CPU to run 100%.

This is the first kernel-threaded thread to write, with global variables that enable communication between two kernel threads. But here comes the fatal error: whenever Wait_event_interruptible () is awakened by wake_up_interruptible, the thread enters the dead loop. The later discovery is that the thread does not voluntarily dispatch itself, it needs to be explicitly dispatched by schedule or schedule_timeout (). If you do not add TC = 0, wait_event_intrruptible () will not sleep (see the previous article " Waiting Queue "), will not be dispatched to abandon the CPU, so into the dead loop. This process can be seen by analyzing the source code of the Wait_event_intrruptible ().

#include <linux/init.h>#include<linux/module.h>#include<linux/kthread.h>#include<linux/wait.h>Module_license ("Dual BSD/GPL"); Static structTask_struct *_tsk; Static structTask_struct *_tsk1;Static intTC =0;Staticwait_queue_head_t Log_wait_queue; Static intThread_function (void*data) {       Do{PRINTK (kern_info"In thread_function thread_function:%d times \ n", TC); Wait_event_interruptible (LOG_WAIT_QUEUE,TC==Ten); TC=0;///This line must be added for the kernel to dispatch. Kernel threads do not have to be actively dispatched like applications, we need to use the dispatch function explicitly, it is not possible to reset the TC value in Thread_function_1, because the thread will not be dispatched, the thread is always consuming CPUPRINTK (Kern_info"Have been woke up!\n"); } while(!kthread_should_stop ()); returnTC; }  Static intThread_function_1 (void*data) {       Do{PRINTK (kern_info"In thread_function_1 thread_function:%d times\n", ++TC); if(TC = =Ten&& waitqueue_active (&log_wait_queue)) {wake_up_interruptible (&log_wait_queue); } msleep_interruptible ( +); } while(!kthread_should_stop ()); returnTC; }    Static intHello_init (void) {PRINTK (Kern_info"Hello, world!\n."); Init_waitqueue_head (&log_wait_queue); _tsk= Kthread_run (Thread_function, NULL,"Mythread"); if(Is_err (_tsk)) {//you need to use Is_err () to determine if the thread is valid, and then an article introduce Is_err ()PRINTK (Kern_info"First create Kthread failed!\n"); }      Else{PRINTK (kern_info"First create Ktrhead ok!\n"); } _tsk1= Kthread_run (Thread_function_1,null,"mythread2"); if(Is_err (_tsk1)) {PRINTK (Kern_info"Second Create Kthread failed!\n"); }      Else{PRINTK (kern_info"Second Create Ktrhead ok!\n"); }      return 0; }    Static voidHello_exit (void) {PRINTK (Kern_info"Hello, exit!\n."); if(!Is_err (_tsk)) {          intRET =kthread_stop (_tsk); PRINTK (Kern_info"First thread function has stopped, return%d\n", ret); }      if(!Is_err (_TSK1)) {                   intRET =kthread_stop (_TSK1); PRINTK (Kern_info"Second thread function_1 has stopped, return%d\n", ret);  }} module_init (Hello_init); Module_exit (hello_exit);

Description: The purpose of this program is to use one thread (thread_function_1) to notify another thread (thread_function) that a condition (TC = = 10) is satisfied (for example, the receiving thread receives 10 frames and then notifies the processing thread to process the received data)

Operation Result:

The program loads and runs (the TC has a value equal to 10 and then wakes up another thread, and then the TC starts counting from 10)

Program Uninstall (program Uninstall in fact, it is very important to note that many programs in the uninstallation of the time back to the various issues will be mentioned later in the article):

Linux-driven core multithreading (ii)

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.