Section: 2:30 3:35 Name: CMPSC 311 Such Final Very Exam May 5, 2014 Closed book, closed neighbor, no electronic tools or additional papers. You may not share or discuss exam questions with anyone. This material may not be reused, redistributed, or embedded without prior written consent of the author.
1 Short Questions (36pts total, 3pts each, be brief) 1. What is the mode of a file? 2. Fill in the blank in the output of the following code: uint16_t x = 42; uint16_t *p = &x; printf("p is: %p\n", p); p += 3; printf("now p is: %p\n", p); Output: p is: 0xfff800 Now p is: 3. Write a short C implementation of the Unix false command. 4. Name a function used specifically to print a formatted error message after a C library function or system call fails and sets errno? 5. In a distributed version control system, what must be true in order to push revision X to a repository which contains everything up to revision Y? 6. How does a program reap a child process that has become a zombie? 7. What is a file descriptor? 1
8. Each thread in a multi-threaded process has its own copy of which memory segment or segments (code, data, heap, stack)? 9. When the operating system receives an incoming TCP or UDP packet, how does it know which socket it should be delivered to? 10. What function is used to disconnect a socket? 11. What is the difference between the following two code snippets? lseek(fd, 100, SEEK_SET); read(fd, buf, 4); pread(fd, buf, 4, 100); 12. What specific protocol/system does the gethostbyname function use? 2 Medium Questions (34pts total) 13. (5pts) The following function has a problem. Write a fixed version. int *give_me_twenty(int x) { int array[20]; int i; for (i = 0; i < 20; i++) array[i] = x; return array; } 2
14. (5pts) What does the following statement do? Be specific. int (*foo)(double *); 15. (6pts) Write two function calls that open the file hello.txt for reading only: one using stream I/O, and the other using low-level file descriptor I/O. Be sure to store the return values in a variable of the appropriate type. 16. (6pts) Name one advantage of: (a) a sequential server over a forking server (b) a forking server over a threaded server (c) a threaded server over a forking server 3
17. (6pts) Name the following six version control operations: (a) Create a new revision in the repository: (b) Make a copy of an entire repository, including all revisions: (c) Copy revisions from a local repository to a remote one: (d) Copy revisions from a remote repository to a local one: (e) Copy a revision from the repository to the working copy: (f) Create a new revision that includes two or more branches: 18. (6pts) Below is a struct that represents a network header. Fill out the header_to_hbo function that converts the values in the header from network byte order to host byte order. struct header { uint32_t sourceid, destid; uint16_t port; uint8_t ttl, flags; }; void header_to_hbo(struct header *h) { } 4
3 Long Questions (30pts total, 10pts each) 19. Reading questions: (a) What is the first argument to the sprintf function in C? (b) What does the alarm function do? (c) Write a C snippet that tells the program to ignore the SIGINT signal. 20. Convert the following description into a C abstract data type using the techniques described in class: A shoe is a type of accessory. (Note: you may assume that there is already an ADT called accessory.) Every shoe has a particular size. When I initialize a new shoe, I should be able to specify the size. Given a shoe, I should be able to get its size. 5
21. Name four functions we covered in lecture which operate specifically on sockets (NOT on other types of files), and identify what each one does. Be sure to mention whether it is used on the client side, the server side, or both. 4 Bonus As specifically as possible, name beverages consumed by the instructor during class. All of them have actually been identified aloud by name during the semester. One point for a correct answer, but an incorrect guess will cancel out a correct one. 6