USE THE CODE GIVEN! Instructions: Write code that forks into two processes, a pa
ID: 3540784 • Letter: U
Question
USE THE CODE GIVEN!
Instructions:
Write code that forks into two processes, a parent process, and a child process.
The parent process will take the arguments to main() and send the characters in them one at a time to the child process through a pipe (one call to write for each character).
Do not send characters from argv[0] through the pipe. Start with argv[1] and continue through the rest of the arguments.
The child process will read the characters sent by the parent process one at a time, and count the number of characters it received from the parent. The child process should not use the arguments to main() in any way whatsoever. The child process will return the number of characters counted to the parent by returning normally.
The parent process will need to reap the child process to find out how many characters were counted by the child process.
It may be of use to know that read() will return immediately with a zero once the other end of the pipe is closed by the parent.
If I call your code this way:
The parent process should print out:
Important: printing should be done only by the parent process. The child process should not print anything.
Your output should be formatted exactly as shown (small differences in whitespace are OK). Remember to replace
Explanation / Answer
int main() { int i; printf("hello before fork "); printf("i : %d ",i); i=fork(); printf(" "); if(i==0) { printf("Child has started "); printf("child printing first time "); printf("getpid : %d getppid : %d ",getpid(),getppid()); sleep(5); printf(" child printing second time "); printf("getpid : %d getppid : %d ",getpid(),getppid()); } else { printf("parent has started "); printf("getpid : %d getppid : %d ",getpid(),getppid()); printf(" "); } printf("Hi after fork i : %d ",i); return 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.