0

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;

    srand(time(NULL));

    //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) {
                    perror("fork");
                    abort();
            }
            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?

SzajnIn
  • 81
  • 2
  • 7

2 Answers2

0

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...

Sharing memory between forked processes is explained well here

I would use mmap to create shared memory between the processes, you'll need to specify for each process where to put it's result, then use wait to determine when all children have exited, and a good program would evaluate the exit status and inform the user if any child exited abnormally.

Don't forget to clean up the shared memory before the parent exits.

Community
  • 1
  • 1
Jfevold
  • 422
  • 2
  • 11
  • No offence, but setting up SHM to just return 10 times a value between 0 and 100 seems quiet some effort ... ;-) – alk May 01 '16 at 15:36
  • Agreed. Using fork instead of multi-threading is also overkill, but this is an academic exercise, not an optimization issue for production code. – Jfevold May 01 '16 at 15:37
0

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;
    wait(&status);
    if(WIFEXITED(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.
        --i;
    }
Jfevold
  • 422
  • 2
  • 11