The initial process creates 5 child processes, and waits for them to finish. Each child process performs 5 repetitions, where in each repetition

I’m a bit confused with the creation of processes with fork(), sleep() and wait() in c. Take the following piece of code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
  for (int i = 0; i < 5; i++) {
    if (fork() == 0) {
      for (int i = 0; i < 5; i++) {
        printf("pid:%d email:myemail \n", getpid());
        sleep(1);
      }

      printf("%d\n", getpid());
      exit(0);

      for (int i = 0; i < 5; i++) {
        wait(NULL);
      }
    }
  }
}

After this piece of code has been executed,executes processes but does not remove repetitions from 5 processes. I’m a little confused with the process.

The initial process creates 5 child processes, and waits for them to finish.
Each child process performs 5 repetitions, where in each repetition:

Prints the message
pid: PID email: USER_EMAIL
where PID is the child PID of the process, while USER_EMAIL is the email

Suspends its operation for 1 second (at a time) with the sleep call

The parent process prints the children’s PIDs when they are finished

>Solution :

@mixalispetros, you have multiple things to fix, and they all have to be fixed together for your code to work as intended.

      exit(0);

      for (int i = 0; i < 5; i++) {
        wait(NULL);
      }

The process ends on exit(0). wait is never called.

    if (fork() == 0) {
         // what code runs here?  The code in the new process
    }

What code runs within the fork() conditional? The new process’s code. Which process should run wait()? The original process. So in addition to being after an exit, the wait() is also in the wrong process.

Where to move it? The for loop wait()s for 5 child processes. Why would there be 5 child processes for which to to wait()? Because we had already started all 5 child processes before we went into our loop of 5 wait()s.

The wait()s must happen not just outside the child process conditional block, but also outside the loop around the call to fork().

I’m a bit confused with the creation of processes with fork(), sleep() and wait() in c

It is confusing. Refer to the documentation often to keep it straight.
Remember, fork() returns twice – in the original process (returning the process ID of the new process), and in the new process (returning 0).

Looking at the wait() manpage, I can see that NULL is not an intended input to the wait() function. NULL is meant for use with pointers; its value being correct for wait() is incidental. The obvious choice to pass wait() is -1:

   The value of pid can be:
   ...
   -1     meaning wait for any child process.
   
   0      meaning wait for any child process whose process group ID
          is equal to that of the calling process at the time of the
          call to waitpid().

In summary

  • Put the wait loop outside the loop that fork()s child processes. This will also move it ouside the block of code that executes in the child process.
  • Pass -1 or 0 to wait(), not NULL.

Leave a Reply