现在的位置: 首页 > 综合 > 正文

linux c 之sigsuspend 进程阻塞

2013年05月28日 ⁄ 综合 ⁄ 共 4386字 ⁄ 字号 评论关闭

函数原型:

  #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, then
sigsuspend() will
return after the signal-catching function returns, with the signal mask restored to the set that existed prior to the
sigsuspend() 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再返回。


  1. int main(void) {  
  2.    sigset_t   newmask, oldmask, zeromask;  
  3.   
  4.    if (signal(SIGINT, sig_int) == SIG_ERR)  
  5.       err_sys("signal(SIGINT) error");  
  6.   
  7.    sigemptyset(&zeromask);  
  8.   
  9.    sigemptyset(&newmask);  
  10.    sigaddset(&newmask, SIGINT);  
  11.    /* block SIGINT and save current signal mask */  
  12.    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)  
  13.       err_sys("SIG_BLOCK error");  
  14.   
  15.    /* critical region of code */  
  16.    pr_mask("in critical region: ");  
  17.   
  18.    /* allow all signals and pause */  
  19.    if (sigsuspend(&zeromask) != -1)  
  20.       err_sys("sigsuspend error");  
  21.    pr_mask("after return from sigsuspend: ");  
  22.   
  23.    /* reset signal mask which unblocks SIGINT */  
  24.    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)  
  25.       err_sys("SIG_SETMASK error");  
  26.   
  27.    /* and continue processing ... */  
  28.    exit(0);  
  29. }  
  30.   
  31. static void sig_int(int signo) {  
  32.    pr_mask("\nin sig_int: ");  
  33.    return;  
  34. }  



 
结果:

  1. $a.out  
  2. in critical region: SIGINT  
  3. ^C  
  4. in sig_int: SIGINT  
  5. after return from sigsuspend: SIGINT  


如果按照sig_handler先返回,那么SIGINT是不该被打印出来的,因为那时屏蔽字还没有恢复,所有信号都是不阻塞的。那么是Stevens说错了么?当然没有,只是Stevens没有说请在sigsuspend的原子操作中到底做了什么?
sigsuspend的整个原子操作过程为:
(1) 设置新的mask阻塞当前进程;
(2) 收到信号,恢复原先mask;
(3) 调用该进程设置的信号处理函数;
(4) 待信号处理函数返回后,sigsuspend返回。

大致就是上面这个过程,噢,原来signal handler是原子操作的一部分,而且是在恢复屏蔽字后执行的,所以上面的例子是没有问题的,Stevens说的也没错。由于Linux和Unix的千丝万缕的联系,所以在两个平台上绝大部分的系统调用的语义是一致的。上面的sigsuspend的原子操作也是从《深入理解Linux内核》一书中揣度出来的。书中的描述如下:

  1. /*The sigsuspend( ) system call puts the process in the TASK_INTERRUPTIBLE state, after having blocked the standard signals specified 
  2.  by a bit mask array to which the mask parameter points. The process will wake up only when a nonignored, nonblocked signal is sent  
  3.  to it. The corresponding sys_sigsuspend( ) service routine executes these statements: 
  4. */  
  5.   
  6. mask &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));  
  7. spin_lock_irq(¤t->sigmask_lock);  
  8. saveset = current->blocked;  
  9. siginitset(¤t->blocked, mask);  
  10. recalc_sigpending(current);  
  11. spin_unlock_irq(¤t->sigmask_lock);  
  12. regs->eax = -EINTR;  
  13. while (1) {  
  14.     current->state = TASK_INTERRUPTIBLE;  
  15.     schedule(  );  
  16.     if (do_signal(regs, &saveset))  
  17.         return -EINTR;  
  18. }   




int sigsuspend(const sigset_t *sigmask);

此函数用于进程的挂起,sigmask指向一个信号集。当此函数被调用时,sigmask所指向的信号集中的信号将赋值给信号掩码。之后进程挂起。直到进程捕捉到信号,并调用处理函数返回时,函数sigsuspend返回。信号掩码恢复为信号调用前的值,同时将errno设为EINTR。进程结束信号可将其立即停止。

 

??

  1. #include <stdio.h>  
  2. #include <signal.h>  
  3.   
  4. void checkset();  
  5. void func();  
  6. void main()  
  7. {  
  8.      sigset_tblockset,oldblockset,zeroset,pendmask;  
  9.      printf("pid:%ld\n",(long)getpid());  
  10.      signal(SIGINT,func);  
  11.   
  12.      sigemptyset(&blockset);  
  13.      sigemptyset(&zeroset);  
  14.      sigaddset(&blockset,SIGINT);  
  15.   
  16.      sigprocmask(SIG_SETMASK,&blockset,&oldblockset);  
  17.      checkset();  
  18.      sigpending(&pendmask);  
  19.   

抱歉!评论已关闭.