Summary of multi-threaded Linux signals (i.)
1. In a multithreaded environment, the resulting signal is passed to the entire process, in general, all threads have the opportunity to receive this signal, the process in the receiving signal of the thread context execution signal processing function, specifically which thread execution is difficult to learn. that is, the signal is randomly sent to a thread of the process.
2 The implementation of the signal function bsd/linux is not in the signal processing function call, the processing of the recovery signal is the default, but the signal processing when the signal is blocked until the signal processing function returns. Other implementations may call the signal processing function, the processing of the recovery signal is the default mode, it is necessary to reconstruct the signal processing function for us to define the processing function, in these systems, the better way is to use sigaction to establish a signal processing function.
3 Send a signal to the process, which thread will receive it? Apue said that in multi-threaded programs, if you do not do special signal blocking processing, when sending signals to the process, the system chooses a thread to process this signal.
4 If a thread can block a signal in a process, and some threads can handle it, then when we send this signal to a process or thread that cannot process the signal, the system will post the signal to the thread that can handle the signal with the smallest process number.
5 If we register the signal processing function at the same time and use sigwait to wait for the signal, who will take the signal? After experimentation, sigwait on Linux has a high priority.
6 in the POSIX threading model in Linux, threads have separate process numbers that can be used by getpid () to get the thread's process number, while the thread number is stored in the pthread_t value. The process number of the main thread is the process number of the whole process, so sending a signal to the main process will only send the signal to the main thread. If the main thread sets a signal mask, the signal is posted to a thread that can be processed.
7 When you call the system function to execute a shell command, you can safely block SIGCHLD, because the system handles the termination of the child process on its own.
8 When using sleep (), to be assured to block the SIGALRM signal, the sleep function is not dependent on the ALRM function of the SIGALRM signal to work.
Summary of multi-threaded Linux signals (II.)
1. By default, the signal is received by the main process, even if the signal processing function is registered by a child thread
2. Each thread has its own signal-masking word, and can use the Sigprocmask function to mask a thread's response to the signal, leaving only the thread that needs to process the signal to process the specified signal.
3. For a signal processing function, the last time the program executes the processing function to register , that is, in all the threads, the same signal in any line thread the signal processing must be the same
4. You can use Pthread_kill to send a signal to a specified thread
Apue: Each thread has its own signal-masking word, but the processing of the signal is shared by all threads in the process, This means that while a single thread can block certain signals, when a thread modifies the processing behavior associated with a signal, the Some threads share this change in processing behavior. Thus, if a thread chooses to ignore a signal and other threads can restore the default processing behavior of the signal, or set a new handler for the signal, the signal selection of the above thread can be revoked .
The signal in the process is sent to a single thread, and if the signal is related to a hardware failure or a timer timeout, the model is sent to the thread that caused the event, while the other signals are sent to any thread.
The behavior of Sigprocmask is not defined in the multithreaded process, and the thread must use Pthread_sigmask
Summary: A signal can be processed by any thread that does not block it, but there is only one processing function that is common to multiple threads within a process. ......
Summary of multi-threaded Linux signals (III.)
In 1 Linux multithreaded applications, each thread can set the signal mask for this thread by calling Pthread_sigmask (). In general, blocked signals will not interrupt the execution of this thread unless the signal is generated because the program is running in an error such as SIGSEGV, and the signal sigkill and sigstop that cannot be ignored is not blocked.
2 when a thread calls Pthread_create () to create a new thread, the signal mask for this thread is inherited by the newly created thread.
3 signal installation Best Use sigaction way, sigaction, is to replace signal to design a more stable signal processing, signal use is relatively simple. Signal (SIGNALNO,SIGNALPROC);
The tasks that cannot be completed are: 1. Do not know the cause of the signal;
2. No other signals can be blocked in the processing signal
And Signaction, you can set more messages. In particular, the signal processing function in the process of receiving signals, what kind of processing.
The Sigaction function is used to change the behavior of a process after it receives a specific signal.
The 4 Sigprocmask function can be used only as a single thread, using the Pthread_sigmask function in multiple threads.
The 5 signal is a special message to the process, and its typical characteristics are asynchrony.
The 6 signal set represents a collection of multiple signals, the type of which is sigset_t.
7 Each process has a signal mask (or signal-masking word) that defines the set of signals that the current process requires blocking.
8 So-called blocking means that the Linux kernel does not deliver all signals in the mask to the process. The process can temporarily block the delivery of a particular signal by modifying the signal mask, and the blocked signal does not affect the behavior of the process until the signal is actually delivered .
9 ignoring signals is different from blocking signals, and ignoring signals means that the Linux kernel has delivered the generated signal to the application, but the application simply discards the signal.
Transferred from: http://blog.chinaunix.net/uid-12274566-id-3050955.html
Summary of Linux multithreading signal processing