








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this exam paper are: Physical Page, Statement Justification, Page Belongs, Physical Page, Interrupt Changes, User Mode, Kernel Mode, Context Switch, Scheduling Policy, Cpu Scheduling
Typology: Exams
1 / 14
This page cannot be seen from the preview
Don't miss anything!









University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science
CS 162 I. Stoica Spring 2010
FIRST MIDTERM EXAMINATION Tuesday, March 9, 2010
INSTRUCTIONS—READ THEM NOW! This examination is CLOSED BOOK/CLOSED NOTES. There is no need for calculations, and so you will not require a calculator, Palm Pilot, laptop computer, or other calculation aid. Please put them away. You MAY use one 8.5” by 11” double-sided crib sheet, as densely packed with notes, formulas, and diagrams as you wish. The examination has been designed for 100 minutes/100 points (1 point = 1 minute, so pace yourself accordingly). All work should be done on the attached pages.
In general, if something is unclear, write down your assumptions as part of your answer. If your assumptions are reasonable, we will endeavor to grade the question based on them. If necessary, of course, you may raise your hand, and a TA or the instructor will come to you. Please try not to disturb the students taking the examination around you.
We will post solutions to the examination as soon as possible, and will grade the examination as soon as practical, usually within a week. Requests for regrades should be submitted IN WRITING, explaining why you believe your answer was incorrectly graded, within ONE WEEK of the return of the examination in class. We try to be fair, and do realize that mistakes can be made during the regarding process. However, we are not sympathetic to arguments of the form “I got half the problem right, why did I get a quarter of the points?”
__________________________________________ SID: _______________________________ (Signature)
__________________________________________ Discussion Section (Day/Time): ________ (Name—Please Print!)
QUESTION POINTS ASSIGNED POINTS OBTAINED 1 20
2 20
3 20
4 20
5 20
6 (bonus question) 5
TOTAL 100 (+5)
Question 1. (20 points)
True or False (12 points). Instructions: write “true” or “false” after each of the following statements (Please give one statement justification for each of your answers).
a. (2 points) Each physical page belongs to only one process.
b. (2 points) Every interrupt changes the CPU from user mode to kernel mode.
c. (2 points) All Page Table entries will be invalid directly after a context switch between processes.
d. (2 points) The CPU scheduling policy that minimizes average completion time can lead to starvation.
e. (2 points) If there is only one CPU in the computer, then at most one process can be in the “running” state at any time.
f. (2 points) The ”Shortest-Job-First” policy always results in the lowest average completion time in a batch system.
Question 2. Threads and Synchronization (20 points)
(a) (4 points) Give two reasons of why disabling interrupts can be a bad way to implement critical sections.
(b) (6 points) Assume you are given the CompareAndSwap atomic primitive defined (in C) as follows:
bool CompareAndSwap(int* value, int from, int to) { if (*value == from) { *value = to; return true; } return false; }
Consider the following implementation of lock acquire using CompareAndSwap:
void acquire() { while(!CompareAndSwap(&value, UNLOCKED, LOCKED)); }
i) Give one reason why this implementation may not be efficient.
ii) Without preemption, is this still a valid implementation of acquire()? Why? (Use no more than two sentences for your explanation.)
(c) (8 points) Complete the following implementation of the P() and V() functions for a semaphore using condition variables using Mesa semantics.
void P() { lock.acquire();
value -= 1;
void V() {
cond.wake();
lock.release(); }
(d) (2 points) How could you simplify your implementation if you were given condition variables that follow Hoare semantics instead of Mesa semantics? Use no more than two sentences to explain your answer.
ii) (4 points) Assume you are allowed to add an extra instance of either resource R1 or R2. Are the tasks deadlocked in this case? If yes, explain how each of the four deadlock conditions is satisfied. (Use one sentence per condition.) If not, give a possible sequence in each the tasks execute.
(c) Consider the network below implementing wormhole routing between any two nodes. Each node is connected to six of its neighbors by two network links, one in each direction. Messages are routed from a source node to a destination node and can stretch through the network (i.e. consume links along the route from source to destination). Messages can cross inside nodes. Assume that no network link can service more than one message at a time, and that each message must consume a continuous set of channels (like a snake). Hint: You can think about a message as a train that spans multiple links (possible all the way from source to destination), and of each link as a one-way rail.
i) (4 points) Show a situation (with a drawing) in which messages are deadlocked and can make no further progress. Explain how each of the four conditions of deadlock is satisfied by your example.
ii) (4 points) Define a routing policy that avoids deadlocks in the network of (i). Explain using at most two sentences why your routing policy avoids deadlock.
c) (5 points) Now assume that P1 arrives at t=0, P2 arrives at time t=10min, and P3 at time t=20min. What will be the completion time of each process when using the Shortest Remaining Time First (SRTF)?
d) (5 points) Assume now that P3 is performing an I/O operation every 10ms and that the I/O operation takes 40ms, but would still take 1 minute to run if it was the only process running. Assume that P1, P2, and P3 are submitted at the same time and you use Round- Robin scheduling (like in part (b)).
i) How long does it take P3 to complete if the time slice is 100ms?
ii) What time slice length would minimize the completion time of P3?
Question 5. Page Tables & TLBs (20 points)
Orange Inc hires you to design the virtual memory system for a new cell phone with 32-bit virtual and physical addresses, in which memory is allocated in 2 KB pages. Suppose that you decide to use a single-level page table, in which you also store three metadata bits for each page: Writable, Executable and Valid.
(a) (4 points) Answer the following questions, briefly explaining your solution:
i) How long, in bits, is a virtual page number?
ii) How long, in bits, is a physical page number?
iii) How long, in bits, is an offset within a page?
iv) How much memory is needed to store the page table of each process?
(b) (4 points) Your manager asks you to consider using a multi-level page table in your design. Explain one advantage and one disadvantage of multi-level page tables over single-level page tables. (Use no more than four sentences in total.)
Instruction TLB Hit?
Success? Physical Address
New TLB State
0x
Virtual Page #
Physical Page #
Writable? Valid?
0x
Virtual Page #
Physical Page #
Writable? Valid?
0x
Virtual Page #
Physical Page #
Writable? Valid?
0xFFFFF
Virtual Page #
Physical Page #
Writable? Valid?
0x
Virtual Page #
Physical Page #
Writable? Valid?
0x
Virtual Page #
Physical Page #
Writable? Valid?
Question 6 (BONUS). Thread Interleavings (5 points)
The following Java code implements insertion into a binary tree:
void insert(Node node, int value) { if (value <= node.value) { if (node.left == null) { node.left = new Node(value); } else { insert(node.left, value); } } else { if (node.right == null) { node.right = new Node(value); } else { insert(node.right, value); } } }
Unfortunately, this implementation is not thread-safe. Suppose that we start with a binary tree containing the values 3, 5, and 7, organized as shown above, and that we have a reference root to the root node (the one containing 5). We then launch the two threads below, each of which tries to insert two values into the binary tree. What are the possible outcomes after both threads finish? Draw each possible resulting tree in a format like the starting tree above. (You may continue on the back of this page if necessary.)
Thread 1 Thread 2 insert(root, 0); insert(root, 2);
insert(root, 1); insert(root, 6);
root