HOMEWORK 4 (COMMENTS AND COMMON MISTAKES) Version 1 (Feb 28, 2007) << Problem 2 >> o Although in-line (no loops) pseudo-code would satisfy the requirements of the problem, with a little effort, you should be able to compact your solution by expressing the algorithm with a loop. o To handle stdout redirection to a file, you will use dup2 in a similar manner to how it is used with a pipe. But instead of creating a pipe and getting back two file descriptors, you will need to call open and get back one file descriptor. Then, call dup2 to redirect stdout; specifically, you will replace the file descriptor STDOUT_FILENO with the file descriptor of the newly opened file. Note that dup2 requires file descriptors (non-negative integer), NOT a file pointer (FILE *); i.e., call open, NOT fopen. Furthermore, the open call should look something like: fd = open("FileName", O_WRONLY|O_CREAT|O_TRUNC, mode); which will create the file if it doesn't exist and delete it if it already does. o Dup2 should be called AFTER fork. Otherwise, you will end up destroying xssh's stdin/stdout or creating too many file descriptors o waitpid(..., 0) will block and is used to handle terminating foreground processes. waitpid(..., WNOHANG) does not block and is used to detect any terminated background processes by polling to see if there is any new information about terminated processes. << Problem 3 >> o If a problem asks "Indicate if your output is correct or not", the answer should not be "because it works as expected". You need to define what it means to work as expected. o If your comments on correctness are in-lined with the output, highlight the comments so that the reader can locate them easily. o Suggestion for Project A: Clearly distinguish debug output from normal output (e.g., TAB and tag debug output).