Turn from: http://blog.csdn.net/anxuegang/article/details/6636410
These days to a network flow collector program has been basically changed, the original in the main function in a few child threads started after sleeping 10 minutes after the start of cleaning child thread and exit. Now want to change to a child thread started after the main thread into infinite sleep, until received sigterm or SIGINT. The main program is as follows:
Other header files
#include header files for <signal.h>//Signal processing
int main (int argc, char * argv[]) {
Other required 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 child threads
...........
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, waits for the signal to arrive and jumps out of sleep
while (1) {
Sigpending (&sig_pending);
if (Sigismember (&sig_pending, sigterm) | |
Sigismember (&sig_pending,sigint)) {
Break
}
Sleep (2);
}
Child thread Exit sensible
................
return 0;
}
After the program is run, it is very strange that the program does not appear when the child thread exits when CTRL + C is pressed.
After careful analysis, it is found that the problem lies in ignoring the characteristics of the multithreaded model under Linux.
The threads under Linux are essentially lightweight processes (light weighted process) that generate the corresponding process control structure when the thread is generated, except that 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 copies the same process information from the parent thread (process), such as opening a file list and a signal blocking mask. Since we modified the signal blocking mask after the mechanical engineer of the Strand, now the child thread is using the original process information of the main thread, so the child thread will still respond to the SIGINT and sigterm signals, so when we use CTRL + C to send the SIGINT signal, the main process does not process the signal, The child process (thread) is processed by default, that is, exit. When the child process exits, it sends a SIGCHLD signal to the parent process (the thread), indicating that the child process exited, and that the signal was not blocked, causing the main process (thread) to exit immediately, and the aforementioned operation occurred. One solution to this problem is to signal settings before the child thread is generated, or to signal settings within the child thread. Since a child thread is often a transaction handler, I recommend that the former be used in simple cases, and that if the signal to be processed is more complex, the latter method must be used. In this way, the above procedural logic can be changed to read as follows:
#include header files for <signal.h>//Signal processing
int main (int argc, char * argv[]) {
Other required variable declarations
sigset_t sig_set,sig_pending;
Start several child threads
...........
The main thread goes to sleep, waits for the signal to arrive and jumps out of sleep
while (1) {
Sigpending (&sig_pending);
if (Sigismember (&sig_pending, sigterm) | |
Sigismember (&sig_pending,sigint)) {
Break
}
Sleep (2);
}
Child thread Exit sensible
................
return 0;
}