Wednesday 15 September 2010

c - Why does this example of Forking not need mutex? -


My professor has a sample code that was hooked to work as a pipe. But how can she ensure that parents will run in front of the child without using a Mute X?

  zero runpip (int pfd []) {int pid; Switch (drink = fork) (case 0: / * hair * / duplex 2 (pfd [0], 0); Close (pfd [1]); / * The child does not need this closing of pipe * / execvp (cmd2 [0], cmd2); Perror (CMD2 [0]); Default: / * Guardian * / dup2 (PFD [1], 1); Close (pfd [0]); / * The parent does not need this closing of the pipe * / execvp (cmd1 [0], cmd1); Perror (cmd1 [0]); Case -1: Defect ("fork"); Exit (1); }}  

This will not usually make any difference whether a child or a parent Let's go.

If parents start writing execvp () before and pipe, then once the pipe is completed the writing stops, unless the children have some data Reads.

If the child starts reading execvp () before and with a pipe, it is read as long as the guardian does not write some data.

As a side note, parents will first run on the modern Linux kernel (e.g., 3.16.0) after , although they should not be dependent on it. (At the same time, the timeliness of parents may be right that after fork () , it shows that the child first runs.) Some 2.4 Kernels first ran the child.

This is because the child closes the end of the writing of the pipe and the end of the reading of the parent pipe is to ensure that only one file descriptor remains open for each end of the pipe is. (Remember that fork (2) duplicate file descriptor.) This means that the child will see the end of the file after the end of the writing end of the parent. If the child closes the reading end and parents try to write the pipe after that, then the parent will receive the SIGPIPE .


1 comment: