从父母向孩子发送信号,反之亦然 [英] Sending signal from parent to child and vice-versa

查看:48
本文介绍了从父母向孩子发送信号,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试练习信号,并试图实现以下目标

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

1)父母双方打印出10个数字,然后将指挥棒传递给其他人

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

2)父母/孩子等待通过sigsuspend的转弯

2) Parent/Child wait for there turn through sigsuspend

3)信号捕获只是为了捕获信号

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

4)kill用于发送带有相应进程ID的信号

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;
}

推荐答案

要从父级向子级发送信号,您需要首先存储子级的pid(这是成功派生的返回值).在父代代码中,您使用getpid(),它返回当前正在运行的进程的ID,因此返回父代.

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.

尝试类似的东西:

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

这篇关于从父母向孩子发送信号,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆