/* * Example demonstrating fork(), getpid(), and getppid() */ #include main() { int pid; /* hold child's process-id in parent */ printf("Just one process so far. My pid is %d\n", getpid()); fflush(stdout); /* ensuring stdout buffer isn't duplicated in fork() */ switch ( pid = fork() ) { case 0: /* child */ printf("I'm the child, parent has pid %d\n", getppid()); break; case -1: /* error: resources must be exceeded */ printf("Fork returned error code, no child\n"); break; default: /* parent */ printf("I'm the parent, child has pid %d\n", pid); break; } }