Signal to achieve synchronization between parent and child processes: the role of Sigsuspend

Source: Internet
Author: User
Tags signal handler thread

Function Prototypes:

#include <signal.h>

int sigsuspend (const sigset_t *mask);

Role:

Used to replace the process's signal mask temporarily with mask before receiving a signal, and to suspend the execution of the process until the signal is received.

The Sigsuspend () function replaces the current signal mask of the calling thread with the set of signals pointed to by SIG Mask and then suspends the thread until delivery of a signal whose action is either to execute a signal-catching function Or to terminate the process. This is not cause any of the other signals so may have been pending on the "process to" become on the thread.

If the action is to terminate the process then Sigsuspend () would never return. If the action is to execute a signal-catching function, Thensigsuspend () 'll return after the signal-catching function re Turns, with the signal mask restored to the "set" existed prior to Thesigsuspend ().

It isn't possible to block signals the cannot be ignored. This is enforced by the system without causing a error to be indicated.

That is, after sigsuspend, the process hangs there, waiting for the open signal to wake up. After receiving the signal, the system restores the current signal set to its original, and then calls the handler function.

return value:

Sigsuspend returns the signal mask before the call is restored. After the signal processing function completes, the process continues to execute. The system call always returns-1 and sets the errno to Eintr.

Since Sigsuspend () suspends process execution indefinitely, there is no successful completion return value. If A return occurs,-1 are returned and errno are set to indicate the error.

The Sigsuspend () function would fail if:

[Eintr]

A signal is caught by the calling process and control are returned from the signal-catching function.

That is, after sigsuspend, the process hangs there, waiting for the open signal to wake up. After receiving the signal, the system restores the current signal set to its original, and then calls the handler function.

Stevens in the UNIX Environment Advanced Programming book, "If a signal is caught and if the signal handler returns, then Sigsuspend returns and the Signal mask of the "process is" set to its value before the called to Sigsuspend. "Because the Sigsuspend is atomic operation, so this is the feeling of the first call signal hand Ler first returns, then Sigsuspend returns.

 int main (void) {sigset_t newmask, oldmask, Zeromask;    
        
   if (Signal (SIGINT, sig_int) = = Sig_err) Err_sys ("Signal (SIGINT) error");    
        
   Sigemptyset (&zeromask);    
   Sigemptyset (&newmask);    
   Sigaddset (&newmask, SIGINT);    
      /* Block SIGINT and save current signal mask/if (Sigprocmask (Sig_block, &newmask, &oldmask) < 0)    
        
   Err_sys ("Sig_block error");    
        
   /* Critical Region of code */Pr_mask ("In Critical region:");    
   /* Allow all signals and pause/if (Sigsuspend (&zeromask)!=-1) err_sys ("Sigsuspend error");    
        
   Pr_mask ("After return from Sigsuspend:"); /* Reset signal Mask which unblocks SIGINT/if (Sigprocmask (Sig_setmask, &oldmask, NULL) < 0) Err_sy    
        
   S ("Sig_setmask error");    
/* and Continue processing ... * * exit (0); } static void Sig_int (int signo) {PR_mask ("\nin sig_int:");    
Return }

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.