Sending signal from parent to child and vice-versa

那年仲夏 提交于 2019-12-12 04:18:34

问题


I am trying to practice signals and was trying to achieve below things

1) Child and parent prints 10 numbers and passes the baton to other

2) Parent/Child wait for there turn through sigsuspend

3) sigaction is just for heck of it to catch the signal

4) kill is used send the signal with respective process id

However the output is marred with race-conditions and I see once parent gets signal from child control is never handed back to child

Also I expected sigaction to catch the signal as well which doesnt seem to happen.

Can you please point what all I am doing wrong ?

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

static volatile int count = 0;

void sighandler(int sig)
{
    if (sig == SIGUSR1)
    {
        printf(" \n child sends parent signal - \n ");
    }

    if (sig == SIGUSR2)
    {
        printf("\n parent sends child signal - \n ");
    }
}

int main(void)
{
    //pid_t pid, cid;
    sigset_t block_csignal, block_psignal, empty_signal;
    struct sigaction ccatch, pcatch;

    setbuf(stdout, NULL);

    /* Creating signal set for suspending process till
       it receives below signal */
    sigfillset(&block_csignal);
    sigdelset(&block_csignal, SIGUSR2);

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

    /* Creating signal set for catching the signal
       and changing signal disposition */
    sigemptyset(&ccatch.sa_mask);   /* ccatch for catching signal from parent */
    ccatch.sa_flags = 0;
    ccatch.sa_handler = sighandler;

    sigemptyset(&pcatch.sa_mask);  /*  pcatch for catching signal from child */
    pcatch.sa_flags = 0;
    pcatch.sa_handler = sighandler;

    sigaction(SIGUSR2, &ccatch, NULL); /* catch signal from parent for child */
    sigaction(SIGUSR1, &pcatch, NULL); /* catch signal from child for parent */

    switch(fork())
    {
        case -1:
            printf("error in child creation \n ");
            exit(-1);
        case 0:
            printf(" \n Control in hand of child \n ");

            while(count < 50)
            {
                int temp = 0;
                printf(" \n c count ---  \n ");
                while (temp < 10)
                {
                    printf(" %d ", count);
                    temp++;
                    count++;
                }
                printf(" \n parent id in child process --- %d \n ", getppid());
                kill(getppid(), SIGUSR1);  /* send signal to parent */
                sigsuspend(&block_csignal); /* wait till you get signal from parent */

            }
            exit(1);
        default:
            printf("\n Control in hand of parent \n ");
            sigsuspend(&block_psignal); /*wait till you get signal from child*/
            printf("\n Control back in hand of parent \n ");
            while (count < 50)
            {
                int temp = 0;
                printf(" \n p count ---  \n ");
                while (temp < 10)
                {
                    printf(" %d ", count);
                    temp++;
                    count++;
                }
                kill(getpid(), SIGUSR2); /* send signal to child */
            }
            break;
    }

    printf("\n ");
    return EXIT_SUCCESS;
}

回答1:


In order to send a signal from parent to child, you need first to store the child's pid (it is the return value from a successful fork). In the parent code, you use getpid() which returns the id of the currently running process, hence the parent.

Try something like:

  int cid = fork();
  if(cid == 0) //child
  if(cid > 0){ // parent
    //... 
    kill(cid,... 
} 


来源:https://stackoverflow.com/questions/34969849/sending-signal-from-parent-to-child-and-vice-versa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!