I have a problem, in C, with an execve after a fork. I need to create 20 children and, only when they are succesfully create, I'll do stuff. I'm using a semaphore to do this but still not working, because in execve file that I running after the fork, it read last 2 or 3 children. But I want to "stop" the file and make him wait for the creation.
Take a look my code: Father.c
system("gcc person.c -o Person");
for(i = 0; i < NUM_PEOPLE; i++){
if((pidprocesso = fork()) == 0) {
// Insert in one struct
insertInStruct(randType, randName, randDna, getpid(), 1, i);
// Semaphore that have init val to NUM_PEOPLE
reserve(sem_totchild); // This make a -1 operation
char *argv[] = {"0", "1", "2", "stuff", NULL};
execve("./Person", argv, NULL);
}
}
person.c
while((semctl(sem_totchild, 0, GETVAL)) > 0){
// Something to wait the end of creation
// And I can't use sleep()
}
// Code that will run after creation
Obviously I linked well the semaphore in all two files.