How to use pipes and signals correctly at the same

2019-07-18 16:54发布

I have 2 children, and I want to send signals from children to parent, and an answer (random number, why? why not...) named pipe from the parent to each child.

I have this code:

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

And the pipes, and the signals work correctly if I use them separately, but together, here, not.

I got only this:

Fifo start!
Error at fid number: 17

by error handlers.

How can I use correctly the pipes and the signals at the same time?

UPDATE: I've rethinked the parent as was mentioned in the comment section, now the output, what I can see is:

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!

So, the problem as I see is, the child process can't wait for the pipes, how can I solve this?

UPDATE2:

If I delete

int status;
wait(&status); 

from the parent then works fine, just nothing is the to ensure the second signal arrives before the pipe things.

1条回答
做自己的国王
2楼-- · 2019-07-18 17:46

One repeating error in your code:

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

sizeof original1 is 7, the character at index 6 is the zero terminator of the string. By overwriting the zero terminator with r+'0' you destroy the string property of the array, so that you can no longer use strlen on it.

Fix:

char original1[] = "rdnr: 0";
original1[sizeof original1 - 2] += r;
查看更多
登录 后发表回答