当前位置:天才代写 > tutorial > C语言/C++ 教程 > Linux下C编程:sigsuspend执行进程阐明

Linux下C编程:sigsuspend执行进程阐明

2017-11-02 08:00 星期四 所属: C语言/C++ 教程 浏览:697

用于在接管到某个信号之前,姑且用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后,历程就挂在哪里,期待着开放的信号的叫醒。系统在接管到信号后,顿时就把此刻的信号集还原为本来的,然后挪用处理惩罚函数。

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返回。

大抵就是上面这个进程,噢,本来signal handler是原子操纵的一部门,并且是在规复屏蔽字后执行的,所以上面的例子是没有问题的,Stevens说的也没错。由于Linux和Unix的千丝万缕的接洽,所以在两个平台上绝大部门的系统挪用的语义是一致的。上面的sigsuspend的原子操纵也是从《深入领略Linux内核》一书中推断出来的。书中的描写如下:

/*The sigsuspend( ) system call puts the process in the TASK_INTERRUPTIBLE state, after having blocked the standard signals specified    
 by a bit mask array to which the mask parameter points. The process will wake up only when a nonignored, nonblocked signal is 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;     
}

查察全套文章:http://www.bianceng.cn/Programming/C/201212/34807.htm

 

    关键字:

天才代写-代写联系方式