My code is forking a process and printing each process' PID and PPID. I was expecting the child's PPID to be same as the parent's PID, but it is not coming up as such.
I'm using Ubuntu 14.04.
#include <stdio.h>
#include <sys/wait.h>
int main(){
int pid;
pid = fork();
if(pid==0){
printf("\nI am the child and my parent id is %d and my id %d\n", getppid(), getpid());
}
else
printf("\nI am the parent and my pid is %d and my parent id is %d\n", getpid(), getppid());
return 0;
}
Here is the output I am getting:
I am the parent and my pid is 29229 and my parent id is 27087
I am the child and my parent id is 1135 and my id is 29230
fork()
may return -1 in case of error. You should handle that in your code. – ott-- Oct 01 '16 at 21:26fork
andwait
man pages will provide information on such things. – John WH Smith Oct 01 '16 at 21:28