I want to make this code generate 1 parent and 2 child processesCode:#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <string.h> /* *Decimal number is taken in through standard input and *output to a file called bin.txt */ int main(int argc, char* argv[]){ pid_t pid1, pid2; int rv; int D2Bcommpipe[2]; /* This holds the fd for the input & output of the pipe */ int D2Hcommpipe[2]; char input[32] = "00"; FILE *inputfile; FILE *outputfile; inputfile = fopen("dec.txt", "r"); outputfile = fopen("bin.txt", "w"); if(inputfile == NULL) fprintf(stderr, "dec.dat NOT opened successfully\n"); else fprintf(stderr, "dec.dat opened successfully\n"); if(outputfile == NULL) fprintf(stderr, "bin.dat NOT created successfully\n"); else fprintf(stderr, "bin.dat created successfully\n"); /* Setup communication pipeline first */ if(pipe(D2Bcommpipe)){ fprintf(stderr,"Pipe not opened. terminating...\n"); exit(1); } if(pipe(D2Hcommpipe)){ fprintf(stderr, "Pipe not opened. terminating...\n"); exit(1); } /* Attempt to fork and check for errors */ if( (pid1=fork()) == -1){ fprintf(stderr,"Fork error. terminating....\n"); /* something went wrong */ exit(1); } /*pid1 Child Process*/ if(pid1 == 0){ dup2(D2Bcommpipe[0], 0); //Dup child into reading stdin from pipe close(D2Bcommpipe[1]); dup2(D2Hcommpipe[1], 1); //Dup Child into piping its stdout close(D2Hcommpipe[0]); setvbuf(stdout, (char*) NULL, _IONBF, 0); /* Set non-buffered output on stdout */ if(execl("dectohex","dectohex",NULL) == -1){ fprintf(stderr,"dectohex NOT found by execlp, terminating...\n"); exit(1); } } /*********************** *pid1 Parent process ***********************/ else{ if((pid2=fork()) == -1){ fprintf(stderr,"Fork error. terminating....\n"); /* something went wrong */ exit(1); } /*pid2 child Process*/ if(pid2 == 0){ dup2(D2Hcommpipe[0], 0); close(D2Hcommpipe[1]); if(execl("hextobin", "hextobin", NULL) == -1){ fprintf(stderr, "hextobin NOT found by execlp, terminating...\n"); } } /*********************** *pid2 Parent process ***********************/ else{ dup2(D2Bcommpipe[1],1); close(D2Bcommpipe[0]); setvbuf(stdout, (char*) NULL, _IONBF, 0); /* Set non-buffered output on stdout */ while(fscanf(inputfile, "%s", &input) != EOF){ printf("%s\n", input); memset(input, 0, 32); } close(D2Bcommpipe[1]); close(1); } //wait(&rv); /* Wait for child process to end */ if(!WIFEXITED(rv)) fprintf(stderr, "Message for dectobin.c child process not found\n"); else if(WEXITSTATUS(rv)) fprintf(stderr, "One or more errors in child process of dectobin.c\n"); } return 0; }
the parent will push its stdout through pipe1 into the process dectohex.c
dectohex.c will be duped into reading stdin via pipe 1 and pushings its stdout through pipe2
hextobin.c will be duped into reading stdin via pipe 2 and then push its stdout through pipe3 where the parent will finally read from pipe 3 and get what it wants.
In general, I am converting a number from decimal to binary by first piping the number to a process that converts it to hex then piping to process that converts the hex to binary then piping that number BACK to the parent process.
Problem: I can pipe from the parent to child1 just fine but when I try to pipe from child1 to child2 and have child2 just output to the terminal I get nothing. I think there is some fluke in the way I am forking my processes and duping the children. Would anyone care to take a look?
Also, any tips on better practices I could be using in this code are appreciated.