Tux

...making Linux just a little more fun!

question regarding fork() system call

aman verma [verma.aman81 at gmail.com]


Tue, 29 Jan 2008 09:35:48 +0530

dear sir/ma'm i just want to know if we are using fork() system call again and again then what will be the output like

main()
{
    fork();
    printf("\n my id=%d and and my parent id=%id",getpid(),getppid());
 
      fork();
    printf("\n my id=%d and and my parent id=%id",getpid(),getppid());
 
}

please forward me the answer

thanks and regards, amandeep verma


Top    Back


Amit Kumar Saha [amitsaha.in at gmail.com]


Tue, 29 Jan 2008 10:23:33 +0530

On 1/29/08, aman verma <verma.aman81@gmail.com> wrote:

> dear sir/ma'm
> i just want to know if we are using fork() system call again and again then
> what will be the output like
>
> main()
> {
>     fork();
>     printf("\n my id=%d and and my parent id=%id",getpid(),getppid());
>
>       fork();
>      printf("\n my id=%d and and my parent id=%id",getpid(),getppid());
>
> }

The key to understand this is that a child is forked on every encounter with fork(). You can visualize it as starting with a tree with the initial parent process as the root and then whenever it comes across a fork(), add a new child node to it and carry on the execution of the child node as well as the parent node individually.

Please correct me if I am wrong in my analogy.

HTH, Amit

-- 
Amit Kumar Saha
*NetBeans Community
Docs Coordinator*
Writer, Programmer, Researcher
http://amitsaha.in.googlepages.com
http://amitksaha.blogspot.com


Top    Back