Linux operating system Multithreading signal summary __linux

Source: Internet
Author: User
Tags sleep function

Http://www.ccw.com.cn/college/soft/b2c/os/htm2012/20120101_957770.shtml

A summary of Linux multithreaded signal programming

Linux Multithreading signal Summary (i)

1. In a multi-threaded 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 in the thread context of the execution of signal processing functions, specifically which thread execution is difficult to learn.

The implementation of the 2 signal function bsd/linux not when the signal processing function is called, the recovery signal processing is the default, but the signal processing is blocked until the signal processing function returns. Other implementations may, when invoking a signal processing function, restore the processing of the signal as the default, and therefore need to reconstruct the processing function defined by the signal processing function in the signal processing function, in which a better method is to use sigaction to establish the 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 a special signal blocking processing, when sending signals to the process, the system chooses a thread to handle this signal.

4 If in the process, some threads can block a signal, and some threads can handle the signal, and when we send this signal to the process or to a thread in the process that cannot process the signal, the system will post the signal to the process with the smallest number of threads that can handle the signal.

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 experiments, Linux sigwait high priority.

6 in the POSIX threading model of Linux, threads have separate process numbers, and can get the thread's process number through getpid (), while the thread number is stored in pthread_t values. 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 calling the system function to execute the shell command, you can safely block SIGCHLD because system will handle the child process termination problem itself.

8 use Sleep (), to be assured to block the SIGALRM signal, the current sleep function will not rely on the ALRM function of the SIGALRM signal to work.

Linux Multithreading signal Summary (ii)

1. By default, the signal is processed by the main process, even if the signal processing function is registered by a child thread

2. Each thread has its own signal screen word, you can use the Sigprocmask function to mask the response of a thread to the signal processing, leaving only the thread that needs to handle the signal to handle the specified signal.

3. For a signal processing function, in the process of execution of the last registration of the processing function, that is, in all threads, the same signal in any line Chengri to the signal processing must be the same

4. You can use Pthread_kill to send a signal to a specified thread

Apue said: Each thread has its own signal screen word, but the signal processing is the process of all threads shared,

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 the change in the handling behavior. So that if one thread chooses to ignore a signal, other threads can

To restore the default processing behavior of the signal, or to set a new handler for the signal, which can undo the

Signal selection.

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

Sent to the thread that caused the event, while the other signals are sent to any thread.

Sigprocmask behavior is not defined in multithreaded processes and threads must use Pthread_sigmask

Summary: A signal can be processed by any thread that does not mask it, but there is only one handler in a process that is shared by multiple threads. ......

Linux Multithreading signal Summary (iii)

1 Linux multi-threaded applications, each thread can set this thread's signal mask by calling Pthread_sigmask (). In general, a blocked signal will not interrupt execution of this thread unless the signal is generated because the program is running an error such as SIGSEGV, and the signal Sigkill and Sigstop cannot be 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 of the best use of Sigaction way, Sigaction, is to replace the signal to design a more stable signal processing, signal use is relatively simple. Signal (SIGNALNO,SIGNALPROC);

Tasks that cannot be accomplished are: 1. Do not know the cause of the signal;

2. Can not block other signals in the processing signal

And Signaction, you can set more messages. Especially in the process of signal processing function to receive signals, what to do.

The Sigaction function is used to change the behavior of a process after it receives a specific signal.

The 4 Sigprocmask function can only be used for single-threaded, and Pthread_sigmask functions are used in multiple threads.

A 5 signal is a special message sent to a process, and its typical characteristic is asynchronous.

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, called a signal screen word), which defines the set of signals that the current process requires to block.

8 blocking means that the Linux kernel does not deliver all the signals in the mask to the process. The process can then temporarily block the delivery of a particular signal by modifying the signal mask, and the blocked signal does not affect the process's behavior until the signal is actually delivered.

9 ignoring signal is different from blocking signal, ignoring signal is that the Linux kernel has delivered the signal to the application, but the application simply discards the signal.

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.