Question: Write a C program that creates a pipe and forks a child process. The parent then sendsthe following message on his side of the pipe

Write a C program that creates a pipe and forks a child process. The parent then sendsthe following message on his side of the pipe I am your daddy! and my name is , the child receives this message and prints it to its stdout verbatim.The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side of the pipe stating Daddy, my name is . The parent then writes the message received from the child to its stdout. Make sure the parentwaits on the child exit as to not creating orphans or zombies: creating such leftover willresult in zero grade.

I have this solution, is this correct?

#include #include #include #include pid_t child_pid; int cp_pipe[2];int pc_pipe[2]; void cleanup() { close(cp_pipe[0]); close(cp_pipe[1]); close(pc_pipe[0]); close(pc_pipe[1]); if(child_pid != 0) { while(wait(NULL) != -1); }} int main(int argc, char** argv) { char read_buffer[BUFSIZ]; if(atexit(cleanup) != 0) { exit(EXIT_FAILURE); } if(pipe(cp_pipe) != 0) { exit(EXIT_FAILURE); } if(pipe(pc_pipe) != 0) { exit(EXIT_FAILURE); } child_pid = fork(); if(child_pid == -1) { exit(EXIT_FAILURE); } else if(child_pid == 0) { close(pc_pipe[1]); close(cp_pipe[0]);

if(read(pc_pipe[0], read_buffer, BUFSIZ) == -1) { exit(EXIT_FAILURE); } printf("%s", read_buffer); dprintf(cp_pipe[1], "Daddy, my name is %d", getpid()); }

else { close(pc_pipe[0]); close(cp_pipe[1]); dprintf(pc_pipe[1], "I am your daddy! and my name is %d ", getpid()); if(read(cp_pipe[0], read_buffer, BUFSIZ) == -1) { exit(EXIT_FAILURE); } printf("%s", read_buffer); } exit(EXIT_SUCCESS);}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!