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

Source: Internet
Author: User
Tags sigint signal

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 I want to change it to the main thread after the sub-thread starts.
Go to unlimited sleep until you receive sigterm or SIGINT. The main program is as follows:
Other header files
# Include <signal. h>
// 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;

}

Detected after running the program
After pressing CTRL + C, the program immediately exits without the information of the Sub-thread exiting, which is very strange.
After careful analysis, we found that the problem was that we ignored the features of the multi-threaded model in Linux.
Point.

Linux threads are essentially light weighted
Process), the corresponding process control structure will be generated when the thread is generated, but the 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. Because we modified the signal blocking mask after the subthread was generated
At the moment, the sub-thread uses the original process information of the main thread, so the sub-thread will still respond to the SIGINT and sigterm signals, so when we use Ctrl + C to send
When the SIGINT signal is sent, the main process does not process the signal, and the sub-process (thread) performs default processing, that is, exit. When the child process exits, it will send a sigchld signal to the parent process (thread ).
Indicates that the sub-process exits. Because the signal is not blocked, the main process (thread) also exits immediately. Therefore, one solution to this problem is to set the signal before the subthread is generated.
Or set the signal inside the sub-thread.
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
The logic is changed to the following:

# Include <signal. h> // 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;

}

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.