1

this program is used to create maximum no. of processes system is allowed to create

Ok fine but i didn't get the else part what is going in there

When i execute it my system hangs boot itself automatically??

Can someone please explain it how the following code works??

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

int main()
{
    pid_t pid;
    int i = 1;

    for(;;)
    {
        pid = fork();
        if(pid < 0)
            printf("MAX no of concurrent process are %d\n",i);

        if(pid == 0)
            i++;
        else
        {
            wait(0);
            exit(0);
        }
    }

    return 0;
}
bitcell
  • 921
  • 8
  • 16
  • the else part will only run on parent process. hence, it wait()s for its children to exit. – Isa A Nov 26 '14 at 07:24
  • yes it is but what about the exit() after the wait() statement? – user4255847 Nov 26 '14 at 07:32
  • @IsaA how come parent call exit()? – user4255847 Nov 26 '14 at 07:33
  • A semantic parser would issue a "warning: unreachable code" statement. ;-) – Peter - Reinstate Monica Nov 26 '14 at 07:39
  • 1
    Should be `if(pid<0) { printf("..."); exit(0); }` – user3386109 Nov 26 '14 at 07:56
  • @user3386109 can you tell how the program will work with what you have just said?? – user4255847 Nov 26 '14 at 07:58
  • I am referring to Book The 'C' Odessey UNIX – user4255847 Nov 26 '14 at 07:59
  • 1
    The problem that I see is that the posted code uses all available processes, and then after the `fork` fails, it calls `wait(0)` on a process that has no children. So I expect that `wait` will never return, and the system will crash since it can't create more processes. – user3386109 Nov 26 '14 at 08:04
  • @user3386109 when fork is called first time child process will increment i and the parent process will call the else part i.e it will wait for child process to exit and then call exit() itself. I dont't get it why would parent process wait for child who doesn't call exit and then exit itself. can you please elaborate your answer – user4255847 Nov 26 '14 at 08:34
  • Yes, but what happens when the `fork` fails and returns a value that's less than 0? You call `printf`, and then what? – user3386109 Nov 26 '14 at 08:43
  • @user3386109 i really want to know why would parent process wait for the child process which doesn't even call exit ? – user4255847 Nov 26 '14 at 08:49
  • @user3386109 ok i get it but my system hangs after few seconds and reboot itself the if(pid<0) part never get called – user4255847 Nov 26 '14 at 08:57
  • 2
    Yes, running a ***[fork bomb](http://en.wikipedia.org/wiki/Fork_bomb)*** on your system is a bad way to determine the process limit. Even with the change I suggested, it's still possible that your system will crash before displaying the `printf`. There are better ways to determine the process limit. See ***[this discussion](http://stackoverflow.com/questions/9361816/maximum-number-of-processes-in-linux)*** for example. – user3386109 Nov 26 '14 at 08:59
  • @user3386109 This is not really a fork bomb: it only creates *one* child process per child (no exponential increase) and is supposed to stop once an error occurs. – glglgl Nov 26 '14 at 09:02
  • On failure of any [syscalls(2)](http://man7.org/linux/man-pages/man2/syscalls.2.html), notably [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html), you should use `errno`, e.g. with [perror(3)](http://man7.org/linux/man-pages/man3/perror.3.html) – Basile Starynkevitch Nov 26 '14 at 09:05
  • the fork failure part never make it system runs out of memory before and reboot itself – user4255847 Nov 26 '14 at 09:11

1 Answers1

1

On entering the for loop, the process is tried to be forked.

On success, in the parent process, fork() returns something > 0, the PID of the child. The child returns 0.

On failure, fork() returns < 0. This case should be handled appropriately.

In your code, the child process increments the "inherited" i and goes on with the next loop run, but the parent waits for its child and exits.

This all goes well up to the point where fork() fails. Then you get an output, but the code is continued nevertheless, up to wait(0). There it hangs, and so do all its parents.

If you would do

if(pid < 0) {
    printf("MAX no of concurrent process are %d\n",i);
    exit(0); // or return 0
}

the child which was unable to create another child would exit properly and so would do all of its parents.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • the if(pid < 0) doesn't matter it will never going to execute the system will run out of memory before and hang itself till death – user4255847 Nov 26 '14 at 09:07