Reading Notes of advanced programming in UNIX environment (2)

Source: Internet
Author: User

1. Function sigaction

The sigaction function is used to check or modify the processing actions associated with the specified signal. The function prototype is as follows:

# Inlcude <signal. h>

Int sigaction (INT signo, const struct sigaction * restrict act, struct sigaction * restrict Act );

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. The structure of sigaction is as follows:


Once an action is set for a given signal, the setting remains valid until signation is called to explicitly change it.

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


The sa_sigaction field in the sigaction structure is an alternative signal processing program. This signal processing program is used when the sa_flags flag is set. Generally, the form of the signal processing function is as follows: void handler (INT signo ). However, if the sa_siginfo flag is set, the prototype of the signal processing function is void handler (INT signo, siginfo_t * info, void * context ). The siginfo_t structure contains information about the signal generation cause. The style of the structure is as follows:


The context parameter is a non-type pointer, which can be forcibly converted to the ucontext_t structure type, which identifies the context information during signal transmission.


2. Functions sigsetjmp and siglongjmp

These two functions are used for non-local transfer in the signal processing program.

# Include <setjmp. h>

Int sigsetjmp (sigjmp_buf ENV, int savemask); // If called directly, 0 is returned. If it is returned from the call of siglongjmp, the return value is not 0.

Void siglongjmp (sigjmp_buf ENV, int Val );

When sigsetmask is called, if the savemask value is 1, the current shielding word of the process is saved in env. When siglongjmp is called, if the sigsetjmp call with non-0savemask has saved the blocked word of the Process in env, siglongjmp restores the blocked word from it.

Example:

# 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 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 ("sigprocmask 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 (SIGUSR1 (SIGUSR1, sig_usr1) = sig_err) err_sys ("signal (SIGUSR1) error"); If (signal (sigalrm, sig_alrm) = sig_err) err_sys ("signal (sigalrm) error"); pr_mask ("starting main:"); If (sigsetjmp (jmpbuf, 1 )) {pr_mask ("ending 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); (;;) 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 shielding character setting of a process is specified by sigmask. The process is suspended before a signal is captured. If a signal is captured, sigsuspend returns, and the new value of the process is set to the previous value.

Example:

# 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 ("sigprocmask error"); printf ("Mask: % s ", STR); If (sigismember (& sigset, SIGINT) printf (" SIGINT "); If (sigismember (& sigset, sigquit )) printf ("sigquit"); If (sigismember (& SIG Set, 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_fla GS | = 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 (& waitmask, SIGUSR1); sigemptyset (& newmask); sigaddset (& newmask, SIGINT ); if (sigprocmask (S Ig_block, & newmask, & oldmask) <0) err_sys ("sig_block error"); pr_mask ("in critical region:"); If (sigsuspend (& waitmask )! =-1) err_sys ("suspend error"); pr_mask ("after return from sigsuspend:"); If (sigprocmask (sig_setmask, & oldmask, null) <0) err_sys ("sig_setmask error"); pr_mask ("program end:");} static void sig_int (INT signo) {pr_mask ("in sig_int :");}

4. Function sigqueue

To use the queuing signal, you must perform the following operations:

(1) Specify the sa_siginfo flag when installing the signal processing program using the sigaction function.

(2) provide the signal processing program in the sa_sigaction Member of the sigaction structure.

(3) Use the sigqueue function to send signals.

# Include <signal. h>

Int sigqueue (pid_t PID, int signo, const Union sigval value );


5. Signal name and number

The psignal function can be used to print the string corresponding to the signal number.

# Include <signal. h>

Void psignal (INT signal, const char * MSG); // string MSG input to standard error stream

If you only need the Character Description of the signal, you can use the strsignal function.

# Include <string. h>

Char * strsignal (INT signo );

Reading Notes of advanced programming in UNIX environment (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.