Operating Systems Sample Final Exam - SOLUTIONS Name (10 pts) 1. Suppose a memory allocator has a freelist with blocks of sizes as shown: 64 24 96 4096 Using a first-fit strategy, draw what happens to the freelist for the following sequence of requests: 1) Allocate a block of size 16 2) Allocate a block of size 48 3) Allocate a block of size 16 4) Allocate a block of size 256 1: 48 -> 24 -> 96 -> 4096 2: 24 -> 96 -> 4096 3: 8 -> 96 -> 4096 4: 8 -> 96 -> 3840 (10 pts) 2. The VAX used a paging system with 32-bit addresses, divided into three fields.. a 2 bit segment, 21 bits of page number, and 9 bits of offset. The 2-bit segment meant that each process could have up to four page tables associated to it (although segment 10 was used by the system and segment 11 was unused, leaving only segments 00 and 01 for the process). seg # page # offset 2 21 9 a. How large are the pages? 2^9 = 512 bytes. b. How large is each page table? (Give both # of entries and size in bytes.) Each table has 2^21 = 2M entries, each of which will need 4 bytes, so 8Mb. (10 pts) 3. Two threads simultaneously run the following code to insert an item onto a linked list. Here the_list is the global linked list, and item is a local variable for each thread. item->next = the_list; the_list = item; a. Explain how one thread s item could be left off the list. Thread one does item->next = the_list, thread 2 does the same. Then thread one does the_list=item, then thread 2 does it. Since the_list now points to thread 2 s item, and
thread 2 s item->next field points to the start of the old list, thread 1 s item is not on the list. b. What are the implications for the malloc() function from this course? It s not thread safe. In particular, freed memory may be lost as in part a. What would you change in malloc() to fix the problem? Add a mutex to protect the freelist. (10 pts)4. Suppose the virtual memory map for a process looks like the diagram on the left, with unmapped areas shown in white. Which of the following operations will result in a segmentation fault? System Use ffffffff X Read at $00000008 c0000000 Write at $1FFFFFF0 Heap Code Stack System Use 40000000 20000000 00000000 Read at $20000008 X Write at $20000008 X Read at $3FFFFFF0 Write at $40000010 Read at $40000010 X Write at $B0000008 (10 pts) 5. Consider two threads running at once: Thread 1: lock(a_lock); a++; if (a == 10) { lock(b_lock); b++; unlock(b_lock); a = 0; unlock(a_lock) Thread 2: lock(b_lock); b--; if (b == 0) { lock(a_lock); a--; unlock(a_lock); unlock(b_lock)
Suppose a is initially 9 and b is initially 1. If both threads execute at the same time, what are the possible outcomes? 1: a = 0 and b = 1 (t1 finishes then t2 starts) 2: a =9 and b = 0 (t2 finishes then t1 starts) 3: both threads hang (10 pts) 6. A floppy disk has the following free space bitmap: 1 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0 8 1 6 2 4 The superblock is stored in block 0, and the root directory is stored in block 1. The root directory contains: Filename Start block Size (in blocks) afile 2 3 bigfile 20 11 ccode.c 8 4 This file system is inconsistent. What is wrong? How would you suggest repairing it to minimize potential data loss? Bits at 12, 13, 17, 25 are inconsistent with files. Fix by changing size of ccode.c to 6, marking bit 25 as 1, marking bit 17 as 0. (10 pts) 7. A disk backup program copies every file on a hard disk to tape so that the disk can be restored in case of a crash. a. How do hard links affect the program? What can it do to deal with hard links? Multiple directory entries for same file, not one of which is special. It can mark each file on disk when it backs it up so that it only makes one backup of each file, and then note when a hard link needs to be rebuilt on restoring. b. How do symbolic links affect the program? What can it do to deal with symbolic links? Needs to detect symbolic links and save the link, not the file it links to. Also, for links to directories, the program shouldn t follow the symbolic link when walking the directory tree. (10 pts) 8. Sun s Network File System (NFS) is stateless. Discuss the implications for:
a. Opening and closing files. Open/close only affects internal state of the filesystem, so they can t be used. Instead, pass filename as an argument with every read/write call. b. Absolute and relative pathnames. Relative pathnames require a current directory, which is state. All pathnames need to be absolute. c. Random file access. This is not a problem. d. Sequential file access. Keeping a current position in the file requires state, and so cannot be done. All read/write calls need to include the file location. (10 pts) 9. Optical WORM disk drives are used by companies that need to store a large amount of data in a permanent, unalterable way. For example, companies use WORM drives to comply with S.E.C. rules for keeping financial records. WORM stands for write-once-read-many, which means that every sector of an initially blank disk can be written to exactly once, after which it becomes permanently read only. This might be accomplished by burning pits into the disk with a laser. Unlike a CD-ROM, which must be written all at once, a the sectors of a WORM drive can be used up slowly over time. Users will create files, then change them or even delete them, although the disk keeps a permanent record of the older versions. In designing a file system for a WORM drive, would you choose contiguous file allocation, linked file allocation (such as a FAT), or indexed file allocation? Give reasons why your choice is better than the other two. Indexed would be the best. When a file is changed, can give it a new inode and re-index the data if necessary. This way if a file only adds new blocks or has a few changes, the unchanged data blocks do not need to be rewritten. Contiguous is ok. By leaving free blocks at the end of a file, appending to a file wouldn t require rewriting the entire file, but any other change to the file would require a completely new copy to be made.
Linked allocation is bad because the FAT table would need to be rewritten every time a file changed. (10 pts)10. Give an example of a single command line Unix command that will create three processes. ps sort more (10 pts)11. Name two services provided by the TCP layer. Process-to-process communication via ports. Reliable, ordered, byte stream, persistent connections. (10 pts)12. What is the purpose of this program? Be specific - explain what the command line arguments are used for and what the output means. This program is a port scanner, which looks at a machine on the internet and discovers open ports - ports with a listening server attached. It attempts to open a TCP connection to every port on host (given by argv[1]) in the range from start to end (argv[2] and argv[3]). When a connection is accepted, it prints a message that that port is open. Note: This program is from a past final, and uses the old code for address resolution and socket connection. It s a good question, but I d have to change it substantially to ask it on our test. int main(int argc, char *argv[]) { // check arguments if (argc!= 4) { cerr << "usage: " << argv[0] << " host start end" << endl; exit(1); int pstart = atoi(argv[2]); int pend = atoi(argv[3]); // look up target host information struct hostent *targ_host = gethostbyname(argv[1]); if (targ_host == NULL) exit(1); // create socket int sock = socket(af_inet,sock_stream,0); if (sock < 0) exit(1); // build target s address description struct sockaddr_in targ_addr; memcpy(&targ_addr.sin_addr,targ_host->h_addr,targ_host->h_length); targ_addr.sin_family = AF_INET; // main loop
for (int p = pstart; p <= pend; p++) { targ_addr.sin_port = htons(p); // try to connect if (connect(sock,(sockaddr *)&targ_addr,sizeof(targ_addr))) { ; // failed - do nothing else { cout << p << " is open" << endl; close(sock);