title: Calculating expression with multiple fork() children?
body: <p>Suppose we have expression <code>g=(a+b)*(c+d)-(e/f)</code> with hard-coded arbitrary numbers for variables. I would like to calculate this expression using multiple child processes in order to better understand how fork() works.</p>
  
  <p>My first attempt was to calculate <code>(a + b)</code> on child <strong>pid1</strong>, <code>(c + d)</code> on child <strong>pid2</strong>, <code>(e / f)</code> on child <strong>pid3</strong>, and then do summation &amp; subtraction in the <strong>parent</strong> process.</p>
  
  <p>Well, to my disappointment, <code>(a + b)</code> calculation done in the child process <strong>pid1</strong> did not affect <code>double expression1</code> variable in the parent process. I think the reason behind that - each <strong>fork()</strong> creates a separate memory space; as soon as a child process exits, all calculations done in that child process are gone.</p>
  
  <p>What do you usually do in a situation like this? I thought maybe I could nest fork() child process within a child process to calculate <code>(a + b)</code> first; <strong>wait</strong>; then <code>(c + d)</code>; <strong>wait</strong>; <code>(e / f)</code>; <strong>wait</strong>; <strong>the first child</strong> calculates the entire expression; child <strong>return(0)</strong>; <strong>parent</strong> terminates.</p>
  
  <p>But I think there's an easier solution to this problem, am I right?</p>