Child process forked 10, how can the parent process collect its return values?


I have to find the biggest value in an array of a 1000 numbers with 10 child processes (so that every one of them only checks a hundred values), and the parent only has to collect the data. I'm already done with the whole thing, but I'm stuck at reading the values.

Here's the code:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(){
    int array[1000];
    int i, j;
    int pids[10];
    int searchminindex;
    int searchmaxindex;
    int maxindex;


    //fill up array with random numbers
    for(i = 0; i < 1000; i++)
            tomb[i] = random() % 5000;

    //create 10 child processes
    for (i = 0; i < 10; i++) {
            if ((pids[i] = fork()) < 0) {
            else if (pids[i] == 0) {
                    searchminindex = i * 100;
                    searchmaxindex = (i+1) * 100;

                    //finding the biggest value
                    maxindex = searchminindex;
                    for(j = searchminindex+1; j < maxindex; j++) {
                            if( array[maxindex] < array[j])
                                    maxindex = j;

    for(i = 0; i < 10; i++){
        //here's where I'd read the return values of the subarrays

    return 0;

I've tried using pipes and also using WEXITSTATUS, but I'm really confused and don't know where to close one end of the pipe and things like that, and with WEXITSTATUS I'm completely lost.

Any way you could help?

You need to test the pid returned from fork, and branch your code so your main process doesn't act like a child, and so that your children don't spawn children of their own. Once that's taken care of...

An alternative to mmap or setting up shared memory at all is to use WEXITSTATUS. According to the man page, it'll only return the least significant 8 bits, so if your return values can be greater than 127, this is likely not your best option. Can be made to work up to 255, but be careful about signedness of char, it's not standard.

int returned_values[10];
for(int i = 0; i < 10; ++i)
    int status;
        returned_values[i] = WEXITSTATUS(status);
    else {
        //Do something more meaningful here
        //This means a child received a signal, or any of the other ways wait returns other than a child exiting.