Parallel processing and synchronization using semaphores in C
<h3>Questions:</h3>
<ol>
<li>Are my processes running in parallel?
<em>I want six processes running in parallel.</em></li>
<li>How can I sync these processes (parent with five child processes) using semaphores, in an infinite loop? So the output would be: 1 2 3 4 5 reset 1 2 3 4 5 reset etc...</li>
<li>Any simple and understanding semaphore documentation?</li>
</ol>
<p>Code:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/wait.h&gt;

parentWithFiveChildren()
{   
    printf(&quot;1 &quot;); //Parent
    fflush(stdout);

    int i, status;

    for (i = 2; i &lt; 7; i++)
    {   
        sleep(1); 
        if (fork() == 0) //Child Processes
        {               
            if (i == 6) 
            {
                printf(&quot;reset &quot;); 
                fflush(stdout);
                exit(0);
            }
            printf(&quot;%d &quot;, i); 
            fflush(stdout);
            sleep(7); 
            exit(i); //Exiting child processes
        }
    }

    while ((wait(&amp;status)) &gt; 0)
    printf(&quot;\a&quot;);
    return 0;
}

int main(void)
{
    parentWithFiveChildren();
}
</code></pre>
<h3>Output:</h3>
<pre><code>1 2 3 4 5 reset
</code></pre>