Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>The appropriate parent process should wait (using <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html" rel="nofollow"><code>wait()</code> or <code>waitpid()</code></a> or a platform-specific variant) for its children to die before exiting.</p></li> <li><p>Concurrent execution requires the scheduler to get a chance to run. You can force the issue with appropriate sleep (micro-sleep, <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html" rel="nofollow">nano-sleep</a>) operations. Some system calls will help (so <code>fflush(0)</code> might help).</p></li> </ol> <hr> <pre><code>#include &lt;stdio.h&gt; #include &lt;sys/wait.h&gt; #include &lt;time.h&gt; #include &lt;unistd.h&gt; int main(void) { int pid, i; struct timespec tw = { .tv_sec = 0, .tv_nsec = 10000000 }; pid = fork(); switch(pid) { case -1: printf("Fork error"); break; case 0: 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); nanosleep(&amp;tw, 0); } printf("First child dies quietly.\n"); break; default: printf("Parent process is born, my pid is %d\n", getpid()); pid = fork(); switch(pid) { case -1: printf("Fork error"); break; case 0: 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); nanosleep(&amp;tw, 0); } printf("Second child dies quietly.\n"); break; default: printf("Parent process waiting for children.\n"); int corpse; int status; while ((corpse = waitpid(0, &amp;status, 0)) &gt; 0) printf("Child %d died with exit status 0x%.4X\n", corpse, status); printf("Parent process dies quietly.\n"); break; } } return 0; } </code></pre> <p>Example output:</p> <pre><code>Parent process is born, my pid is 46624 First child is born, my pid is 46625 First child executes iteration 1 Parent process waiting for children. Second child is born, my pid is 46626 Second child executes iteration 1 First child executes iteration 2 Second child executes iteration 2 First child executes iteration 3 Second child executes iteration 3 First child executes iteration 4 Second child executes iteration 4 Second child executes iteration 5 First child executes iteration 5 Second child executes iteration 6 First child executes iteration 6 Second child executes iteration 7 First child executes iteration 7 Second child executes iteration 8 First child executes iteration 8 Second child executes iteration 9 First child executes iteration 9 First child dies quietly. Second child dies quietly. Child 46625 died with exit status 0x0000 Child 46626 died with exit status 0x0000 Parent process dies quietly. </code></pre> <p>Note that the 10 millisecond delays almost force alternating execution. Without something similar, you are stuck with the idiosyncrasies of your system and its scheduler, and many a modern machine is just too fast!</p>
 

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