A robust signal interface in Linux

Source: Internet
Author: User

Linux provides a more robust signal interface: sigaction.

# Include <signal. h>

Int sigaction (INT Sig, const struct sigaction * Act, struct sigaction * OCAT );

The sigaction structure is defined in the file signal. H. It defines the action to be taken after receiving the signal specified by the SIG parameter. This structure should include at least

The following members.

Void (*) (INT) sa_handler/* function, sig_dfl or sig_ign */

Sigset_t sa_mask/* signals to block in sa_handler */

Int sa_flags/* signal action modifiers */

The sigaction function returns 0 upon success and 0 upon failure.

In the sigaction structure pointed to by the cat parameter, sa_handler is a function pointer that is called when it points to the signal Sig.

The sa_mask Member specifies a signal set. before calling the signal processing function pointed to by sa_handler, the signal set will be added to the signal shielding word of the process.

#include <signal.h>#include <stdio.h>#include <unistd.h>void ouch(int sig){    printf("OUCH! - I got signal %d\n", sig);}int main(){    struct sigaction act;    act.sa_handler = ouch;    sigemptyset(&act.sa_mask);    act.sa_flags = 0;    sigaction(SIGINT, &act, 0);  while(1) {    printf("Hello World!\n");    sleep(1);  }}

Signal Set

The header file signal. h defines the type sigset_t and the function used to process the signal set. Sigaction and other functions use these signal sets to modify the behavior of processes when receiving signals.

#include<signal.h>int sigaddset(sigset_t *set, int signo);int sigemptyset(sigset_t *set);int sigfillset(sigset_t *set);int sigdelset(sigset_t *set ,  int signo);

Sigempty initializes the signal set to null. Sigfillset processes signals as defined signals. Adding or deleting a given signal from the Signal Set

(Signo ). They return 0 upon success,-1 upon failure, and errno.

The sigismember function determines whether a given signal is a member of a Signal Set. If yes, 1 is returned. If no, 0 is returned. If the given signal is invalid,-1 is returned and

Set errno to einval.

# Include <signal. h>

Int sigismember (sigset_t * Set, int signo );

The sigprocmask function is used to set or check the process signal shielding characters. A blocked signal is a set of signals that cannot be received by the current process.

# Include <signal. h>

Int sigprocmask (INT how, const sigset_t * Set, sigset_t * oset );

The sigprocmask function can modify the signal shielding characters of a process according to the method specified by the parameter "how. The new signal shielding word is specified by the set parameter.

The value of how can be one of the 11-6 tables.

Sig_block adds the signal in the Set parameter to the signal shielding character.

Sig_setmask sets the signal shielding word to the signal in the Set parameter.

Set_unblock: removes the signal from the set parameter from the blocked signal.

If a signal is blocked by the process, it will not be passed to the process. But it stays in the pending status. The program can call the sigpending function to view the blocked signal.

Which of the following are waiting for processing.

# Include <signal. h>

Int sigpending (sigset_t * Set );

A process can suspend its own execution by calling the sigsuspend function to know that a signal in the signal set has reached.

# Include <signal. h>

Int sigsuspend (const sigset_t * sigmask );

This function replaces the process mask with the signal set given by the sigmask parameter, and then suspends program execution.

1. sigaction flag

Sa_flags used in the sigaction structure in the sigaction Function

Sa_nocldstop: If the sa_nocldstop bit is set and the target signal is sigchld, the parent process will not receive a notification when the child process stops (STOP) unless the child process exits.

Sa_nocldwait: The sa_nocldwait flag will prevent sub-processes from becoming zombie processes. It is used when the target signal is sigchld. If the process sets this flag and then calls a wait system call, the process will be blocked until all sub-processes are terminated, and finally return-1: here, the explanation in apue2ed is that 1 is returned), and The errno global variable is set to echild.

Sa_onstack: in some cases, signal processing must be performed on a specific stack. This method is provided for sigaction system calls. If this bit is set, the signal will be delivered to the specified stack.

Sa_nodefer: If the sa_nodefer bit is set, the system will not block the delivery of the current signal when it is being processed.

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.