Sigaction function for Signals

Source: Internet
Author: User

The sigaction function is used to check or modify the processing actions associated with the specified signal.(Or execute these two operations at the same time ).

#include <signal.h> sigaction(  signo,   sigaction *restrict act,  sigaction *-

The signo parameter indicates the Signal Number of the specific action to be detected or modified. If the act pointer is not empty, modify its action. If the oact pointer is not null, the system returns the previous action of the signal through the oact pointer.. This function uses the following structure:

    (*sa_handler)();            sa_flags;                           (*sa_sigaction)(, siginfo_t *,  *

When changing the signal action, if the sa_handler field contains the address of a Signal capture function (opposite to the constant SIG_IGN or SIG_DFL), The sa_mask field describes a signal set, before calling the signal capturing function, this signal set must be added to the signal shielding word of the process.. In this way, some signals can be blocked when a signal processing program is called.Therefore, when processing a given signal, if this signal occurs again, it will be blocked until the processing of the previous signal ends.

Once an action is set for a given signal, the setting remains valid until sigaction is explicitly changed.. (Not in earlier versions,:)

The sa_flags field of the act structure specifies various options for signal processing.

Table 10-5 Options for processing each signal (sa_flags)

The sa_sigaction field is an alternative signal processing program. This signal processing program is used when the SA_SIGINFO flag is used in the sigaction structure.. For the sa_sigaction field and sa_handler field, the implementation may use the same bucket, so the application can only use one of the two fields at a time.

Generally, call the signal processing program in the following ways:

 handler( signo);

However, if the SA_SIGINFO flag is set, call the signal processing program as follows:

 handler( signo, siginfo_t *info,  *context);

The siginfo_t structure contains information about the signal generation cause. The general style of the structure is as follows:

      si_signo;                  si_errno;                  si_code;                 *si_addr;                   si_status;                si_band;             

 

Si_code values of various signals (including the preceding data structure and flag options) can be viewed using the man sigaction command.

If the signal is SIGCHLD, The si_pid, si_status, and si_uid fields are set.

If the signal is SIGILL or SIGSEGV, si_addr contains the root address of the fault, although the address may not be accurate.

If the signal is SIGPOLL, The si_band field contains the priority band of the STREAMS message, which generates POLL_IN, POLL_OUT, or POLL_MSG events.

The si_errno field contains the error number, which corresponds to the conditions for generating signals and is defined by implementation.

The context parameter of the signal processing program is a non-type pointer, which can be forcibly converted to the ucntext_t structure type to identify the context of the process during signal transmission.

 

 Instance: signal function

Now we use sigaction to implement the signal function. Many platforms do this.

Program list 10-12 use sigaction to implement the signal function

#include * signo, Sigfunc *=&= (signo ==|=|=(sigaction(signo, &act, &oact) < 

Note: you must use the sigemptyset function to initialize the sa_mask Member of the act structure. Cannot guarantee: act. sa_mask = 0; will do the same thing.

For all signals except SIGALRM, we have tried to set the SA_RESTART flag, so system Calls interrupted by these signals can be automatically restarted. The reason we do not want to restart the system call interrupted by the SIGALRM signal is that we want to set a time limit for I/O operations.

Instance: signal_intr Function

Program listing 10-13 is another version of the signal function, which attempts to prevent any interrupted system calls from restarting.

Program list 10-13 signal_intr Function

#include * signo, Sigfunc *=&= |=    (sigaction(signo, &act, &oact) < 

 

If the system defines the SA_INTERRUPT flag, we add this flag in sa_flags to improve portability, thus preventing the interrupted system call from restarting.

 

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.