First of all, I surely know there are faster and less overkill solutions to this, but I absolutely need to fill in an array with child processes only.
Let's say I have 3 childs:
int pos = 0;
for (i = 0; i<3 ; i++){
switch (fork()){
case -1: //fork error;
printf("[ERROR] - fork()\n");
exit(EXIT_FAILURE);
case 0: //child
fill(&req, pos);
pos++;
exit(EXIT_SUCCESS);
default:
break;
}
}
where fill basically works like this:
void fill (request *req, int pos){
req->array[pos] = 1;
}
I realized this method of course doesn't work, since every child has a copy of pos = 0, and they just increment their copy, so the array always gets modified at 0. The struct request is a simpe struct with a pid and a int array to send through fifo.
typedef struct request {
int cpid; //client pid
int array[SIZE]; //array
} request;
What can I do to fill in this array with the child processes only? I have to repeat, I can't use workarounds, just fork() and childs. Thanks!