Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy aren't my processes running concurrently?
    text
    copied!<p>My questions are:</p> <p>1.) How can I get the parent process to always die last? I get that this isn't being accomplished because the parent is the first pid that gets ran, but I don't know how to change it.</p> <p>2.) How do I get my child processes to execute at the same time like his? I've even bumped the number up really high to see if it was just a coincidence, but it appears to not be.</p> <p>EDIT: SOLUTIONS</p> <p>1.) added wait(NULL) twice in inner Default<br> 2.) was happening. used sleep(1) to prove it.</p> <p>My code is as follows</p> <pre><code>#include &lt;stdio.h&gt; int main() { int pid, i; pid = fork(); switch(pid) { case -1: // error printf("Fork error"); break; case 0: // child process printf("First child is born, my pid is %d\n", getpid()); for(i=1; i&lt;10; i++) printf("First child executes iteration %d\n", i); printf("First child dies quietly.\n"); break; default: // parent process printf("Parent process is born, my pid is %d\n", getpid()); pid = fork(); switch(pid) { case -1: // error printf("Fork error"); break; case 0: // child process printf("Second child is born, my pid is %d\n", getpid()); for(i=1; i&lt;10; i++) printf("Second child executes iteration %d\n", i); printf("Second child dies quietly.\n"); break; default: // parent process printf("Parent process dies quietly."); } } return 0; } </code></pre> <p>My output always looks like this:</p> <pre> Parent process is born, my pid is 7847 First child is born, my pid is 7848 First child executes iteration: 1 First child executes iteration: 2 First child executes iteration: 3 First child executes iteration: 4 First child executes iteration: 5 First child executes iteration: 6 First child executes iteration: 7 First child executes iteration: 8 First child executes iteration: 9 First child executes iteration: 10 First child dies quietly. Parent process dies quietly. Second child is born, my pid is 7849 Second child executes iteration 1 Second child executes iteration 2 Second child executes iteration 3 Second child executes iteration 4 Second child executes iteration 5 Second child executes iteration 6 Second child executes iteration 7 Second child executes iteration 8 Second child executes iteration 9 Second child executes iteration 10 Second child dies quietly. </pre> <p>My assignment is:</p> <p>Write a C program ("procs.c") that creates three processes: a parent process that creates two child processes.</p> <p>The first child should do the following:</p> <ul> <li><p>display "First child is born, my pid is ..."</p></li> <li><p>display ten times the message "First child executes iteration X", where X is the number of the iteration</p></li> <li><p>display "First child dies quietly."</p></li> </ul> <p>The second child should do the following:</p> <ul> <li><p>display "Second child is born, my pid is ..."</p></li> <li><p>display ten times the message "Second child executes iteration X", where X is the number of the iteration</p></li> <li><p>display "Second child dies quietly."</p></li> </ul> <p>The parent process should do the following:</p> <ul> <li><p>display "Parent process is born, my pid is ..."</p></li> <li><p>create the first child</p></li> <li><p>create the second child</p></li> <li><p>display "Parent process dies quietly."</p></li> </ul> <p>Compile the program using gcc and name the executable "procs". Execute the program several times and notice how the output of the two children interlace.</p> <p>A possible output of this program is:</p> <pre> nova> ./procs Parent process is born, my pid is 7847 First child is born, my pid is 7848 First child executes iteration: 1 First child executes iteration: 2 First child executes iteration: 3 First child executes iteration: 4 First child executes iteration: 5 Second child is born, my pid is 7849 Second child executes iteration 1 Second child executes iteration 2 Second child executes iteration 3 First child executes iteration: 6 Second child executes iteration 4 Second child executes iteration 5 Second child executes iteration 6 First child executes iteration: 7 Second child executes iteration 7 Second child executes iteration 8 Second child executes iteration 9 Second child executes iteration 10 Second child dies quietly. First child executes iteration: 8 First child executes iteration: 9 First child executes iteration: 10 First child dies quietly. Parent process dies quietly. </pre>
 

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