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

标签 c

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

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

2)我还必须在这两个 child 之间发送信号 100 次,但我不知道在哪里使用“for 循环”和“等待”信号。

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

最佳答案

这里有一个同步问题。

两个子进程大致同时启动。所以您无法预测哪个会先杀死另一个。如果第一个 child 首先运行 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);
    }
}

关于c - 在两个子进程之间发送信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39858311/

相关文章:

c - 在 Arduino 中读取传感器时按下按钮

c - 运行以下文件处理 C 程序时,为什么输出不符合预期?

c - 获取监视器编号小部件在 gtk 2 中已启用

c - 查询代码意外修改数组而不是返回索引

mysql - 将 sprintf 与 mysql_query 结合使用

c++ - 双 10 小数点精度

c - C语言中的数组错误

c - C 中矩阵可以在结构体中工作吗?

c - 使用 libxml2 传递指针来搜索 xmlTree

c - 尝试乘以 2 个矩阵时出现段错误 11