/* COMP201/2401 - Winter 2013 fork: create a clone of the current process */ #include #include int main() { int pid; int i; printf("Forking...\n"); sleep(1); // create a clone process pid = fork(); if (pid == 0) { // in the child process, the return value of fork // is zero printf("child process: fork returned %d \n", pid); printf("child process: process id is %d \n", getpid()); printf("child process: parent pid is %d \n", getppid()); for (i=1; i<=10; ++i) { printf(" child iteration %2d \n", i); sleep(2); } } else { // in the original process, the return value of fork // is the PID of the child process printf("parent process: fork returned %d \n", pid); printf("parent process: process is is %d \n", getpid()); printf("parent process: parent pid is %d \n", getppid()); for (i=1; i<=10; ++i) { printf(" parent iteration %2d \n", i); sleep(1); } } }