Question
Fix the following C code to make the parent process results from the child and add filtering/aggregation. #include #include #include #include #include int main(int argc,
Fix the following C code to make the parent process results from the child and add filtering/aggregation.
#include
#include
#include
#include
#include
int main(int argc, char * argv[]) {
int pipe1[2];
pid_t childPID;
char buffer;
if (argc != 2) {
fprintf(stderr, "Usage: %s ", argv[0]);
exit(EXIT_FAILURE);
}
if (pipe(pipe1) == -1) {
fprintf(stderr, "%s", "pipe has failed. ");
exit(EXIT_FAILURE);
}
childPID = fork();
if (childPID == -1) {
fprintf(stderr, "%s", "fork has failed. ");
exit(EXIT_FAILURE);
}
if (childPID == 0) {
printf("I am the child. ");
close(pipe1[1]);
printf("Child is about to read from the pipe. ");
while (read(pipe1[0], &buffer, 1) > 0)
write(STDOUT_FILENO, &buffer, 1);
write(STDOUT_FILENO, " ", 1);
close(pipe1[0]);
printf("Child has echoed from the pipe to standard output. ");
exit(EXIT_SUCCESS);
}
else { // Parent writes argv[1] to pipe
printf("I am the parent. ");
close(pipe1[0]);
write(pipe1[1], argv[1], strlen(argv[1]));
close(pipe1[1]);
printf("Parent has written data into the pipe. ");
printf("Parent will wait for child to terminate. ");
wait(NULL); // Parent waiting for child to terminate
exit(EXIT_SUCCESS);
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started