1

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.

  • Possible duplicate of [Semaphore blocking children](https://stackoverflow.com/questions/47595022/semaphore-blocking-children) – Petr Skocik Dec 04 '17 at 22:15
  • Must use `semop()` with `semval` equals to 0. If sempahore is initiated to 20 and decremented and each child creation, you will have your "rendez-vous". – Jean-Baptiste Yunès Dec 04 '17 at 22:17
  • Non there isn't a duplicate of semaphore. – Riccardo Calabrese Dec 04 '17 at 22:31
  • One simple way to synchronize the children is for the parent to open a pipe and launch all the kids. It then closes both ends of the pipe. The children immediately close the write end of the pipe, and then attempt to read one byte from the pipe. No byte arrives, but when the pipe is closed, they all get told, and can close the read end of the pipe too, and get on with work. Simple, effective, portable. – Jonathan Leffler Dec 04 '17 at 22:55
  • 1
    Note that your `argv` arrays tells the `Person` program that it is called `0`. With `execve()` comes great power and responsibility; you can call the program anything you want, but you must call it something. It isn't clear from your code (which is not an MCVE ([MCVE]), I note) why you think the System V IPC semaphores will be accessible in the `person.c` code; you've not shown how it connects to the semaphores, etc. You should be using `semop()`, I think, rather than `semctl()`, too. – Jonathan Leffler Dec 04 '17 at 22:57
  • I linked the semaphore well cause in both .c files I have a "sem_totchild = semget(KEYSEM_TC, 1, IPC_CREAT | PERMISSION)" where KEYSEM_TC is a const define with config.h file. – Riccardo Calabrese Dec 05 '17 at 11:35
  • regarding: `char *argv[] = {"0", "1", "2", "stuff", NULL}; execve("./Person", argv, NULL);` this is not correct, it should be: `char *argv[] = {"Person", "0", "1", "2", "stuff", NULL}; execve("*argv, argv, NULL); perror( "execve failed"); exit( EXIT_FAILURE );` – user3629249 Dec 06 '17 at 14:59
  • the programs should be using a NAMED semaphore so it can be used across processes. Amongst other things, the name must begin with a `/`. The correct function to call for a shared/named semaphore is `sem_open()` – user3629249 Dec 06 '17 at 15:19
  • this [semaphore](https://stackoverflow.com/questions/8359322/how-to-share-semaphores-between-processes-using-shared-memory) has plenty of errors in the selected answer, but the first few lines of the answer show how to use a named semaphore, Note all processes must use exactly the same name (unlike the linked answer) – user3629249 Dec 06 '17 at 15:24
  • @Jean-Baptiste Yunès - When you wrote: _Must use `semop()` with `semval` equals to 0_, did you mean _… with `sem_op` equal to 0_? – Armali Aug 23 '19 at 06:33

0 Answers0