HW2 answers released: 09/29, 21:00 > Study 09/21's lecture handout (https://naizhengtan.github.io/21fall/notes/handout_3a.pdf) > and answer the following questions. > Submit your answers to Canvas assignments. There is an entry for this homework. > > > Question 1: Describe what happens when line 21 is executed. > [hint: read "$ man 2 execve"] The executable "command" will be executed with "args" as arguments and we do not send any environment parameters to the program (0 means NULL). > > > Question 2: Compile and run the code below. > """ > #include > int main() { > // We used to have a bug here (can you see why?) > // the line was: char * argv[] = {"ls", "-a"} > char * argv[] = {"ls", "-a", NULL}; > int ret = execve("/bin/ls", argv, 0); > } > """ > Write down the output of this program and what you think happened. > [cheng: my answer is: """ . .. lab1 lab2 """ You should see something else. ] > [open question: "ls" is part of the arguments ({"ls", "-a"}). why? > what if you replace "ls" with something else? do you expect this outcome? and > what do you think of this consequence (of replacing "ls" with something else)?] Your "ls" program should work normally even if you change the first argument "ls" to something else (e.g., "lsxxx"). But, that does not mean every program will ignore the first argument which is supposed to be the name of the program. See also this question and its answers: https://unix.stackexchange.com/questions/315812/why-does-argv-include-the-program-name > > Question 3: Describe what happens when line 23 is executed. > [hint: read "$ man 2 wait"] > The parent process will wait for the child process to finish. > > Question 4: In line 23, what does "0" mean in "wait(0)"? > [hint: read "$ man 2 wait"] > In C, 0 == NULL. Here NULL means the parent process doesn't care detailed exit information of the child process. > > Question 5: Describe what happens when line 69-71 are executed. > [hint: read "$ man 2 pipe"] > The system call "pipe" creates two file descriptor and put them into "fdarray". These two file descriptor are connected: when we write "hello" to one (fdarray[1]), we can read "hello" from the other (i.e., fdarray[0]) > > Question 6: Read "6. Commentary" (starting line 153). And answer the > question---have you read "6. Commentary"? [cheng: hope your answer is "yes"]