在两个子进程之间发送信号

时间:2016-10-04 17:25:03

标签: c

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

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

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

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

}

signalSender:

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

1 个答案:

答案 0 :(得分:0)

这里有同步问题。

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

您需要引入某种类型的同步方法。一种简单的方法是在调用sleep之前简要地使用第二个进程kill,以便为第一个进程提供启动的机会。同样,第一个进程应调用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);
    }
}