Как добавить третий дочерний процесс?

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
 
int main() {
  int ps[2];
  int pid[2];
  if(pipe(ps)!=0) {
    printf("pipe error\n");
    return 1;
  }
 
  if((pid[0]=fork())==0) {
    close(ps[0]);
    dup2(ps[1],1);
    close(ps[1]);
    puts("Hello, World");
    exit(0);
  }
if((pid[1]=fork())==0) {
    int i;
    char bf[120]="";
    close(ps[1]);
    dup2(ps[0],0);
    close(ps[0]);
    for(i=0;(bf[i]=getchar())!=EOF;i++);
    bf[i-1]='\0';
    printf("from %d: read %d '%i'\n",getpid(),strlen(bf),bf);
    exit(0);
  }
 
  close(ps[0]);
  close(ps[1]);
  waitpid(pid[0],NULL,0);
  waitpid(pid[1],NULL,0);
  return 0;
}

Ответы (0 шт):