













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














University of California, Berkeley College of Engineering Computer Science Division ⎯ EECS Fall 2008 John Kubiatowicz
SOLUTIONS December 3rd^ , 2008 CS162: Operating Systems and Systems Programming
Your Name:
SID Number: Circle the letters of CS Login
First: a b c d e f g h I j k l m n o p q r s t u v w x y z Second: a b c d e f g h I j k l m n o p q r s t u v w x y z
Discussion Section:
General Information: This is a closed book exam. You are allowed 1 page of hand-written notes (both sides). You have 3 hours to complete as much of the exam as possible. Make sure to read all of the questions first, as some of the questions are substantially more time consuming.
Write all of your answers directly on this paper. Make your answers as concise as possible. On programming questions, we will be looking for performance as well as correctness, so think through your answers carefully. If there is something about the questions that you believe is open to interpretation, please ask us about it!
Problem Possible Score
Total
Problem 1f[2pts]: The NFS file service requires client state to be maintained at the server.
is included in the request message.
Problem 1g[2pts]: TLB lookup (i.e. address translation) must occur prior to checking for data in the first-level cache.
can perform TLB lookup in parallel with cache lookup by sending page offset portion of the address (doesn’t get translated) to the cache while translation is occuring. Also, we mentioned virtual-mapped caches (where tags are virtual addresses); in this case, the cache lookup happens before TLB lookup – which only occurs on cache misses.
Problem 1h[2pts]: The clock algorithm provides an approximation to a least-recently used (LRU) page replacement mechanism.
used”, thereby allowing us to replace a “not recently used” page.
Problem 1i[2pts]: The bottom half of a device driver responds to interrupts from the device.
putting waiting threads to sleep. The bottom half receives the interrupts and wakes waiting threads.
Problem 1j[2pts]: DNS is a centralized service for translating user-readable names into IP addresses.
queries (at least 2) starting with the right-most portion of the address (the top-level domain) and working to the left-most portion of the address.
Problem 2: Peer-to-Peer Instant Messaging [35pts]
This problem will investigate the construction of a peer-to-peer communication network built. We will utilize instant messaging as our application on top of this network. As you see by the above figure, clients connect with individual routers. Messages transit from a source client to a destination client via one or more hops through a router. In order to make this Instant Messaging client particularly user friendly, we will address messages to usernames (that are strings), rather than to IP addresses. This means that the job of the routers is to propagate information about usernames so that messages can be properly routed. When they first start up, clients will chose a router with which to connect. We will call this router the “home router” for the client. The client will then send a “route announcement” message to its home router containing the client’s username. The home router will forward these announcements to other routers and other clients. Further, the home router will send the identities of clients that it knows about to the new client. To send an Instant Message, a client will produce a packet that includes the username of the destination client, their own username (i.e. the source), and a message string. They will transmit this message to their home router. The message will be forwarded along the shortest path through routers to the message destination. It is important to note that clients are only allowed to communicate with their home routers, not other routers or clients.
We will use UDP/IP for our implementation. The packet format is shown above. UDP/IP communication is connectionless. To communicate on a given port, an application simply binds a socket to that port, after which it can immediately begin sending and receiving messages on that
Router A
Router B
Router C
Client 1
Client 3
Client 2
Client 5
Client 4
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
Router A
Router B
Router C
Client 1
Client 3
Client 2
Client 5
Client 4
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
Port 123
UDP Data
16-bit UDP length 16-bit UDP checksum
16-bit source port 16-bit destination port
16-bit identification
4 ToS 13-bit frag off
Total length(16-bits)
TTL Prot:UDP 16-bit header checksum 32-bit source IP address 32-bit destination IP address
IHL flags
0 15 16 31 IP Ver Time to Live (hops)
IP header 20 bytes
UDP header 8 bytes UDP Data
16-bit UDP length 16-bit UDP checksum
16-bit source port 16-bit destination port
UDP Data
16-bit UDP length 16-bit UDP checksum
16-bit source port 16-bit destination port
16-bit identification
4 ToS 13-bit frag off
Total length(16-bits)
TTL Prot:UDP 16-bit header checksum 32-bit source IP address 32-bit destination IP address
IHL 16-bit identification flags
4 ToS 13-bit frag off
Total length(16-bits)
TTL Prot:UDP 16-bit header checksum 32-bit source IP address 32-bit destination IP address
IHL flags
0 15 16 31 IP Ver Time to Live (hops)
IP header 20 bytes
UDP header 8 bytes
Problem 2c[3pts]: Now assume that our goal is to route Instant Messages with the fewest number of hops, and that some routers may boot after clients (adding shorter paths, for instance). We will use the ‘Dist’ parameter of route advertisement messages (Type=0) to indicate how many hops we have traveled from the client that we are advertising. Explain how the route advertisement messages will propagate through the system (be explicit) and what additional information you will keep in the routing tables. Be explicit.
Assume that clients will set the Dist parameter of their route advertisement to zero before transmitting it to their home router. Then, with each hop, the Dist parameter of the message will be incremented. Consequently, as route advertisement messages are routed through the system, the Dist parameter will say how many hops they have traveled from the client. Now, we will add a “hopCount” parameter to each routing table entry that says how many hops are left to get to the destination. When a route advertisement message arrives at a router, we will check to see if (1) either the destination is not in the table at all (as in 2b) or (2) there is an entry in the table with a larger “hopCount” value than the “Dist” parameter in the route advertisement. If either of these cases are true, we will put the entry in the table with the “Hops” parameter set to the current Dist value, increment the Dist value, then send the advertisement message to everyone we are connected to. NOTE: Again, we were looking for specifics. Some people talked in generalities without really telling us how to implement this idea.
Problem 2d[2pts]: What should happen when a new connection between two routers is initiated? Your answer should be consistent with (2c).
Each router should go through its routing table entries and generate route advertisement messages to send to the other router. When generating these messages, a router will set the “Dist” parameter to be one more than the “hopCount” parameter stored in its routing table entry. On receiving one of these new route advertisements, the router will follow exactly the same algorithm as given in (2c). NOTE: we were looking for people to tell us that (1) entries were exchange, (2) the distance parameters were incremented by 1 before exchanging to account for the additional hop.
Problem 2e[2pts]: Explain how we can utilize the ‘Dist’ field of Instant Messages (Type = 1) to guard against loops in the routing table, should they occur.
When generating a new Instant Message, a client will set the Dist field to zero. Each router will increment this value before sending the message on to the next hop. If the value is ever greater than some maximum value, the message will be dropped. NOTE: A number of people wanted to compare the distance in a packet to distances in the routing table. Unfortunately, if we have cycles in the table, it will be because the table is inconsistent. Consequently, we cannot be sure that we can trust the distance parameters to prevent problems. Instead, better to use a max hop count. Also, people who advocated this idea seemed to forget the fact that the distances in routing tables go DOWN with each hop; thus, any use of these values would have to start by setting the message Dist parameter of new messages to the distance value stored by the home router, then DECREMENTING the parameter with each hop.
Problem 2 continued: Peer-to-Peer Client
In the following, we are now going to implement our peer-to-peer router in Java. First, we need to establish a few things about the UDP sockets interface in Java . The Java interface for UDP is straightforward. In fact, it is easier to use the TCP interface. It uses a DatagramPacket object to hold the packet. DatagramPacket objects include a byte array for data as well as an IP address and port representing either the destination of the message (for outgoing messages) or the source IP address and port (for incoming messages). Datagram objects are changeable (mutable) after construction (see table, next page). Use of Java Datagram facilities is straightforward. For instance, a client could send the string “Hello world” as a datagram to port 66 of a server called “test.com” in the following way:
DatagramSocket mysock = new DatagramSocket(); // pick unused source port
// Need an array of bytes to send in a datagram String outs = “Hello World”; byte[] outbuf = outs.getBytes();
// Need to resolve the address of the server InetAddress outIP = InetAddress.getByName(“test.com”);
// Construct the outgoing packet DatagramPacket dp = new DatagramPacket(outbuf, outbuf.length, outIP, 66);
// Send packet to server test.com, port 66 mysock.send(dp);
Further, the components of a DatagramPacket can be mutated to send it elsewhere:
// send packet to echo.com, port 66 dp.setAddress(InetAddress.getByName(“echo.com”)); mysock.send(dp);
To receive messages on a particular port (say 66), we simply bind a socket to a particular port:
DatagramSocket serversock = new DatagramSocket(66); // wait on port 66
// Set up datagram to receive messages byte buff[] = new byte[256]; // space to receive packet DatagramPacket rp = new DatagramPacket(buff, buff.length);
// Wait forever until packet is received: serversock.receive(rp);
// These methods are available after reception: int length = rp.getLength(); // # bytes of data received InetAddress sourceIP = rp.getAddress();// IP address of sender int port = rp.getPort(); // Port of sender
The following code implements the IMPacket class to manipulates information encoded in packets.
**1. class IMPacket {
Line 15: byte[] data = result.getData(); data[0] = Type; data[1] = Dist; // Add first two bytes result.setLength(2); addString(result,Dest); // Add Dest if (Type==1) { // Ah – Type 1 add Src/Message addString(result,Src); // Add Src addString(result,Message); // Add Message } NOTE: we cannot just do something like Type.tostring() (or whatever – this is bad syntax!); converting an integer to a string produces a decimal string,,,
In order to keep track of nodes communicating with it, each router keeps a list of routers and clients that it has received route announcements from. Each entry in the list is a DirectEndpoint object. We can tell if we are talking to someone new by using the ‘lookup’ function:
If (!DirectEndpoint.lookup(addr,port)) { /* new endpoint */ }
We can add a new endpoint with ‘addUnique’. This will add a new entry to the list only it the endpoint has not be seen before. It will then return the unique result.
DirectEndpoint newend = DirectEndpoint.addUnique(addr,port);
Further, the set of all endpoints can be enumerated with the enumeration interface:
Enumeration allEndpoint = DirectEndpoint.elements(); While (allEndpoint.hasMoreElements()) { DirectEndpoint next = (DirectEndpoint)allEndpoint.nextElemenet(); /* Do Something with next entry*/ } Continuing with our code:
**31. class DirectEndpoint {
Line 42: int hopCount;
Line 43: int hopCount
Line 45: this.hopCount = hopCount;
H ere is the code to process route announcements:
**63. void HandleRoute( DatagramSocket socket,
// Walk through everyone we are talking to directly
**74. Enumeration allEndpoint = DirectEndpoint.elements();
Explain: Need to change routing entry if we are hearing about shorter path. Line 69: (newpacket.Dist < oldentry.hopCount)
Problem 2j[2pts]: Construct the new route table entry, line #70:
Line 70: new RouteEntry(source,newpacket.Dist);
Problem 2k[2pts]: Construct the new route announcement message as a DatagramPacket, line #73. Hint: you will be using the encode() method of IMPacket.
Line 73: newpacket.Dist++; // Increment distance outpacket = newpacket.encode(); // Encode it.
Last, but not least, here is the code to actually forward Instant Messages:
**81. void HandleIM( DatagramSocket socket,
Problem 2l[4pts]: Finally, provide the code that actually routes messages for line #85. This should have no more than 6 lines. Hint: stay consistent with your answer to (2e)
Line 85: if ((entry != null) && (newpacket.Dist++ < MAXIMUMHOP)) { DatagramPacket outpacket = newpacket.encode(); outpacket.setAddress(entry.nextHop.addr); outpacket.setPort(entry.nextHop.port); socket.send(outpacket); }
NOTE: we assume that MAXIMUMHOP is a constant defined somewhere.
Problem 2m[Extra Credit, 3pts]: The code that you have seen so far neglects to connect routers with one another. What is the simplest mechanism to connect a set of routers together at boot time? Assume that each router starts with an array of Strings that contains DNS names of other routers that it should connect with. Show a short loop (no more than 8 lines) that could be run at the time that the router boots in order to connect it with other routers.
String[] PeerRouters = {“first.com”, “second.com”, … }; /* What code goes here? */
// Put this code after line 49 (setup of server socket);
// Make up a dummy advertisement to tell routers about us IMPacket dummy = new IMPacket();
// These are defaults anyway, but just thought I’d point them out Dummy.Type = 0; dummy.Dist = 0; dummy.Dest = “”;
// Go through and announce ourselves/put other router into the table for (int i=0; i<PeerRouters.length; i++) { InetAddress addr = InetAddress.getByName(PeerRouters[i])); dummy.setAddress(addr); dummy.setPort(123); socket.send(dummy); DirectEndpoint.addUnique(addr,123); }
// Might also want to alter ‘HandleRoute()’ to avoid propagating dummy // route advertisement messages to other routers (although there would be // no harm in doing so.
Problem 3d[2pts]: Some operating systems provide a system call called “rename()” that can be used to give a file a new name. Assuming that the new directory that holds the name is on the same disk partition as the old one, is there any difference between using “rename()” to rename a file and just copying the file to a new file with the new name, followed by deleting the old one? Explain.
We admit that this might have been a bit confusing (hence our suggestion to think of “mv” instead of the “rename()” call). At any rate, rename() can be used to change the location of a file from one directory to another. It does so by altering directory entries but otherwise leaving the file inode and contents along. Thus, there is a difference: rename() is much more efficient than copying/deleting old file.
Problem 3e[2pts]: Continuing question (3d), let’s now assume that we are using “rename()” to change the name of a file from a directory hosted on one disk to a directory hosted on another disk, is there any difference between using this call to rename a file and just copying the file to a new file with the new name, folowed by deleting the old one? Explain.
Same announcement during exam as 3d. Here, since we are crossing from one disk to another, the inodes and datablocks are complete different. Thus, there is no more efficient way to change the name of a file across file systems than to copy/delete old version. Expected answer: there is no difference between rename() and copy/delete.
Problem 3f[2pts]: What are the main differences between non-blocking and asynchronous I/O?
Both non-blocking and asynchronous I/O return from the system call without delay. The difference is that non-blocking I/O returns immediately, regardless of the state of the requested I/O operation and could thus perform a partial read or write. The application writer may need to submit a non-blocking I/O operation multiple times to perform a complete I/O operation. Asynchronous I/O, on the other hand, notifies the user later when the operation has completed via a separate mechanism (such as a signal); asynchronous I/O will perform the complete I/O operation before notification.
Problem 3g[2pts]: What UNIX structure keeps track of the sectors allocated to a given file?
The inode structure.
Problem 3h[4pts]: Suppose that we have a disk with the following parameters:
We need to be careful to get all of our units correct. I am going to stick with milliseconds.
Tread = Controller + Seek + Rotational + Xfer Time
− ms
s s
bytes
bytes
min
revolutions
min
ms
ms ms 40 106 103
2 6 = 12.269 ms
Tread =12.269ms
Problem 3i[2pts]: Explain how the 6ms seek time from (3h) would actually have been measured (by the disk manufacturer) and why a smaller value would be more appropriate when modeling many operating systems.
The manufacturer number is computed by averaging the time to seek from one random track to another random track. Typical operating systems managed to use a lot more locality in their access paterns and thus their average seek time will be much lower.
Problem 3j [4pts]: Suppose that the average rate of requests from the operating system is 75 req/s and is memoryless. Given your numbers from (3h), what is the typical size of the disk queue required to handle this rate? Assume C=1.5 for disk service. What might the operating system do to reduce this queuing requirement?
This is just a straightforward use of the M/G/1 queue formula from class: Tq = ( ) u
u Tser C −
Where Tser is the service time from 3h, C=1.5, λ =75req/s, and u=λ×Tser. Since λ=75 req/s, u=λ×Tser =75 req/s×12.269ms×.001s/ms=0.
Tq = ( ) 1 0. 92
ms × + × =176.4ms=.1764 s
By Little’s law, length of q, Lq =λ×Tq=75 req/s×.1764s = 13.23 requests
By exploiting locality, the operating system could reduce Tser and thus the amount of queuing.
Problem 4: Potpourri [20pts] Please keep your answers short (one or two sentences per question-mark). We may not give credit for long answers.
Problem 4a[2pts]: What are the advantages and disadvantages of a fully-associative cache? When would it make sense to utilize a fully-associative cache?
The advantages of a fully-associative cache is that it doesn’t suffer from conflict misses; it can always hold the last N unique memory references, regardless of the reference pattern. The disadvantage of a fully-associative cache is that it is bigger and slower than a direct- mapped cache or a cache with lower than full associativity. It makes sense to utilize a fully- associative cache when the number of entries are small and the cost of conflict misses is high.
Problem 4b[3pts]: Explain why modern out-of-order processors (i.e. processors that execute instructions out of order) can still provide precise exceptions.
Modern processors provide a structure called a “reorder buffer” that helps with branch miss- predictions. The reorder buffer stores the result of instructions that complete out of order and allows them to be complete back in order in chunks. Effectively, the reorder buffer permits execution results to be flushed back to the point of the exception, thus providing a clean, precise interrupt point.
Problem 4c[3pts]: Assume that Alice and Bob have never met. Explain how a certificate authority (such as Verisign) could help Alice and Bob establish a secure channel between them which proves authenticity and maintains privacy.
Alice and Bob generate their own public/private key pairs for public key encryption. They then go separately to a certificate authority (CA) to prove their identities and get the CA to sign their public keys. Later, when Alice and Bob try to communicate, they start by providing their public keys and certificates to each other. Once they have established the authenticity of each other’s public keys, they then have a secure channel with which to communicate.
[this is not an efficient channel; for that, they would go the additional step of performing a key-generation protocol using the public keys to produce a shared symmetric key]
Problem 4d[3pts]: What does Two-phase commit allow you to guarantee? Why does this not violate the “General’s Paradox”?
Two-phase commit allows us to guarantee that all participants will agree to either commit or not commit some operation; eventually everyone will do the same time. This does not violate the ‘General’s Paradox’ since we make no guarantee about when everyone completes the operation, merely that it will happen eventually.
Problem 4e[4pts]: How does TCP/IP automatically adjust the rate at which it sends information so as to maximize bandwidth without overwhelming the slowest link? What happens if a link along the path of a TCP channel is suddenly very popular (thereby providing a small fraction of its previous bandwidth)? Does the TCP channel keep sending at the same rate? Why or why not?
It uses the TCP slowstart algorithm. It starts with a small transmission window, then increases the window size by one for each ACK received. When the network starts dropping packets, the slowstart algorithm divides the window size in half, then begins slowly increasing it again. In this way, TCP is able to find the largest window (and thus long-haul bandwidth) that doesn’t cause congestion. If a link suddenly becomes popular, this will cause packets to be delayed or dropped, causing TCP to shrink its window size, thus gracefully adapting to the new restrictions. Since the window size shrinks, TCP does not keep sending at the same rate.
Problem 4f[3pts]: Define what CSMA/CD stands for (e.g. in an Ethernet network) and explain how it adapts to a varying number of simultaneous communications. Why does a system with CSMA/CD saturate at a much higher fraction of available bandwidth than the original Aloha radio network.
It stands for “Carrier Sense, Multiple Access, Collision Detection”. When sending, the transmitter first figures out whether anyone is already transmitting (‘Carrier Sense’). If not, it sends data, but detects collision (‘Collision Dectection’).
This system adapts in the way that it handles collisions: it backs off for a random period of time before trying again. With each unsuccessful attempt, it doubles the expected wait period. Thus, it automatically adjusts the waiting period based on the number of simultaneous transmissions.
CSMA/CD saturates at a higher bandwidth because it does a better job of avoiding trashing transmissions that have already started (because of the Carrier Sense) and adapting the waiting time.
Problem 4g[2pts]: Explain what copy-on-write is (with respect to the virtual memory system) and give at least one instance when it might be useful.
Copy-on-write is a technique for copying a large chunk of memory from one address space into another. The copied set of memory pages are placed into the page table of the destination process and left in the page table of the source process. Both set of page table entries are changed to “read-only”. As long as neither process tries to write to the pages, this result is indistinguishable from an actual copy. If either the source or destination process attempts to write one of the pages, then we get a page fault, at which point we generate an actual copy page, clean up the page table entries so that they point at the two copies and are marked writable, then allow the write to continue. Thus, we only go to the trouble of copying pages if they will actually be modified by one or the other process.
Copy on write is useful for, among other things, supporting a traditional UNIX Fork operation that provides a complete copy of the parent’s address space to the child.