Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

1. What causes musculoskeletal pain?

Answered: 1 week ago

Question

Write Hund's rule?

Answered: 1 week ago