如何同时正确使用管道和信号? [英] How to use pipes and signals correctly at the same time?

查看:84
本文介绍了如何同时正确使用管道和信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有此代码:

  #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.

我只有这个:

Fifo start!
Error at fid number: 17

通过错误处理程序.

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

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:

如果我删除

int status;
wait(&status); 

然后,来自父级的

可以正常工作,只是什么也无法确保第二个信号在管道事物之前到达.

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

推荐答案

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

One repeating error in your code:

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

sizeof original1为7,索引6处的字符为字符串的零终止符.通过用r+'0'覆盖零终止符,您将破坏数组的字符串属性,以便不再可以在其上使用strlen.

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.

修复:

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

这篇关于如何同时正确使用管道和信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆