Linux under C Programming: Sigsuspend Execution Process Analysis

Source: Internet
Author: User
Tags execution signal handler thread linux

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

/*the sigsuspend () function replaces the current signal mask of the calling thread and the set of signals pointed to     
   By Sigmask and then suspends the thread until delivery of a signal whose action are 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 () the signal-catching functio n returns, 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.

Stevens in the Advanced Programming for UNIX environment. "If a signal is caught and if the signal handler returns, then Sigsuspend returns and the SIG Nal 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 handler Return first, then sigsuspend back.

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_     
         
   SYS ("Sig_setmask error");     
/* and Continue processing ... * * exit (0); } static void Sig_iNT (int signo) {pr_mask ("\nin sig_int:");     
Return }

Results:

$a. Out in 
critical region:sigint     
^c in     
sig_int:sigint     

If you follow the Sig_handler first, then SIGINT should not be printed, because the screen word has not been restored, all signals are not blocked. Well, is Stevens wrong? Of course not, just Stevens did not say please in the Sigsuspend atomic operation in the end what do?

The entire atomic operation process for Sigsuspend is:

(1) Setting a new mask blocking the current process;

(2) receiving the signal to restore the original mask;

(3) Calling the signal processing function set by the process;

(4) After the signal processing function returned, Sigsuspend return.

is basically the above process, oh, the original signal handler is a part of the atomic operation, but also after the restoration of the screen word, so the above example is no problem, Stevens said is correct. Because of the countless connections between Linux and UNIX, the semantics of most system calls on two platforms are the same. The above Sigsuspend atomic operation is also calculated from the "deep understanding of the Linux kernel" book. The description in the book is as follows:

/*the sigsuspend () system call puts the "process in the" task_interruptible state, after having blocked the standard s specified by    
 a bit mask array to which the mask parameter points. The process would wake up only when a nonignored, nonblocked signal are sent to     
 it. The corresponding sys_sigsuspend () service routine executes these statements:    
* * Mask 
         
&= ~ (Sigmask (SIGKILL) | Sigmask (SIGSTOP));     
SPIN_LOCK_IRQ (¤t->sigmask_lock);     
Saveset = current->blocked;     
Siginitset (¤t->blocked, mask);     
Recalc_sigpending (current);     
SPIN_UNLOCK_IRQ (¤t->sigmask_lock);     
Regs->eax =-eintr;     
while (1) {     
    current->state = task_interruptible;     
    Schedule (  );     
    if (Do_signal (regs, &saveset))     
        return-eintr;     
}

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.