Linux multi-thread programming and signal processing is easy to neglect.

Source: Internet
Author: User
Tags sigint signal
The Linux-based multi-thread programming and signal processing is an example that is easy to neglect-Linux general technology-Linux programming and kernel information. The following is a detailed description. Linux multi-thread programming and signal processing is easy to neglect.

In the past few days, a network traffic collector program has been basically changed. In the main function, several subthreads are started and then sleep for 10 minutes. Then, the sub-threads are cleared and exited. Now, if you want to change it to a subthread after it starts, the main thread enters infinite sleep until it receives the SIGTERM or SIGINT. The main program is as follows:
Other header files
# Include // Header file required for Signal Processing
Int main (int argc, char * argv []) {
// Other variable declarations
Sigset_t sig_set, sig_pending;


// Set signal blocking
Sigemptyset (& sig_set );
Sigaddset (& sig_set, SIGTERM );
Sigaddset (& sig_set, SIGINT );
Sigprocmask (SIG_BLOCK, & sig_set, NULL );


Start several subthreads
...........

// Set signal blocking
Sigemptyset (& sig_set );
Sigaddset (& sig_set, SIGTERM );
Sigaddset (& sig_set, SIGINT );
Sigprocmask (SIG_BLOCK, & sig_set, NULL );

// The main thread goes to sleep and waits for the signal to jump out of sleep
While (1 ){
Sigpending (& sig_pending );
If (sigismember (& sig_pending, SIGTERM) |
Sigismember (& sig_pending, SIGINT )){
Break;
}
Sleep (2 );
}

// Reasonable reason for sub-thread exit
................
Return 0;

}

After the program runs, it is very strange to find that the program exits immediately after pressing Ctrl + C without the information of the Sub-thread exiting.
After careful analysis, the problem is that the characteristics of the multi-threaded model in Linux are ignored.

In Linux, a thread is essentially a light weighted process. When a thread is generated, a corresponding process control structure is generated, this structure shares the same process memory space with the process control structure of the parent thread. At the same time, the process control structure of the new thread will copy the same process information from the parent thread (process), such as opening the file list and blocking mask. Since the signal blocking mask is modified after the subthread is generated, the subthread uses the original process information of the main thread at this moment. Therefore, the subthread will still respond to the SIGINT and SIGTERM signals, therefore, when we use Ctrl + C to send a SIGINT signal, the main process does not process the signal, and the sub-process (thread) performs default processing, that is, exit. When a child process exits, it sends a SIGCHLD signal to the parent process (thread), indicating that the child process exits. Because the signal is not blocked, the main process (thread) exits immediately, the preceding running condition occurs. Therefore, one solution to this problem is to set the signal before the subthread is generated, or set the signal inside the subthread. Since subthreads are often a transaction processing function, I suggest using the former in simple cases. If the signal to be processed is complicated, the last method must be used for processing. In this way, the above program logic can be changed to the following:

# Include // Header file required for Signal Processing
Int main (int argc, char * argv []) {
// Other variable declarations
Sigset_t sig_set, sig_pending;
Start several subthreads
...........


// The main thread goes to sleep and waits for the signal to jump out of sleep
While (1 ){
Sigpending (& sig_pending );
If (sigismember (& sig_pending, SIGTERM) |
Sigismember (& sig_pending, SIGINT )){
Break;
}
Sleep (2 );
}

// Reasonable reason for sub-thread exit
................
Return 0;

}
Related Article

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.