如何同时正确使用管道和信号?

时间:2019-01-28 01:05:28

标签: c unix pipe signals parent-child

我有2个孩子,我想从孩子向父母发送信号,并给出一个从父母到每个孩子的答案(随机数,为什么?为什么不...)。

我有此代码:

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

    #include <unistd.h>
    #include <string.h>

    #define WRITE(Str) (void)write(1,Str,strlen(Str))
    void handler(int signumber)
    {
        WRITE("Signal arrived\n");
    }

    int main(){
      /*random number */
      srand(time(NULL)); //random number
      int r = rand() % 2; //random number between 0 and 1 
      char original1[]="rdnr: ";
      original1[6]=r+'0';
      r = rand() % 2; 
      char original2[]="rdnr: ";
      original2[6]=r+'0';

      /*pipe, named pipe*/
        int pid,fd,fd2;
        printf("Fifo start!\n");
        int fid=mkfifo("fifo.ftc", S_IRUSR|S_IWUSR ); // creating named pipe file
        int fid2=mkfifo("fifo2.ftc", S_IRUSR|S_IWUSR );

        if (fid==-1)  //error handling
        {
          printf("Error at fid number: %i",errno);
          exit(EXIT_FAILURE);
        }
        if (fid2==-1)  //error handling
        {
          printf("Error at fid2 number: %i",errno);
          exit(EXIT_FAILURE);
        }
        printf("Pipe system OK!\n");

      /*signal*/
      sigset_t sigset;
      sigemptyset(&sigset); //empty signal set
      sigaddset(&sigset,SIGTERM); //SIGTERM is in set
      //sigfillset(&sigset); //each signal is in the set
      sigprocmask(SIG_BLOCK,&sigset,NULL); //signals in sigset will be blocked

      signal(SIGTERM,handler); //signal and handler is connetcted
      signal(SIGUSR1,handler); 

      pid_t child2;
      pid_t child=fork();
      if (child>0)
      {
        child2 = fork();
        if(child2>0){
          printf("Parent, wainting for signal...\n");

      sigsuspend(&sigset);
      sigprocmask(SIG_UNBLOCK,&sigset,NULL);
      int status;
      wait(&status);
      printf("Parent got 2 signal!\n");

      printf("Parent will send 2 random number: %s and %s\n", original1, original2);
      fd=open("fifo.ftc",O_WRONLY);
          write(fd,original1,12);
          close(fd);
      fd2=open("fifo2.ftc",O_WRONLY);
          write(fd2,original2,12);
          close(fd2);
      printf("Parent has sent the numbers!\n");
        }
        else{
          printf("I'm child2.\n");
          printf("child2 signalnr: %i (it is not blocked)\n", SIGUSR1);
          kill(getppid(),SIGUSR1);

          /*get pipe*/
          sleep(5);
          char s[1024]="nothn";  

          printf("got on pipe, in child2: %d!\n",fid);
          fd=open("fifo.ftc",O_RDONLY);
          read(fd,s,sizeof(s));
          printf("got this on pipe, by child2: %s \n",s);
          close(fd);
          // remove fifo.ftc
          unlink("fifo.ftc");
        }
      }
      else
      {
        printf("I'm child1.\n");
        printf("child1 signal nr: %i (it is blocked)\n",SIGTERM);
        sleep(3);
        kill(getppid(),SIGTERM);

        //sleep(5);
        /*get pipe*/
        char s[1024]="nothn";
        printf("Got this on pipe, by child1: %d!\n",fid2);
        fd2=open("fifo2.ftc",O_RDONLY);
        read(fd2,s,sizeof(s));
        printf("got this inpipe fd2: %s by child2 \n",s);
        close(fd2);
        // remove fifo2.ftc
        unlink("fifo2.ftc");
      }
      return 0;
    }

如果我分别使用它们,但在这里一起使用,则管道和信号正常工作。

我只有这个:

Fifo start!
Error at fid number: 17

通过错误处理程序。

如何同时正确使用管道和信号?

更新: 我已经重新考虑了评论部分中提到的父级,现在的输出是:

Fifo start!
Pipe system OK!
Parent, wainting for signal...
I'm child2.
I'm child1.
child1 signal nr: 15 (it is blocked)
child2 signalnr: 10 (it is not blocked)
Handled signal nr: 10
Got this on pipe, by child1: 0!
Handled signal nr: 15
got on pipe, in child2: 0!

所以,我所看到的问题是,子进程无法等待管道,我该如何解决呢?

UPDATE2:

如果我删除

int status;
wait(&status); 
从父级开始

工作正常,什么也没做,以确保第二个信号在管道事物之前到达。

1 个答案:

答案 0 :(得分:0)

您的代码中有一个重复的错误:

char original1[]="rdnr: ";
original1[6]=r+'0';

sizeof original1为7,索引6处的字符为字符串的零终止符。通过用r+'0'覆盖零终止符,您将破坏数组的字符串属性,从而无法在其上使用strlen

修复:

char original1[] = "rdnr: 0";
original1[sizeof original1 - 2] += r;