Linux/UNIX signal (2)

Source: Internet
Author: User
Tags sleep function
Signal (2) the sigaction function is used to check or modify the processing actions associated with the specified signal. This function replaces the sign signal (2) sigaction function

The sigaction function is used to check or modify the processing actions associated with the specified signal. This function replaces the signal function.

# Include

Int sigaction (int signum, const structsigaction * act, struct sigaction * oldact );

This function uses the following structure:

Struct sigaction {

Void (* sa_handler) (int );

Void (* sa_sigaction) (int, siginfo_t *, void *);

Sigset_t sa_mask;

Int sa_flags;

Void (* sa_restorer) (void );

};

Return value: if the request succeeds, 0 is returned. If an error occurs,-1 is returned.

Here, the parameter signum is the signal number to detect or modify the specific action. If the oact pointer is not empty, the original action on the signal is written to the position it points. If act is a null pointer, the sigaction function does not need to perform other settings. Otherwise, the action on the specified signal will be set in this parameter.

In the sigaction structure directed by the act parameter, sa_handler is a function pointer that points to the signal processing function that will be called when the signal sig is received. It is equivalent to the func parameter of the passing function signal. We can set the sa_handler field to special SIG_IGN and SIG_DFL, which indicate that the signal will be ignored or the processing method of the signal will be restored to the default action.

The sa_mask field specifies a signal set. before calling the signal processing function pointed to by sa_handle, the signal set will be added to the signal shielding word of the process. This is a group of signals that will be blocked and will not be passed to the process. Set the signal shielding character to prevent the previously seen signal from being received when its processing function has not been completed.

The following program can implement the signal function.

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_INTERRUPT       act.sa_flags |= SA_INTERRUPT;#endif    }else {#ifdef SA_RESTART       act.sa_flags |= SA_RESTART;#endif    }   if (sigaction(signo, &act, &oact) < 0)       return(SIG_ERR);   return(oact.sa_handler);}
Sigsetjmp and siglongjmp

These two functions should be used for non-local transfer in signal processing.

# Include

Int sigsetjmp (sigjmp_buf env, intsavesigs );

Void siglongjmp (sigjmp_buf env, int val );

The only difference between these two functions and setjmp and longjmp is that sigsetjmp adds a parameter. If the savesigs is not 0, sigsetjmp saves the current signal shielding word of the process in the env. When siglongjmp is called, if the sigsetjmp call with non-0 savesigs has saved the env, siglongjmp restores the stored signal shielding characters from it.

Sigsuspend function

# Include

Intsigsuspend (const sigset_t * mask );

Set the signal shielding word of the process to the value pointed to by the mask. The process is suspended until a signal is captured or a signal is generated that terminates the process. If a signal is captured and returned from the signal processing program, sigsuspend is returned, and the signal shielding word of the process is set to the value before the call of sigsuspend.

Absorb function

# Include

Voidabort (void );

This function terminates an exception. This function sends the SIGABRT signal to the calling process. When abort is called, a notification is sent to the host environment to indicate a successful termination. the method is to call the raise function. Abort will fl the opened stream. make sure that the process does not block the signal and does not block the process.

The following is the implementation of the abort function in POSIX.1:

#include
 
  #include
  
   #include
   
    #include
    
      voidabort(void)         /* POSIX-style abort() function */{    sigset_t            mask;    struct sigaction    action;     /*     * Caller can't ignore SIGABRT, if so resetto default.     */    sigaction(SIGABRT, NULL, &action);    if (action.sa_handler == SIG_IGN) {        action.sa_handler = SIG_DFL;        sigaction(SIGABRT, &action, NULL);    }    if (action.sa_handler == SIG_DFL)        fflush(NULL);           /* flush all open stdio streams *//*     * Caller can't block SIGABRT; make sureit's unblocked.     */    sigfillset(&mask);    sigdelset(&mask, SIGABRT);  /* mask has only SIGABRT turned off */    sigprocmask(SIG_SETMASK, &mask, NULL);    kill(getpid(), SIGABRT);    /* send the signal */     /*     * If we're here, process caught SIGABRTand returned.     */    fflush(NULL);               /* flush all open stdio streams*/    action.sa_handler = SIG_DFL;    sigaction(SIGABRT, &action, NULL);  /* reset to default */    sigprocmask(SIG_SETMASK, &mask,NULL);  /* just in case ... */    kill(getpid(), SIGABRT);                /* and one more time */    exit(1);   /* this should never be executed ... */}
    
   
  
 
Sleep function

# Include

Unsigned int sleep (unsigned int seconds );

This function suspends the calling process until one of the following conditions is met:

1. the wall clock time specified by seconds has passed

2. call the process to capture a signal and return it from the signal processing program

Job control signal

Six signal related to job control:

The SIGCHLD sub-process has been stopped or terminated.

SIGCONT if the process has stopped, make it continue to run

SIGSTOP stop signal

SIGTSTP interactive stop signal

SIGTTIN background process Group member read control terminal

The SIGTTOU background process Group is written to the control terminal.

When you type the suspend character key (usually ctrl + Z), SIGTSTP is sent to all processes in the foreground process Group. When we notify shell to resume a job in the foreground or background, shell sends a SIGCONT signal to all processes in the job.

Related Article

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.