歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 信號實現父子進程之間的同步:sigsuspend的作用

信號實現父子進程之間的同步:sigsuspend的作用

日期:2017/3/3 16:23:49   编辑:關於Linux

函數原型:

#include <signal.h>

int sigsuspend(const sigset_t *mask);

作用:

用於在接收到某個信號之前,臨時用mask替換進程的信號掩碼,並暫停進程執行,直到收到信號為止。

The sigsuspend() function replaces the current signal mask of the calling thread with the set of signals pointed to by sigmask 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 will not cause any other signals that may have been pending on the process to become pending on the thread.

If the action is to terminate the process then sigsuspend() will never return. If the action is to execute a signal-catching function, thensigsuspend() will return after the signal-catching function returns, with the signal mask restored to the set that existed prior to thesigsuspend() call.

It is not possible to block signals that cannot be ignored. This is enforced by the system without causing an error to be indicated.

也就是說,sigsuspend後,進程就掛在那裡,等待著開放的信號的喚醒。系統在接收到信號後,馬上就把現在的信號集還原為原來的,然後調用處理函數。

返回值:

sigsuspend返回後將恢復調用之前的的信號掩碼。信號處理函數完成後,進程將繼續執行。該系統調用始終返回-1,並將errno設置為EINTR.

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

The sigsuspend() function will fail if:

[EINTR]

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

也就是說,sigsuspend後,進程就掛在那裡,等待著開放的信號的喚醒。系統在接受到信號後,馬上就把現在的信號集還原為原來的,然後調用處理函數。

Stevens在《Unix環境高級編程》一書中是如是回答的“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 call to sigsuspend.”,由於sigsuspend是原子操作,所以這句給人的感覺就是先調用signal handler先返回,然後sigsuspend再返回。

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;    
}

結果:

$a.out
in critical region: SIGINT    
^C    
in sig_int: SIGINT    
after return from sigsuspend: SIGINT

如果按照sig_handler先返回,那麼SIGINT是不該被打印出來的,因為那時屏蔽字還沒有恢復,所有信號都是不阻塞的。那麼是Stevens說錯了麼?當然沒有,只是Stevens沒有說請在sigsuspend的原子操作中到底做了什麼?

sigsuspend的整個原子操作過程為:

(1) 設置新的mask阻塞當前進程;

(2) 收到信號,恢復原先mask;

(3) 調用該進程設置的信號處理函數;

(4) 待信號處理函數返回後,sigsuspend返回。

#include <stdio.h>  
#include <signal.h>
void checkset();
void func();
void main()
{
sigset_tblockset,oldblockset,zeroset,pendmask;
printf("pid:%ld\n",(long)getpid());
signal(SIGINT,func);
sigemptyset(&blockset);
sigemptyset(&zeroset);
sigaddset(&blockset,SIGINT);
sigprocmask(SIG_SETMASK,&blockset,&oldblockset);
checkset();
sigpending(&pendmask);
if(sigismember(&pendmask,SIGINT))
printf("SIGINTpending\n");
if(sigsuspend(&zeroset)!= -1)
{
printf("sigsuspenderror\n");
exit(0);
}
printf("afterreturn\n");
sigprocmask(SIG_SETMASK,&oldblockset,NULL);
printf("SIGINTunblocked\n");
}
void checkset()
{ sigset_tset;
printf("checksetstart:\n");
if(sigprocmask(0,NULL,&set)<0)
{
printf("checksetsigprocmask error!!\n");
exit(0);
}
if(sigismember(&set,SIGINT))
printf("sigint\n");
if(sigismember(&set,SIGTSTP))
printf("sigtstp\n");
if(sigismember(&set,SIGTERM))
printf("sigterm\n");
printf("checksetend\n");
}
void func()
{
printf("hellofunc\n");
}

父子進程同步到方法如下:

#include "apue.h"
      
static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */
static sigset_t newmask, oldmask, zeromask;  
      
static void
sig_usr(int signo)  /* one signal handler for SIGUSR1 and SIGUSR2 */
{  
    sigflag = 1;  
}  
      
void
TELL_WAIT(void)  
{  
    if (signal(SIGUSR1, sig_usr) == SIG_ERR)  
        err_sys("signal(SIGUSR1) error");  
    if (signal(SIGUSR2, sig_usr) == SIG_ERR)  
        err_sys("signal(SIGUSR2) error");  
    sigemptyset(&zeromask);  
    sigemptyset(&newmask);  
    sigaddset(&newmask, SIGUSR1);  
    sigaddset(&newmask, SIGUSR2);  
      
    /* 
     * Block SIGUSR1 and SIGUSR2, and save current signal mask. 
     */
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)  
        err_sys("SIG_BLOCK error");  
}  
      
void
TELL_PARENT(pid_t pid)  
{  
    kill(pid, SIGUSR2);     /* tell parent we're done */
}  
      
void
WAIT_PARENT(void)  
{  
    while (sigflag == 0)  
        sigsuspend(&zeromask);  /* and wait for parent */
    sigflag = 0;  
      
    /* 
     * Reset signal mask to original value. 
     */
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)  
        err_sys("SIG_SETMASK error");  
}  
      
void
TELL_CHILD(pid_t pid)  
{  
    kill(pid, SIGUSR1);         /* tell child we're done */
}  
      
void
WAIT_CHILD(void)  
{  
    while (sigflag == 0)  
        sigsuspend(&zeromask);  /* and wait for child */
    sigflag = 0;  
      
    /* 
     * Reset signal mask to original value. 
     */
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)  
        err_sys("SIG_SETMASK error");  
}
Copyright © Linux教程網 All Rights Reserved