父进程如何通过子进程验证发送的信号

时间:2011-07-18 20:26:26

标签: c linux multiprocessing ubuntu-10.04

基于此http://man7.org/tlpi/code/online/dist/procexec/fork_sig_sync.c.html

/* fork_sig_sync.c

   Demonstrate how signals can be used to synchronize the actions
   of a parent and child process.
*/
#include <signal.h>
#include "curr_time.h"                  /* Declaration of currTime() */
#include "tlpi_hdr.h"

#define SYNC_SIG SIGUSR1                /* Synchronization signal */

static void             /* Signal handler - does nothing but return */
handler(int sig)
{
}

int
main(int argc, char *argv[])
{
    pid_t childPid;
    sigset_t blockMask, origMask, emptyMask;
    struct sigaction sa;

    setbuf(stdout, NULL);               /* Disable buffering of stdout */

    sigemptyset(&blockMask);
    sigaddset(&blockMask, SYNC_SIG);    /* Block signal */
    if (sigprocmask(SIG_BLOCK, &blockMask, &origMask) == -1)
        errExit("sigprocmask");

    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    sa.sa_handler = handler;
    if (sigaction(SYNC_SIG, &sa, NULL) == -1)
        errExit("sigaction");

    switch (childPid = fork()) {
    case -1:
        errExit("fork");

    case 0: /* Child */

        /* Child does some required action here... */

        printf("[%s %ld] Child started - doing some work\n",
                currTime("%T"), (long) getpid());
        sleep(2);               /* Simulate time spent doing some work */

        /* And then signals parent that it's done */

        printf("[%s %ld] Child about to signal parent\n",
                currTime("%T"), (long) getpid());
        if (kill(getppid(), SYNC_SIG) == -1)
            errExit("kill");

        /* Now child can do other things... */

        _exit(EXIT_SUCCESS);

    default: /* Parent */

        /* Parent may do some work here, and then waits for child to
           complete the required action */

        printf("[%s %ld] Parent about to wait for signal\n",
                currTime("%T"), (long) getpid());
        sigemptyset(&emptyMask);
        if (sigsuspend(&emptyMask) == -1 && errno != EINTR)  // <<<<< Question
            errExit("sigsuspend");
        printf("[%s %ld] Parent got signal\n", currTime("%T"), (long) getpid());

        /* If required, return signal mask to its original state */

        if (sigprocmask(SIG_SETMASK, &origMask, NULL) == -1)
            errExit("sigprocmask");

        /* Parent carries on to do other things... */

        exit(EXIT_SUCCESS);
    }
}

问题

当父进程调用 sigsuspend 时,为什么它不验证发送的信号是SYNC_SIG?

  

http://pubs.opengroup.org/onlinepubs/7908799/xsh/sigsuspend.html

1 个答案:

答案 0 :(得分:0)

代码假定程序运行时不会发生其他信号,所以它不会检查它是否收到正确的信号。这意味着如果发生任何其他信号,程序可能会失败。

添加

有很多方法可以检查是否发送了正确的信号。您可以让处理程序设置一些sig_atomic_t全局变量,并在sigsuspend之后检查其设置是否返回(如果不是则循环重复sigsuspend调用)。或者您可以将传递给sigsuspend的掩码设置为阻止除SYNC_SIG之外的所有内容。如果其他进程向您的进程发送虚假的SIGUSR1,这些都不会起作用,但只有在有人故意试图破坏您的程序时才会发生这种情况。