CSC195, Class 53: More Fun with Processes Overview: * Questions from previous class? * Changing stdin and stdout * Executing other programs * Exercise: cat grep sort cat Notes: * Email me extra credit summaries * Read my essay on end-of-course evaluations soon * Fill out evaluation forms by end-of-class on Wednesday * Questions on the exam? Exam Questions: Q: Can we assume that we're binary searching a sorted array of integers? A: Yes, that's fine. Q: For problem 1, do we have to go through the checklist? A: Yes, it would be nice to have a quick summary that says "I did everything in the checklist." Q: Can we have our exams proofread? A: No. But given the state of my exam, I'm not likely to take off much for your errors. What did we do on Friday? Talked about processes: A process is "a program as its running" * Plus "a process id" (entry in process table) * Resources allocated to it (memory) * Current state (program counter) (settings of variables) Processes may also have a priority (not of interest today) In C, how do create a new process? * fork() returns -1 on failure 0 for child pid of child for parent What happens if you fork multiple times? You create lots and lots of processes Mr. Walker's code never calls wait. What happens when you fail to call wait? You may get the command prompt when the parent terminates. If the child prints some output, it appears after the command prompt. pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options); What does wait return? The process id of the child that exited. Why does wait take an int * as a parameter? We expect wait to change status. How? By setting it to the exit value of the child. 1. Write a program that forks five children and waits for all of them to return before exiting. Each child should print its number (not pid, but which child). Sample output: Hi, I'm child 0. Hi, I'm child 1. Hi, I'm child 2. Hi, I'm child 3. Hi, I'm child 4. All my children are finished. prompt # 2. Insert a call to sleep(somerandomvalue) into each child. 3. How would you update your program to handle ten children?