/* example of fork() system call. RAB 1/03 */ #include #include int main() { int ret; /* for return value from fork() */ printf("I am the PARENT. My pid is %d\n", getpid()); if ((ret = fork()) < 0) { printf("fork() FAILED"); perror("forkeg"); _exit(1); } /* fork() succeeded */ if (ret != 0) { printf("I am the PARENT. My pid is %d, and fork() returned %d.\n", getpid(), ret); /* NOTE: parent should ordinarily wait(), to collect child's exit status */ } else { printf("I am the CHILD. My pid is %d, and fork() returned %d.\n", getpid(), ret); _exit(1); } /* only parent sees this code, because child exited */ printf("I am the PARENT. My pid is %d, and I'm signing off.\n", getpid()); _exit(0); }