Using Fork() and Pipe() to sum a file split to multiple child processes
<p>This is a cleaner final version of two previous question (ugly code; deleted by me).</p>

<p>I am using <code>pipe()</code> and <code>fork()</code> to sum a the contents of a file in child processes. To do this, I split the file up evenly depending on the amount of children:</p>

<p>1000 lines in the file => 2 child processes => first child sums lines 1-500; second child sums lines 501-1000 => send totals back to parent to sum each of their totals. Thus, the total of the whole file is found.</p>

<hr>

<p>Here is my code:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

int main(int argc, char *argv[])
{
int numchild;
int fd[2*numchild][2]; //parent+child pipe
int i, j, len, fpos=0, val, count=0, total=0;
pid_t pid;
int nums = 1000;
FILE * file;

printf("How many children to use: ");
scanf("%d", &amp;numchild);
printf("\nWill use %d child process(es).\n", numchild);

// create all pipes
for (i=0; i&lt;numchild; i++)
{
    pipe(fd[i]);
}

for (i=0; i&lt;numchild; i++)
{
    if((pid = fork()) == 0) // child process
    {
        pid = getpid();

        // read from parent
        len = read(fd[i][0], &amp;fpos, sizeof(fpos));
        if (len &gt; 0)
        {
            file = fopen("file1.dat", "r");
            fseek (file, fpos, SEEK_SET);
            count = 0;
            total = 0;

            printf("Child(%d): Recieved position: %d\n", pid, fpos);

            // read from file starting at fpos
            // add values read to a total value
            while (count &lt; (nums/numchild))
            {
                fscanf(file, "%i", &amp;val);
                total += val;
                count++;
            }
            //write to parent
            write(fd[i+numchild][1], &amp;total, sizeof(total));
            printf("Child(%d): Sent %d to parent.\n", pid, total);
        }
        else
        {
            printf("Child(%d): Error with len\n", pid);
        }

        _exit;
    }

    // parent process
    pid = getpid();

    fpos = ((i*nums*5)/numchild); // 5 is the offset of the file values

    // write to child process
    printf("Parent(%d): Sending file position to child\n", pid);
    write(fd[i][1], &amp;fpos, sizeof(fpos));

    // wait for child responce
    len = read(fd[i+numchild][0], &amp;total, sizeof(total));
    if (len &gt; 0)
    {
        printf("Parent(%d): Recieved %d from child.\n", pid, total);
        total += total;
        printf("Parent(%d): Total: %d\n", pid, total);
    }
    else
    {
        printf("Parent(%d): Error with len\n", pid);
    }
}
}
</code></pre>

<hr>

<p>Output:</p>

<pre><code>Will use 1 child process(es).
Parent(5497): Sending file position to child
Child(5498): Recieved position: 0
▒▒Child(5498): Sent 454019 to parent. //**yes this weird mark shows up**
Parent(5498): Sending file position to child
//hangs
</code></pre>

<p>.</p>

<pre><code>Will use 2 child process(es).
Parent(5898): Sending file position to child
Child(5899): Recieved position: 0
▒|Child(5899): Sent 228601 to parent.
Parent(5899): Sending file position to child
//hangs
</code></pre>

<p>.</p>

<pre><code>Will use 4 child process(es).
Parent(5904): Sending file position to child
Child(5905): Recieved position: 0
▒Child(5905): Sent 118304 to parent.
Parent(5905): Sending file position to child
//hangs
</code></pre>

<hr>

<p>The code works fine for one child where I can see the sum come up, but it hangs and the parent never acknowledges the input from the child. Thus, if I use 2 children, I only see the sum from lines 1-500.  If I use 4 children, I only see the sum from lines 1-250.</p>

<p>What am I doing to hang up my program after the first child sends it's result back to the parent?</p>

<p>Is my code allowing the children to work at the same time?</p>

<hr>

<p>Thanks for your help.</p>

<p>-Tom</p>