【问题标题】:sending signal between two child process在两个子进程之间发送信号
【发布时间】:2016-10-04 17:25:03
【问题描述】:

我在主程序中从父级创建了两个子级。第一个和第二个孩子在创建后执行一个程序(signalSender)(连同另一个孩子的 pid 作为参数)。 signalSender 具有信号处理程序,用于在进程之间发送信号。当第一个孩子执行 signalShooter 时,第二个孩子的 pid 作为参数给出为零。当第二个孩子执行 sigShooter 时,第一个孩子的 pid 作为参数给出。

1) 我想在第二个孩子向第一个孩子发送信号后通过信号处理程序找到第一个孩子的 pid。我试图将它保存到全局变量 pid_t pid2 但它不起作用。

2)我还必须在这两个孩子之间发送信号 100 次,但我不知道在哪里使用“for loop”和“wait”信号。

The main program:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>

int main()
{
    pid_t pid1,pid2,wid;
  char *my_args[5];
  int aInt = 368
     char str[15];
   pid1 = fork();
       if (pid1 < 0)
    {
      fprintf(stderr, ": fork failed: %s\n", strerror(errno));

      exit(1);

    }
    if(pid1 == 0)
    {
      my_args[0] = "sigperf1";
      my_args[1] = "0";
      my_args[2] = NULL;
      execv("signalSender",my_args);
      fprintf(stderr,"signalSender cannot be executed...");
      exit(-1);
    }

    pid2 = fork();

    if(pid2 < 0)
    {
       fprintf(stderr, ": fork failed: %s\n", strerror(errno));
       exit(1);
    }

    if(pid2 == 0)
    {
      sprintf(str, "%d", pid1);
      my_args[0] = "sigperf1";
      my_args[1] = str;
      my_args[2] = NULL; 
     // printf("this is converted = %s\n",my_args[1]);
      execv(“signalSender",my_args);
      fprintf(stderr,"signalSender cannot be executed...");
      exit(-1);
    }
wid = wait(NULL);

}

信号发送者:

#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>

pid_t pid2;
struct sigaction act;

void sighandler(int signum, siginfo_t *info, void *ptr)
{
    printf("Received signal %d\n", signum);
    printf("Signal originates from process %lu\n",
        (unsigned long)info->si_pid);
        pid2 = info->si_pid;
}

int main(int argc,char **argv)
{ 
    memset(&act, 0, sizeof(act));
    act.sa_sigaction = sighandler;
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGUSR1, &act, NULL);

    pid_t current, pidOther;
    current = getpid();
    pidOther = atol(argv[1]);

    if(pidOther != 0) // we are in the second child
    {
        kill(pidOther,SIGUSR1); //sending signal from second child to first 
    }

    if(pidOther == 0) // we are in the first child 
    {
        kill(pid2,SIGUSR1);
    }

    return 0;
}

【问题讨论】:

  • 我想你要找的函数是pause,或者sigsuspend

标签: c


【解决方案1】:

您在这里遇到了同步问题。

两个子进程大致同时启动。所以你无法预测哪个会先kill另一个。如果第一个子进程首先运行kill,它将传递0 作为pid,这将杀死进程组中的每个进程。此外,每个子进程在调用kill 后会立即退出,因此一个可能会在另一个有机会向其发送信号之前退出。

您需要引入某种类型的同步方法。一个简单的方法是在调用kill 之前让第二个进程sleep 让第一个进程有机会启动。同样,第一个进程应该调用pause,它会告诉它等待直到收到信号。

一旦你这样做了,那么每个进程就可以循环调用pausekill来来回回。

if(pidOther != 0) // we are in the second child
{
    sleep(1);    // wait for first child to start
    kill(pidOther,SIGUSR1); //sending signal from second child to first 
    for (i=0;i<5;i++) {
        pause();
        kill(pidOther,SIGUSR1);
    }
}

if(pidOther == 0) // we are in the first child 
{
    pause();   // wait until we get a signal from the second child
    kill(pid2,SIGUSR1);
    for (i=0;i<5;i++) {
        pause();
        kill(pid2,SIGUSR1);
    }
}

【讨论】:

  • 分析得很好,但是... 睡眠看起来像一个膏药。肯定需要确保保证同步?我不确定我们是否真的总体上回答了这个问题。
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多