CS810 Advanced Programming in the UNIX Environment Quiz 2 FULL NAME ______________________________________ USERNAME ______________________________________ 1) What is the difference between the exit(2) and _exit(2) functions? (2 points) A. The exit function exits from the parent process, the _exit function exits from the child process. B. The _exit function discards all input before exiting. The exit function blocks until all input is provided. C. The _exit function sends a SIGKILL signal to the process, causing an immediate exit. The exit function does the reverse. D. The exit function exits from the process, calling all exit hooks. The _exit function does not call exit hooks before exiting. Correct answer: (D) 2) The unlink system call "int unlink(const char *path)" has (possibly amongst others) the following effect: (2 points) A. The file at the given location is removed, making the data blocks the file consumed immediately available. B. The directory in which the given file resides is updated to remove the mapping of inode number to the filename portion of the given pathname. C. The datablocks associated with the inode of the given file are overwritten, allowing only processes with an open filehandle to read the file. D. The given character array is truncated at the directory portion of the pathname. E. All of the above. F. None of the above. Correct answer: (B) 3) What is the prototype of the function used by the parent process to retrieve the child's PID? (2 points) A. pid_t getpid(void) B. pid_t getppid(void) C. pid_t getcpid(int) D. pid_t *getcpid(void) E. None of the above F. All of the above Correct answer: (E). fork(2) returns the pid of the child to the parent; this is the only way for a parent to keep track of its childrens' pids. 4) If you call 'realloc' to increment the amount of memory you allocated in an immediately preceeding 'malloc' call, can you continue to operate on the pointer the original 'malloc' returned? Explain. (2 points) No. If you are incrementing the amount of memory with realloc, it's possible that the additional memory could not be allocated at the end of the previously allocated chunk. In that case, realloc would allocate a completely new chunk in a different location and return a pointer to that new location, making the original pointer returned by your malloc call invalid. Note that (a) if you reduce the amount of memory originally allocated, the original pointer remains valid; (b) even if you increase the amount of memory, the original pointer *may* still be valid, but you have no guarantee 5) Describe the algorithm of the 'pwd' command. Elaborate on which directory- and file-related calls are made. (3 points) $ pwd /home/jschauma/teach/CS810-APUE/F2008/06-signals Approximately: loop: - stat '.' - note inode of '.' as I - stat '..' if inode of '..' == I, then print all names on stack joined with '/' else - open '..' - read directory entries until you encounter the one with inode I, push that name on stack - chdir to .. - goto loop 6) Elaborate on the effect of running each of these three simple programs. Please include anything noteworthy about the parent-child relationships of the processes created by the programs. (3 points each -> 9 points total) A. int main() { while(!fork()); return 0; } B. int main() { while(fork()); return 0; } C. int main() { while(1) fork(); return 0; } Solution: (a) creates one process per iteration, but each parent exits right away, so no zombies are created. (b) creates one zombie process per iteration, since each child exits right away and the parent keeps forking and does not wait for them. (c) fork bomb: both parent and child keep creating processes forever that do the same