1
votes

Here is my updated code snippet

for (int i=0; i<30; i++){

        printf("Forking\n");

        tmpPid = fork();

        switch(tmpPid)

       {
         case -1:

            printf("Error");
            break;
         case 0:

          printf("Fork success for proc %d\n", i);
          //call some function
           _exit(0)
           break;

         default:

         printf("Fork succeed");
         //store the pid in a vector
          break;
    }
}

//iterate through the vector and wait on each process.
// waitpid(vectorList[i], &exitStatus, WNOHANG)

Forking of almost 15-18 child process works fine. But for some process, the child code is not executed at all and the child process just hangs. (i,e fork() only returns child id but not zero). If i buy more time inside the for loop by calling some other dummy functions, then this problem is not seen. Can anyone tell me whats the problem ? Is it necessary to put delay between multiple forks ?

Thanks

3
The bug is in code not shown, most likely the function you call in case 0 (for example, if it doesn't call _exit, that's probably your problem). Give us a complete, compilable, self-contained example that demonstrates the problem. - David Schwartz
If the code in some function ever returns, isn't this pretty much a fork bomb? - unwind
I forgot to mention _exit(0) here. It is there in my actual code. Still 1 or 2 process never hit the child code but the parent is executed. - Sharath Bhat
You should provide an output highlighting where you think things are going wrong. - kmkaplan

3 Answers

1
votes

I'm not sure the program does what it was initially intended to do.

Basically

  for (i=0 ; i<N ; i++) {
      print("Forking\n");
      fork();
  }

reasoning from the end having N == 30.

  • for i == 29: the child and parent end the for loop and die
  • for i == 28: the parent has still to do 29, and so does the child...

etc...

Instead of 30 "Forking" displayed, there will be 2^0+2^1+2^2+...+2^29 "Forking"s.

I.e. 2^N-1 or 2^30 - 1, or 1,073,741,823 forkings.

It is likely that the system complains and hangs reaching either a ulimit / system / window / user / shell limit depending on your OS.

Maybe

  for (i=0 ; i<N ; i++) {
      print("Forking\n");
      if (fork()) exit(0);
  }

or in the snippet have default: to exit(0); making the children die (horrible words) would provide the intended result.

1
votes

If the parent process is killed, then child also will be get killed. So we need to inform the parent process to wait till child finishes its work. Use wait or waitpid in your program.

wait will block the calling process until any one of its child process terminates.

waitpid will block the calling process until the child process which mentioned in first agrument terminates.

0
votes

Some child processes may not have time to print before the parent dies.

You need to wait for the child processes before ending the parent process.

There could also be the problem of several child processes writing to the same output stream simultaneously. (Can anyone verify that this is a potential problem?)