Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743692
  • 博文数量: 438
  • 博客积分: 9799
  • 博客等级: 中将
  • 技术积分: 6092
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 17:25
文章分类

全部博文(438)

文章存档

2019年(1)

2013年(8)

2012年(429)

分类: 系统运维

2012-03-30 19:50:59

我们已经看到我们如何改变一个进程的信号掩码来阻塞或反阻塞选定的信号。我们可以使用这个技术来保护我们不想被一个信号中断的代码关键区域。如果我 们想反阻塞一个信号然后pause,等待前一个被阻塞的信号发生,那么会发生什么呢?假定信号是SIGINT,这样做的不正确的做法是:


sigset_t  newmask, oldmask;

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 */

/* reset signal mask, which unblocks SIGINT */
if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
  err_sys("SIG_SETMASK error");

/* window is open */
pause();  /* wait for signal to occur */

/*continue processing */


如 果信号在进程被阻塞时被发送给它,那么信号分发将被延迟,直到信号被反阻塞。对于这个应用,这可以看成好像信号发生在反阻塞和pause之间(取决于内核 如何实现信号)。如果是这样,也就是说信号确实在反阻塞和pause之间发生,那么我们会有问题。在这个时间间隙里信号的任何发生都会丢失,我们可能不再 看到这个信号,这种情况下pause会无尽地阻塞。这是早期不可靠信号的另一个问题。


为了修正这个问题,我们需要在单个原子操作里同时重置信号掩码和催眠进程的一个方法。这个特性由sigsuspend函数提供。



  1. #include <signal.h>

  2. int sigsuspend(const sigset_t *sigmask);

  3. 错误返回-1,errno被设为EINTR。


进程的信号掩码被设为由sigmask所指的值。然后进程被挂起,直到一个信号被捕获,或一个终止进程的信号发生。如果一个信号被捕获且信号处理器返回,那么sigsuspend返回,而且进程的信号掩码在sigsuspend调用之前设为它的值。


注意这个函数没有成功返回。如果它返回到调用者,它总是返回-1,errno设置为EINTR(表示一个中断的系统调用)。


下面的代码展示了保护代码的一个临界区域免受一个特定信号的正确方法:


注意当sigsuspend返回时,它在调用前设置信号掩码为它的值。在这个例子里,SIGINT信号将被阻塞。因此我们把信号掩码重置为我们早先存储的值(oldmask)。



  1. #include <signal.h>

  2. void pr_mask(const char *str);
  3. static void sig_int(int);

  4. int
  5. main(void)
  6. {
  7.     sigset_t newmask, oldmask, waitmask;

  8.     pr_mask("program start: ");

  9.     if (signal(SIGINT, sig_int) == SIG_ERR) {
  10.         printf("signal(SIGINT) error\n");
  11.         exit(1);
  12.     }
  13.     sigemptyset(&waitmask);
  14.     sigaddset(&waitmask, SIGUSR1);
  15.     sigemptyset(&newmask);
  16.     sigaddset(&newmask, SIGINT);

  17.     /*
  18.      * Block SIGINT and save current signal mask.
  19.      */
  20.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
  21.         printf("SIG_BLOCK error\n");
  22.         exit(1);
  23.     }

  24.     /*
  25.      * Critical region of code.
  26.      */
  27.     pr_mask("in critical region: ");

  28.     /*
  29.      * Pause, allowing all signals except SIGUSR1.
  30.      */
  31.     if (sigsuspend(&waitmask) != -1) {
  32.         printf("sigsuspend error\n");
  33.         exit(1);
  34.     }

  35.     pr_mask("after return from sigsuspend: ");
  36.     /*
  37.      * Reset signal mask which unblocks SIGINT.
  38.      */
  39.     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
  40.         printf("SIG_SETMASK error\n");
  41.         exit(1);
  42.     }
  43.     /*
  44.      * And continue processing...
  45.      */
  46.     pr_mask("program exit: ");

  47.     exit(0);
  48. }

  49. static void
  50. sig_int(int signo)
  51. {
  52.     pr_mask("\nin sig_int: ");
  53. }


程序的输出:
$ ./a.out
program start:
in critical region: SIGINT
^C
in sig_int: SIGINT SIGUSR1 (这里的SIGINT是因为信号处理器捕获到SIGINT而设的,而不是从sigprocmask函数里设置而来的。)
after return from sigsuspend: SIGINT
program exit:


我们在调用sigsuspend时把SIGUSR1加到安装的掩码里,以便当信号处理器运行时,我们可以知道掩码确实改变了。我们可以看到当sigsuspend返回时,它把信号掩码回复到调用之前的值。

sigsuspend的另一个用法是等待一个信号处理器来设置一个全局变量。在下面的代码,我们捕获中断信号和退出信号,但想只当退出信号被捕获时唤醒主例程:



  1. #include <signal.h>

  2. volatile sig_atomic_t quitflag; /* set nonzero by signal handler */

  3. static void
  4. sig_int(int signo) /* one signal handler for SIGINT and SIGQUIT */
  5. {
  6.     if (signo == SIGINT)
  7.         printf("\ninterrupt\n");
  8.     else if (signo == SIGQUIT)
  9.         quitflag = 1; /* set flag for main loop */
  10. }

  11. int
  12. main(void)
  13. {
  14.     sigset_t newmask, oldmask, zeromask;

  15.     if (signal(SIGINT, sig_int) == SIG_ERR) {
  16.         printf("signal(SIGINT) error\n");
  17.         exit(1);
  18.     }
  19.     if (signal(SIGQUIT, sig_int) == SIG_ERR) {
  20.         printf("signal(SIGQUIT) error\n");
  21.         exit(1);
  22.     }

  23.     sigemptyset(&zeromask);
  24.     sigemptyset(&newmask);
  25.     sigaddset(&newmask, SIGQUIT);

  26.     /*
  27.      * Block SIGQUIT and save current signal mask.
  28.      */
  29.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
  30.         printf("SIG_BLOCK error\n");
  31.         exit(1);
  32.     }

  33.     while (quitflag == 0)
  34.         sigsuspend(&zeromask);

  35.     /*
  36.      * SIGQUIT has been caught and is now blocked; do whatever.
  37.      */
  38.     quitflag = 0;

  39.     /*
  40.      * Reset signal mask which unblocks SIGQUIT.
  41.      */
  42.     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
  43.         printf("SIG_SETMASK error\n");
  44.         exit(1);
  45.     }

  46.     exit(0);
  47. }

程序运行结果:
$ ./a.out
^C
interrupt
^C
interrupt
^C
interrupt
^C
interrupt
^\$

为 了支持ISO C的非POSIX和POSIX.1系统之间的可移植性,我们在一个信号处理器里应该做的唯一的事是给sig_atomic_t类型的变量赋一个值,没有别 的了。POSIX.1走得更远并指定了一列函数,它们可以被一个信号处理器安全调用,但是如果我们这样做,我们的代码可能在非POSIX系统不能正确运行。


作为另一个信号的例子,我们展示信号如何被用来同步一个父进程和子进程。下面的代码展示了8.9节的5个例程TELL_WAIT、TELL_PARENT、TELL_CHILD、WAIT_PARENT和WAIT_CHILD。



  1. #include <signal.h>
  2. #include <stdio.h>

  3. static volatile sig_atomic_t sigflag; /* set nonzero by sig handler */
  4. static sigset_t newmask, oldmask, zeromask;

  5. static void
  6. sig_usr(int signo) /* one signal handler for SIGUSR1 and SIGUSR2 */
  7. {
  8.     sigflag = 1;
  9. }

  10. void
  11. TELL_WAIT(void)
  12. {
  13.     if (signal(SIGUSR1, sig_usr) == SIG_ERR) {
  14.         printf("signal(SIGUSR1) error\n");
  15.         exit(1);
  16.     }
  17.     if (signal(SIGUSR2, sig_usr) == SIG_ERR) {
  18.         printf("signal(SIGUSR2) error\n");
  19.         exit(1);
  20.     }
  21.     sigemptyset(&zeromask);
  22.     sigemptyset(&newmask);
  23.     sigaddset(&newmask, SIGUSR1);
  24.     sigaddset(&newmask, SIGUSR2);

  25.     /*
  26.      * Block SIGUSR1 and SIGUSR2, and save current signal mask.
  27.      */
  28.     if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0) {
  29.         printf("SIG_BLOCK error\n");
  30.         exit(1);
  31.     }
  32. }

  33. void
  34. TELL_PARENT(pid_t pid)
  35. {
  36.     kill(pid, SIGUSR2); /* tell parent we're done */
  37. }

  38. void
  39. WAIT_PARENT(void)
  40. {
  41.     while (sigflag == 0)
  42.         sigsuspend(&zeromask); /* and wait for parent */
  43.     sigflag = 0;

  44.     /*
  45.      * Reset signal mask to original value.
  46.      */
  47.     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
  48.         printf("SIG_SETMASK error\n");
  49.         exit(1);
  50.     }
  51. }

  52. void
  53. TELL_CHILD(pid_t pid)
  54. {
  55.     kill(pid, SIGUSR1); /* tell child we're done */
  56. }

  57. void
  58. WAIT_CHILD(void)
  59. {
  60.     while (sigflag == 0)
  61.         sigsuspend(&zeromask); /* and wait for child */
  62.     sigflag = 0;

  63.     /*
  64.      * Reset signal mask to original value.
  65.      */
  66.     if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0) {
  67.         printf("SIG_SETMASK error\n");
  68.         exit(1);
  69.     }
  70. }

如果我们想在等待一个信号发生时睡眠,sigsuspend函数是好的(正如我们在前两个例子里展示的那样),但是如果我们在等待时想要调用其它系统函数会如何呢?不幸的是,这个问题没有完美的解决方法,除非我们使用多线程并让一个单独的线程处理信号(12.8节)。

没有线程的话,我们可以做的最好的方式是当信号发生时在信号处理器里设置一个全局变量。例如,如果我们捕获SIGINT和SIGALRM并使用 signal_intr函数安装信号处理器,那么信号将会中断任何阻塞的系统调用。系统很有可能在我们阻塞在一个select函数的调用时发生 (14.5.1节),它从一个慢的设备等待输入。(对于SIGALRM来说尤其是这样,因为我们设置闹钟来避免永远等待输入。)处理这个的代码看起来类似 于:


if (intr_flag)  /* flag set by our SIGINT handler */
  handle_intr();
if (alrm_flag)  /* flag set by our SIGALRM handler */
  handle_alrm();

/* signals occuring in here are lost */

while (select(...) <0) {
  if (errno == EINTR) {
    if (alrm_flag)
      handle_alrm();
    else if (intr_flag)
      handle_intr();
  } else {
    /* some other error */
  }
}


我 们在调用select之前和如果select返回一个中断系统调用错误时测试每个全局标志。如果信号在前两个if语句和后面的select调用之间被捕获 时发出现问题。出现在这里的信号会被丢失,正如代码注释指明的一样。信号处理器被调用,它们设置恰当的全局变量,但是select从不返回(除非一些数据被准备好读。)


我们想要做的事是下面的步骤:


1、阻塞SIGINT和SIGALRM;


2、测试这两个全局变量来看是否有信号发生,如果有,处理这种情况;


3、调用select(或者任何其它的系统函数,比如read)并反阻塞这两个信号,作为一个原子操作;


sigsuspend函数只当步骤3为pause操作时对我们有用。

阅读(1345) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~