c - Why getppid() from the child return 1 -


i running program

#include<stdio.h> #include <unistd.h> main() {     pid_t pid, ppid;     printf("hello world1\n");     pid=fork();     if(pid==0)     {         printf("i child\n");         printf("the pid of child %d\n",getpid());         printf("the pid of parent of child %d\n",getppid());     }     else     {         printf("i parent\n");         printf("the pid of parent %d\n",getpid());         printf("the pid of parent of parent %d\n",getppid());             } } 

the output got was.

$ ./a.out  hello world1 parent pid of parent 3071 pid of parent of parent 2456 child pid of child 3072 pid of parent of child 1 

i couldnt understand line

the pid of parent of child 1

it should have been 3071?

because parent process finished time child asks parent's pid.

when process finishes, children reassigned children of init process, pid 1.

try using wait() in parent's code wait child execute. should work expect.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -