Fork Pipe This assignment gives the programmer experience with forking to create another process. Pipes are used to comm
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Fork Pipe This assignment gives the programmer experience with forking to create another process. Pipes are used to comm
Pipes Create 2 pipes used for parent child communication that return a status variable. Use throws to check the return status to make sure the pipes were created without error. Create Another Process Fork Pipe Use fork(); Based on the pid returned by fork() determine if the process is the parent process or the child process. Processes Display all message activities as shown in the run sample at the end of this document. Parent Process As usual the process must close unnecessary part of pipes that are not used. Create a character array for the Parent Read Child Message to hold messages from the child. Design a loop that uses the command line argument number set at the program beginning to indicate how many Get Quote messages to send to the child. Inside the loop: Send using a write to the Parent Write Child Read pipe a Get Quote message to the child. Declare a character array of the correct size and clear it. Use a read of the Parent Read Child Write pipe to get a Quote message from the child. When the loop is done write to the Parent Write Child Read pipe an Exit message. Close the pipes the parent used. Display that the parent is done.
Child Process As usual the process must close unnecessary part of pipes that are not used. Seed a random number generator. Design an infinite loop. Inside the Loop: ● Read the received message from the parent. Determine what the message is: If the message is an Exit ● Leave the infinite loop. If the message is Get Quote Use random number generator to get a random quote line from the quote array. Declare and clear a char send array using correct size Copy to the send array from quote array. Write this quote to the parent. ● ● ● Fork Pipe If the message is invalid Submittal Display to the screen that the message is invalid and sent back to the parent Write this invalid message to the parent. Close the pipes the child used. Display to the screen a that the child is done. Activities File After done designing and testing, capture the run results in an activities text file named forkpipe.txt. Use the copy and paste to local editor text file for creating the forkpipe.txt file. Do not use script or terminal session to create the forkpipe.txt file. See the sample run below. Put in a zip folder that you name: forkpipe.cpp forkpipe.txt Submit what ever you called the .zip folder to blackboard.
Fork Pipe {cslinux1:~/courses/3377/assigns/03/forkpipe} {cslinux1:~/courses/3377/assigns/03/forkpipe} Where is the number argument, I will expire now... {cslinux1:~/courses/3377/assigns/03/forkpipe} ./forkpipe 2 g++ forkpipe.cpp -o forkpipe ./forkpipe In Parent: Write to pipe getQuoteMessage sent Message: Get Quote In Child: Read from pipe pipeParentWriteChildMessage read Message: Get Quote In Child Write to pipe pipeParentReadChildMessage sent Message: "Art is not what you see, but what you make others see." ~ Edgar Degas In Parent: Read from pipe pipeParentReadChildMessage read Message: "Art is not what you see, but what you make others see." ~ Edgar Degas In Parent: Write to pipe getQuoteMessage sent Message: Get Quote In Child: Read from pipe pipeParentWriteChildMessage read Message: Get Quote In Child Write to pipe pipe Parent ReadChildMessage sent Message: "No act of kindness, however small, is ever wasted" ~Aesop In Parent: Read from pipe pipeParentReadChildMessage read Message: "No act of kindness, however small, is ever wasted" ~Aesop In Parent: Write to pipe ParentWriteChild ExitMessage sent Message: Exit Parent Done In Child: Read from pipe pipeParentWriteChildMessage read Message: Exit Child Done {cslinux1:~/courses/3377/assigns/03/forkpipe}