One example of Linux multithreaded programming and signal negligence __arduino

Source: Internet
Author: User
Tags sigint signal

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;

}

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.