parent.c
Code:
#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 pid;
int rv;
int commpipe[2]; /* This holds the fd for the input & output of the pipe */
char input[100];
FILE *inputfile;
FILE *outputfile;
inputfile = fopen("dec.txt", "r");
outputfile = fopen("bin.txt", "w");
if(inputfile == NULL) fprintf(stderr, "dec.txt NOT opened successfully\n");
else fprintf(stderr, "dec.txt opened successfully\n");
if(outputfile == NULL) fprintf(stderr, "bin.txt NOT created successfully\n");
else fprintf(stderr, "bin.txt created successfully\n");
/* Setup communication pipeline first */
if(pipe(commpipe)){
fprintf(stderr,"Pipe error!\n");
exit(1);
}
/* Attempt to fork and check for errors */
if( (pid=fork()) == -1){
fprintf(stderr,"Fork error. Exiting.\n"); /* something went wrong */
exit(1);
}
if(pid == 0){
dup2(commpipe[0], 0);
close(commpipe[1]);
if(execl("child","child",NULL) == -1){
fprintf(stderr,"execl Error!");
exit(1);
}
}
else{
/* A positive (non-negative) PID indicates the parent process */
dup2(commpipe[1],1); /* Replace stdout with out side of the pipe */
close(commpipe[0]); /* Close unused side of pipe (in side) */
setvbuf(stdout,(char*)NULL,_IONBF,0); /* Set non-buffered output on stdout */
while(fscanf(inputfile, "%s", &input) != EOF){
printf("%s\n", input);
}
close(commpipe[1]);
dup2(fileno(stdout), 1);
printf("Pipe closed\n");
}
wait(&rv); /* Wait for child process to end */
return 0;
}
child.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
void dectohex(int decimal, char *hex){
int index=0;
while(decimal){
hex[index] = (decimal%16) < 10 ? (decimal%16): 'A' + (decimal%16) - 10;
}
printf("%d\n", hex);
}
/*
*Will take a decimal number from the stantard input and
*output the hexidecimal version to hex.txt
*/
int main(int argc, char* argv[]){
char input[256];
char hex[256];
do{
if(fgets(input, sizeof(input), stdin) != NULL)
printf("Child got: %s", input);
}while(!feof(stdin));
return 0;
}
What I have here is the code from two processes.
The child is simple:
When it is run alone it will take input from the terminal and will re-output the input that was given separated by \n. It will stop running when EOF is encountered (user input ctrl D)
The Parent is a bit more complex:
The parent will get its input from a file "input.txt" "input.txt" will contain random words or numbers, one per line. The parent is forked, a child is created (child.c) which is then duped into taking its input from a pipe called commpipe.
The parent will feed the child input and the child will output what it was fed.
All of this is done well enough but what I need is a way for the child to be handed an EOF when the EOF of input.txt is encounter. What can I change in parent.c or child.c to get this.
Is there a way to just close the writing end of the pipe and have the child detect that the parent's writing end of the pipe is no longer open?