HW2 released: 02/03, 12:00 due: 02/09, 23:59 Answer the following questions. Submit your answers to Canvas assignments. There is an entry for this homework. 1. Read handout week.3a (https://naizhengtan.github.io/22spring/notes/handout_w03a.pdf) and answer the questions below. 1.a What does "0" in "wait(0)" (line 23) mean? (1 point) [answer: 0 is NULL in C] 1.b Read "6. Commentary" and copy-paste it below. (1 point) [answer: Why is this interesting? Because pipelines and output redirection 156 are accomplished by manipulating the child’s environment, not by 157 asking a program author to implement a complex set of behaviors. 158 That is, the *identical code* for "ls" can result in printing to the 159 screen ("ls −l"), writing to a file ("ls −l > output.txt"), or 160 getting ls’s output formatted by a sorting program ("ls −l | sort"). 161 162 This concept is powerful indeed. Consider what would be needed if it 163 weren’t for redirection: the author of ls would have had to 164 anticipate every possible output mode and would have had to build in 165 an interface by which the user could specify exactly how the output 166 is treated. 167 168 What makes it work is that the author of ls expressed their 169 code in terms of a file descriptor: 170 write(1, "some output", byte_count); 171 This author does not, and cannot, know what the file descriptor will 172 represent at runtime. Meanwhile, the shell has the opportunity, *in 173 between fork() and exec()*, to arrange to have that file descriptor 174 represent a pipe, a file to write to, the console, etc. ] 1.c Why do lines 143 and 144 close the "fdarray"? In other words, what can go wrong without closing them? (2 points) [answer: Commands on Line 143 and 144 closes the read and write ends of the pipeline, respectively. If they were not executed, the second command of pipe will not terminate as it didn't receive a EOF trigger.] 2. Play with fork [hints: -- below are valid code. -- we omit header files. -- to see which header files to include, use "$ man ". (e.g., "$ man fork" to see which header files define "fork".) ] 2.a Run the following code for three times. (2 points) int main() { // [update 2/4: the "printf"s below were "print", which are typos.] printf("hello world"); fork(); printf("\n"); } Write down the outputs below and answer the question. Answer: first time output: """ // write your output here """ second time output: """ // write your output here """ third time output: """ // write your output here """ If "hello world" appear twice in any outputs, explain why in 1-2 sentences. (write "CONDITION FALSE" if the "hello world" only appear once.) [answer: you will likely see "hello world" twice for all three runs. If so, the explanation is: Because "hello world" is buffered in the memory when executing fork, the string is duplicated as well. When both parent and child print "\n", the "hello world" is printed twice: one for parent and one for child. ] 2.b Run the following code for three times. (2 points) int main() { fork(); fork(); // [update 2/4: fix the double quotation marks] printf("\nhello world"); } Write down the outputs below and answer the question. Answer: first time output: """ // write your output here """ second time output: """ // write your output here """ third time output: """ // write your output here """ If the outputs are different, why? Explain in 1-2 sentences. (write "CONDITION FALSE" if the outputs are the same) [answer: you will likely see different outputs. If so, the explanation is: After two forks, there are four processes. The scheduling of these four processes are non-deterministic (depending on OS scheduler), hence the outputs may vary. ] 2.c Run the following code for three times. (2 points) int main() { int pid = fork(); if (pid > 0) { wait(NULL); } int pid2 = fork(); if (pid2 > 0) { wait(NULL); } printf("\nhello world"); } Write down the outputs below and answer the question. Answer: first time output: """ // write your output here """ second time output: """ // write your output here """ third time output: """ // write your output here """ If the outputs are the same, explain why in 1-2 sentences. (write "CONDITION FALSE" if the outputs are different) [answer: you will see the same output. ("CONDITION FALSE" should never appear.) Though there are four processes, the "wait" forces the executing orders of the processes: the child run first then the parent. ]