Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You cannot do this in this manner. Fork will create a new process, which will copy (or copy-on-write) the memory pages into the new process. That means that each of your child processes will get their own copy of "k", and each copy of "k" will only get incremented once.</p> <p>To have all the processes communicate back to the "same" k variable, you need to perform some sort of interprocess communications. </p> <p>Examples of good inter-process communications:</p> <ol> <li><p>Have the parent process create a shared memory segment which stores the value of k. Have the children processes wait for an exclusive grab of the shared memory segment (via a parent process created mutex). When the child has the exclusive lock, have the child read the value of k and store the value of k + 1.</p></li> <li><p>Create a per-process pipe between the parent an children processes. Have the parent read the pipe for a message indicating the desire to increment k. Have the parent process then increment k on the child's behalf.</p></li> </ol> <p>Examples of poor inter-process communications:</p> <ol> <li>Any solution which fails to ensure that changes in K are atomic (meaning that two contending children might both increment K to the same value). Such a lack of care would result in a value of K that appears to be incremented fewer times than the number of children processes, as the set values might look like (2, 3, 3, 4, 5). This means that file i/o is mostly useless, unless you create a framework around it that ensures atomic operations in locking the file for exclusive access.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload