Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the code in such a way that the child process asks for a string from the user. This string is sent using the pipe

Modify the code in such a way that the child process asks for a string from the user. This string is sent using the pipe to the Parent process and the parent process displays this string.

Code:

#include #include #include #include #define MSGSIZE 16 char* msg1 = "Hello World #1"; char* msg2 = "Hello World #2"; char* msg3 = "Hello World #3";

int main () { char inbuf [MSGSIZE]; int p[2], pid, nbytes = 12122121; // p[2] has 2 elements: p[0] and p[1] if (pipe(p) < 0) // creating a pipe here, with p as the list of IDs exit(1); printf("p[0] is =%d ", p[0]); // ID of the read end printf("p[0] is =%d ", p[1]); //ID of the write end /*continued */ if ((pid = fork())> 0) { write (p[1], msg1,MSGSIZE); write (p[1], msg2,MSGSIZE); write (p[1], msg3,MSGSIZE); printf("Parent has written to the Pipe "); close (p[1]); wait(NULL); } else { //Child Process close(p[1]);//CHILD only wants to read from the Pipe while ((nbytes = read (p[0], inbuf, MSGSIZE)) > 0) { printf ("%s , inbuf"); int x; // For Delay only for (int i=0; i<1000000000; i++){x++;} // For Delay only printf("nbytes = %d ",nbytes); } printf("nbytes = %d ",nbytes); if (nbytes != 0) exit (2); printf("Finished reading "); } 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_2

Step: 3

blur-text-image_3

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

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions