Question
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
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
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