UNIX Environment Advanced Programming: Threads and Signals

Source: Internet
Author: User
Tags error code signal handler thread valid

Each thread has its own signal screen 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, all threads must share the change in the handling behavior. Thus, if a signal chooses to ignore a signal, other threads can recover the default processing behavior of the signal, or set a new handler for the signal, so that 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 signal is 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.

[CPP] View plaincopyprint?

01.int pthread_sigmask (int how,const sigset_t * newmask, sigset_t * oldmask);/return value: Return 0 if successful, otherwise return error number

int pthread_sigmask (int how,const sigset_t * newmask, sigset_t * oldmask);/return value: Return 0 if successful, otherwise return error number

The Pthread_sigmask function is essentially the same as the Sigprocmask function, except that the pthread_sigmask is in the thread of the work and returns an error code when it fails, rather than setting errno and returning-1 as in Sigprocmask.

A thread can wait for one or more signals to occur by calling Sigwait:

[CPP] View plaincopyprint?

01.int sigwait (const sigset_t * set, int * Signop);/return value: Return 0 if successful, otherwise return error number

int sigwait (CONST sigset_t * set, int * Signop);/return value: Returns 0 if successful, otherwise returns error number The Set parameter indicates the set of signals that the thread waits for, and the integer that the Signop points to will be the return value. Indicates the number of the signal sent (i.e. SIGINT corresponds to number 2nd).

Note: The signal in the signal set that the Sigwait function waits for must be blocked before (that is, the waiting signal needs to be added to the signal mask).

The sigwait function blocks the thread that calls it until it receives a signal that it waits, and then sigwait it out of the pending queue (because it is blocked, so it must be pending), but one thing to note is that after it is taken out of the pending queue, It does not affect the state in which the signal being removed is blocked (that is, the original signal screen Word will not change).

It does only two of the work:

Waiting for the signal it waits for;

If the waiting signal is generated, move it out of the pending queue

If multiple threads call sigwait and wait for the same signal, a thread block occurs. When the signal is delivered, only one thread can return from the sigwait. If the signal is captured (for example, a process establishes a signal handler by using sigaction) and the thread is waiting for the same signal in the sigwait call, then the operating system implementation determines how the signal is delivered. In this case, the operating system implementation allows the sigwait to return, or it can activate the signal handler, but it does not appear to be both. (The following code verifies that the sigwait has a higher priority under the Ubuntu 10.04 System)

To send a signal to a process, you can call kill; to send a signal to a thread, you can call Pthread_kill:

[CPP] View plaincopyprint?

#include <signal.h>

02.int Pthread_kill (pthread_t thread,int signo)//return value: Returns 0 if successful, otherwise the error number is returned. Thread does not exist: Esrch signal is not valid: Einval

#include <signal.h>

int Pthread_kill (pthread_t thread,int signo)//return value: Returns 0 if successful, otherwise the error number is returned. Thread does not exist: Esrch signal is not valid: Einval

You can pass a 0-value signo to check if a thread exists. If the signal's default processing action is to terminate the process, then passing the signal to a thread will still kill the whole process, so we use signal number NO. 0, which is a reserved signal, a function to determine whether the thread is still alive.

Kill () sends a signal to the process, which thread is not known to handle the signal. What may happen is that the process itself masks the signal, and a thread does not have a shield to change the signal, and the thread processes the signal.

Note that the alarm clock timer is a process resource, and all threads share the same alarm. So it is impossible for multiple threads in the process to interfere (or not collaborate) with the alarm timer.

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.