/* file: simple_fork1.c See comments and questions at the end of this file. Compare against simple_fork0.cc. */ #define _XOPEN_SOURCE 500 #include #include int main(int argc, char *argv[]) { int i = 0; while (i < 3) { fprintf (stdout, "i = %d in %d\n", i, getpid ()); fork (); i++; } return 0; } /* Purpose: Illustrate the use of the fork() system call to create a new process. To compile this program: gcc -o simple_fork0 simple_fork0.c To run this program: ./simple_fork0 Sample terminal output, when the above was done on a Linux machine with two CPU's: i = 0 in 25604 i = 1 in 25604 i = 1 in 25605 i = 2 in 25604 i = 2 in 25606 i = 2 in 25607 i = 2 in 25605 Before the course is over, you should be able to answer all of the following questions: 1. How many new processes are created by running this program? 2. Can you explain why the output is as shown above? Look at the man pages for the C library calls use here, including fprintf, fork, and getpid. If there is any detail of this program that you cannot figure out, ask questions, either in class, on the course discussion board, or by e-mail to one of the instructors for this course. */