Mega Code Archive

 
Categories / C / Linux
 

Get parent pid

#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { pid_t pid; pid = fork(); if(pid == 0) { printf("I am child with pid: %d\n", getpid()); printf("as a child my parent pid is: %d\n", getppid()); } else { printf("I am a parent with pid: %d\n", getpid()); printf("as a parent my child pid is: %d\n", pid); } /* Following code executes in both processes */ printf("oew.. I have to exit: %d\n", getpid()); exit(0); }