UNIX level Programming Environment note readout signal (2)

Source: Internet
Author: User
Tags signal handler

1. Function sigaction

Sigaction the processing action associated with the specified signal. Its function prototypes are as follows:

#inlcude <signal.h>

int sigaction (int signo,const struct sigaction * Restrict Act,struct sigaction * Restrict act);

The number of Signo is the number of the signal to be measured or altered in its detailed action.

If the act pointer is not empty, change its action. Suppose the oact pointer is not empty. The system returns the previous action of the signal via the oact pointer. The structure of the structure sigaction is as follows:


Once an action is set on a given signal, the setting is valid until the call to signation explicitly changes it.

The Sa_flags field of the ACT structure specifies the various options for processing the signal. The meanings of these options are listed in detail.


The Sa_sigaction field of the sigaction structure is an alternative signal handler.

This signal handler is used when the SA_SIGINFO flag is set by the sa_flags.

Typically, the signal processing function is in the form of this: void handler (int signo).

But assuming the SA_SIGINFO flag is set, the prototype of the signal processing function is this: void handler (int signo,siginfo_t *info,void *context). The siginfo_t structure includes information about the cause of the signal generation.

The style of the structure is seen in the following example:


The context parameter is an untyped pointer. It can be cast to the ucontext_t struct type, which identifies the context information when the signal is passed.


2. Functions sigsetjmp and SIGLONGJMP

Use these two functions when making non-local transfers in a signal handler.

#include <setjmp.h>

int sigsetjmp (sigjmp_buf env,int savemask);//If called directly, returns 0. If returned from the SIGLONGJMP call, the return is not 0.

void siglongjmp (Sigjmp_buf env,int val);

When the sigsetmask is called. Assuming that the Savemask value is 1, the current shield word of the process is saved in Env.

When calling siglongjmp, assume that a sigsetjmp call with a non-0savemask has saved the screen word of the process in Env. The siglongjmp from which to restore the saved signal screen word.

Cases:

#include "apue.h" #include <setjmp.h> #include <time.h>static void sig_usr1 (int), static void Sig_alrm (int); Static sigjmp_buf jmpbuf;static volatile sig_atomic_t canjump;void pr_mask (const char *STR)//print current blocked signal {sigset_t s        Igset;        int errno_save; Errno_save = errno; /* We can be called by signal handlers * * IF (sigprocmask (0, NULL, &sigset) < 0) perror ("SIGP        Rocmask error ");        printf ("Mask:%s", str);        if (Sigismember (&sigset, SIGINT)) printf ("SIGINT");        if (Sigismember (&sigset, Sigquit)) printf ("Sigquit");        if (Sigismember (&sigset, SIGUSR1)) printf ("SIGUSR1");        if (Sigismember (&sigset, SIGUSR2)) printf ("SIGUSR2");        if (Sigismember (&sigset, SIGALRM)) printf ("Sigalrm");        /* Remaining signals can go here */printf ("\ n"); errno = Errno_save;} SIGFUNC * Signal (int signo,sigfunc * func) {struct Sigaction Act,oact;act.sa_handler = Func;sigemptyset (&act.sa_mask) Act.sa_flags = 0;if (Signo = = sigalrm) {#ifdef sa_interruptact.sa_flags |= sa_interrupt; #endif} else {Act.sa_flags |= Sa_restart;} if (Sigaction (Signo,&act,&oact) < 0) return (SIG_ERR); return (Oact.sa_handler);} int main (void) {if (signal (SIGUSR1,SIG_USR1) = = Sig_err) Err_sys ("Signal (SIGUSR1) error"); if (Signal (SIGALRM,SIG_ALRM) = = Sig_err) Err_sys ("Signal (SIGALRM) error");p R_mask ("Starting main:"); if (sigsetjmp (jmpbuf,1)) {pr_mask ("en Ding main: "); exit (0);} Canjump = 1;for (;;) Pause ();} static void sig_usr1 (int signo) {time_t starttime;if (canjump = 0) return;pr_mask ("Starting SIG_USR1:"); alarm (3); StartTime = time (NULL); for (;;) if (Time (NULL) > Starttime+5) break;pr_mask ("Finishing SIG_USR1:"); canjump = 0;siglongjmp (jmpbuf,1);} static void Sig_alrm (int signo) {pr_mask ("in Sig_alrm:");}

3. Function Sigsuspend

#include <signal.h>

int sigsuspend (const sigset_t * sigmask);

The signal mask Word setting for the process is specified by Sigmask.

The process is suspended until a signal is captured. Assuming a signal is captured, Sigsuspend returns, and the new high-screen word of the process is set to its previous value.

Cases:

#include "apue.h" static void Sig_int (int), void pr_mask (const char *STR)//print the current blocked signal {sigset_t sigset;        int errno_save; Errno_save = errno; /* We can be called by signal handlers * * IF (sigprocmask (0, NULL, &sigset) < 0) perror ("SIGP        Rocmask error ");        printf ("Mask:%s", str);        if (Sigismember (&sigset, SIGINT)) printf ("SIGINT");        if (Sigismember (&sigset, Sigquit)) printf ("Sigquit");        if (Sigismember (&sigset, SIGUSR1)) printf ("SIGUSR1");        if (Sigismember (&sigset, SIGUSR2)) printf ("SIGUSR2");        if (Sigismember (&sigset, SIGALRM)) printf ("Sigalrm");        /* Remaining signals can go here */printf ("\ n"); errno = Errno_save;} SIGFUNC * Signal (int signo,sigfunc * func) {struct Sigaction Act,oact;act.sa_handler = Func;sigemptyset (&act.sa_mask ) Act.sa_flags = 0;if (Signo = = sigalrm) {#ifdef sa_interruptact.sa_flags |= sa_interrupt; #endif} else {act.sa_flags |= SA_ RESTART;} IF (sigaction (Signo,&act,&oact) < 0) return (SIG_ERR); return (Oact.sa_handler);} int main (void) {sigset_t newmask,oldmask,waitmask;pr_mask ("program start:"); if (signal (sigint,sig_int) = = Sig_err) ERR _sys ("Signal (SIGINT) error"), Sigemptyset (&waitmask); Sigaddset (&AMP;WAITMASK,SIGUSR1); Sigemptyset (& Newmask), Sigaddset (&newmask,sigint), if (Sigprocmask (Sig_block,&newmask,&oldmask) < 0) Err_sys ("SIG_ BLOCK error ");p R_mask (" In Critical Region: "), if (Sigsuspend (&waitmask)! =-1) err_sys (" Suspend error ");p R_mask (" After return from Sigsuspend: "), if (Sigprocmask (Sig_setmask,&oldmask,null) < 0) Err_sys (" Sig_setmask error ");p R _mask ("program End:");} static void Sig_int (int signo) {pr_mask ("in Sig_int:");}

4. Function Sigqueue

There are several things you must do to use a queued signal:

(1) Specify the SA_SIGINFO flag when installing a signal handler using the Sigaction function.

(2) A signal processing program is provided in the Sa_sigaction member of the sigaction structure.

(3) Send a signal using the Sigqueue function.

#include <signal.h>

int Sigqueue (pid_t pid,int signo,const Union sigval value);


5. Signal name and number

The ability to print a string corresponding to the signal number can be ported through the psignal function.

#include <signal.h>

void psignal (int signal,const char * msg);//string MSG input to standard error stream

Assume that only a descriptive description of the character signal is required and can be used for the strsignal function.

#include <string.h>

char *strsignal (int signo);

UNIX level Programming Environment note readout signal (2)

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.