/* $Id: getpid.c,v 1.1 2014/10/02 11:13:10 kostik Exp kostik $ */ #include #include #include #include #include #include #ifdef UNISTD_H_NOTUPDATED int forkdepth(u_int *); #endif static void print_pids(const char *s) { pid_t my, parent; u_int depth; int error; my = getpid(); parent = getppid(); if (s != NULL) printf("%s: ", s); printf("me %d parent %d", my, parent); error = forkdepth(&depth); if (error != 0) printf("\nforkdepth failed: %s\n", strerror(error)); else printf(" depth %d\n", depth); } int main(void) { pid_t child; print_pids("main"); fflush(stdout); child = fork(); if (child == -1) err(1, "fork failed"); else if (child == 0) { print_pids("child"); exit(0); } else { printf("main: child %d ", child); print_pids(NULL); } return (0); }