与信号进行过程同步

时间:2018-06-21 14:13:14

标签: c concurrency synchronization signals fork

如今,我正在努力并发地学习信号。但是,我在使用它们时遇到一些问题。在此程序中,我希望有两个进程(一个孩子-一个父进程),父进程分别向其子进程发送SIGHUPSIGINTSIGTSTP信号。我希望孩子以相同的顺序处理它们。我正在尝试在此处对信号进行同步。我有两个主要问题

  • 当父项先于子项运行时,它可以运行,但错过了 SIGINT,而不要进入其处理程序。
  • 当孩子首先在其父对象之前运行时,它将阻止。我希望在 无论哪种方式,程序都能正常运行。

如果您让我告知其他问题及其解决方案以及主要问题,我也非常感谢。

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


char const * hupMsg = "CHILD: I have received a SIGHUP\n";
int hupMsgLen;
void sighup(int x) {
    write(STDERR_FILENO, hupMsg, hupMsgLen);

}

char const * intMsg = "CHILD: I have received a SIGINT\n";
int intMsgLen;
void sigint(int x) {
    write(STDERR_FILENO, intMsg, intMsgLen);
}

char const * die = "CHILD: My DADDY has Killed me!!!\n";
int dieLen;
void sigtstp(int x) {
    write(STDERR_FILENO, die, dieLen);
    exit(111);     // <-- child dies with 111 exit code
}


char const * sigusr1Msg = "SIGUSR1 is being caught\n";
int sigusr1MsgLen;
void sigusr1(int x) {
    write(STDERR_FILENO, sigusr1Msg, sigusr1MsgLen);
}




int main()
{
    dieLen = strlen(die);
    sigusr1MsgLen = strlen(sigusr1Msg);
    hupMsgLen = strlen(hupMsg);
    intMsgLen = strlen(intMsg);

    int         pid;
    sigset_t    allBlockedMask, prevMask, parentUSR1Mask,
                sighupSet, sigintSet, sigtstpSet;

    sigfillset(&sighupSet);
    sigdelset(&sighupSet, SIGHUP);
    sigfillset(&sigintSet);
    sigdelset(&sigintSet, SIGINT);
    sigfillset(&sigtstpSet);
    sigdelset(&sigtstpSet, SIGTSTP);


    sigfillset(&allBlockedMask);

    sigfillset(&parentUSR1Mask);
    sigdelset(&parentUSR1Mask, SIGUSR1);


    struct sigaction saINT, saHUP, saTSTP;
    memset(&saINT, 0, sizeof(saINT));
    memset(&saHUP, 0, sizeof(saHUP));
    memset(&saTSTP, 0, sizeof(saTSTP));
    saINT.sa_flags = 0;
    saINT.sa_handler = sigint;
    saHUP.sa_flags = 0;
    saHUP.sa_handler = sighup;
    saTSTP.sa_flags = 0;
    saTSTP.sa_handler = sigtstp;



    struct sigaction saUSR1;
    memset(&saUSR1, 0, sizeof(saUSR1));
    saUSR1.sa_flags = 0;
    saUSR1.sa_handler = sigusr1;
    sigaction(SIGUSR1, &saUSR1, 0);



    /* get child process */

    if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }

    if (pid == 0) {   /* child */

        fprintf(stderr, "child pid %i\n", getpid());

        /* critical section to set signal handlers */
        sigprocmask(SIG_BLOCK, &allBlockedMask, &prevMask);
        fprintf(stderr, "child in blocking\n");

        /* set function calls */
        sigaction(SIGHUP, &saHUP, 0);
        sigaction(SIGINT, &saINT, 0);
        sigaction(SIGTSTP, &saTSTP, 0);

        /* let parent know, you can continue your process */
        kill(getppid(), SIGUSR1);

        fprintf(stderr, "child now UNblocked \n");
        sigprocmask(SIG_SETMASK, &prevMask, 0);

        /* here, I want to receive signals in turn */
        sigsuspend(&sighupSet);
        sigsuspend(&sigintSet);
        sigsuspend(&sigtstpSet);


    }
    else {
        /* parent */
        //sleep(2);
        fprintf(stderr, "parent pid %i\n", getpid());


        fprintf(stderr, "parent is waiting\n");
        sigsuspend(&parentUSR1Mask);
        fprintf(stderr, "parent is running\n");


        fprintf(stderr, "\nPARENT: sending SIGHUP\n");
        kill(pid, SIGHUP);

        fprintf(stderr, "\nPARENT: sending SIGINT\n");
        kill(pid, SIGINT);

        fprintf(stderr, "\nPARENT: sending SIGTSTP\n\n");
        kill(pid, SIGTSTP);

        wait(0);
    }

    return 0;
}

父项首次运行时的示例输出:

parent pid 4628
parent is waiting
child pid 4629
child in blocking
child now UNblocked 
SIGUSR1 is being caught
parent is running

PARENT: sending SIGHUP

PARENT: sending SIGINT

PARENT: sending SIGTSTP

CHILD: I have received a SIGHUP
CHILD: My DADDY has Killed me!!!

Process finished with exit code 0

子项第一次运行时的样本输出:

child pid 4657
child in blocking
child now UNblocked 
SIGUSR1 is being caught
parent pid 4656
parent is waiting
.
.
.

1 个答案:

答案 0 :(得分:2)

问题:

  • 如评论中所述,信号处理程序不得调用exit(),但可以调用_exit()

  • 您的信号处理程序不会阻塞信号。实际上,这可能会导致您遇到一些问题,因为信号处理程序本身可能会被信号打断。例如,在“父母先运行”的情况下,孩子的信号处理程序中的write()调用可能会被新信号中断。通过使用sa_mask对象的struct sigaction成员来解决此问题,可以指定在处理程序执行期间要阻止的信号掩码。

  • 正如您所观察到的,在交付给孩子的SIGUSR1的父级与父级给sigsuspend()的调用之间,您处于竞争状态。通过在派生之前在父级中而不是在子级中后级中的所有信号来解决此问题。直到调用SIGUSR1(带有允许它的掩码)之后,父级才会收到孩子的sigsuspend()。父级可以在sigsuspend()返回后恢复原始掩码。孩子可以继续恢复现在的信号屏蔽。