Docsity

Operating System Concepts: Thread Control Block & Atomic Transfers, Exams of Operating Systems

Toggle sidebar
bg1
1
CS 162 Midterm Exam
March 1996
Note that exam total is out of 70, not out of 100. Answers in italics.
Your Name: Ms. Perfect
Your TA:
Your Section:
General Information:
This is a closed book examination. You have 90 minutes to answer
as many questions as possible. The number in parentheses at the
beginning of each question indicates the number of points given to
the question; there are 70 points in all. Write all of your answers
directly on this paper. Make your answers as concise as possible
(you needn't cover every available nano-acre with writing). If there
is something in a question that you believe is open to interpretation,
then please go ahead and interpret but state your assumptions in
your answer.
Problem 1: (5 points)
Of the following items, circle those that are stored in the thread
control block.
Answer: a, c, f, g
(a) CPU registers
(b) page table pointer
(c) stack pointer
(d) ready list
(e) segment table
(f) thread priority
(g) program counter
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Operating System Concepts: Thread Control Block & Atomic Transfers and more Exams Operating Systems in PDF only on Docsity!

CS 162 Midterm Exam March 1996

Note that exam total is out of 70, not out of 100. Answers in italics.

Your Name: Ms. Perfect

Your TA:

Your Section:

General Information:

This is a closed book examination. You have 90 minutes to answer as many questions as possible. The number in parentheses at the beginning of each question indicates the number of points given to the question; there are 7 0 points in all. Write all of your answers directly on this paper. Make your answers as concise as possible (you needn't cover every available nano-acre with writing). If there is something in a question that you believe is open to interpretation, then please go ahead and interpret but state your assumptions in your answer.

Problem 1 : (5 points)

Of the following items, circle those that are stored in the thread control block.

Answer: a, c, f, g (a) CPU registers

(b) page table pointer

(c) stack pointer

(d) ready list

(e) segment table

(f) thread priority

(g) program counter

Problem 2 : (10 points)

Write down the sequence of context switches that would occur in Nachos if the “main” thread were to call the following code. Assume that the CPU scheduler runs threads in FIFO order, with no time- slicing and all threads having the same priority. The WillJoin flag is used to signify that the thread will be joined to by its parent. For example, “child2 => child1” would signify that child2 switches to child1.

void Thread::SelfTest2() { Thread *t1 = new Thread("child 1", WillJoin); Thread *t2 = new Thread("child 2", WillJoin);

t1->Fork((VoidFunctionPtr) &Thread::Yield, t1); t2->Fork((VoidFunctionPtr) &Thread::Yield, t2); t2->Join(); t1->Join(); }

main -> child1 -> child2 -> child1 -> child2 -> main (If you assumed an implementation of join that V’ed the child to ask it to finish itself, then there would be an additional couple switches at the end -- we didn’t take off for this..)

Note that neither creating nor forking a thread switches to the child thread (assuming no preemptions occur)-- the parent keeps the CPU throughout, and just puts the child on the ready list. In the above example, the children don’t start running until “main” calls join.

Also note that when I fork the function Yield, when Yield runs, it will first call switch, then come back from switch, then the thread finishes. A common error was to assume that once Yield called switch, it never came back.

We gave some people half credit if we could deduce that you made only one of these errors.

Answer to problem 3: This is dangerous. There are actually 3 things wrong with the implementation:

  1. if child finishes before parent gets to join, thread is deleted (along with semaphore used for synchronizing the parent and child). So when parent calls join, it is referencing a deallocated data structure (and someone else may in the meantime have allocated it for some other purpose, overwriting the semaphore value).
  2. regardless of whether the parent gets to join before or after the child finishes, the child deletes its thread while still running in the context of the thread. This means the thread is still referencing its stack, even though the space for the stack may have been reallocated for some other purpose (and thus things like procedure call and return won’t work properly).
  3. There is no guarantee in Thread::Finish that there is another thread to run; thus, FindNextToRun might return NULL.

All three of these errors only show up sometimes, if exactly the right sequence of events occurs. For example, using data that has been deleted is dangerous, since it will only show up if the memory allocator hands out that data region again. You won’t get an error if the memory allocator hands out some other data region.)

We were mainly looking for the first 2, so if you got both of those, and your fix worked, we gave you full credit. The simplest fix is to move the “delete this” to the last line in Thread::Join().

The most common error was noticing only one of the problems; we gave half credit if you came up with either of the first two problems.

Among the other common errors: (1) using an extra semaphore to have the child thread wait until the parent called join. This fixes #1, but not #2. (2) saying that the problem was that interrupts were never reenabled -- in Nachos, as in other systems, the thread that wakes up after a SWITCH is responsible for reenabling interrupts. (3) not realizing that Join is an operation on the child thread, even though the parent thread is the one that calls Join -- both Join and Finish operate on the same object and thus use the same semaphore.

Problem 4: (10 points)

For the following implementation of atomic transfer , say whether it either (i) works, (ii) doesn’t work, or (iii) is dangerous -- that is, sometimes works and sometimes doesn’t. If the implementation does not work or is dangerous, explain why and show how to fix it so it does work.

The problem statement is as follows: The atomic transfer routine dequeues an item from one queue and enqueues it on another. The transfer must appear to occur atomically: there should be no interval of time during which an external thread can determine that an item has been removed from one queue but not yet placed on another. In addition, the implementation must be highly concurrent -- it must allow multiple transfers between unrelated queues to happen in parallel. You may assume that queue1 and queue2 never refer to the same queue.

void AtomicTransfer (Queue *queue1, queue2) { Item thing; / thing being transferred */

queue1->lock.Acquire(); thing = queue1->Dequeue(); if (thing != NULL) { queue2->lock.Acquire(); queue2->Enqueue(thing); queue2->lock.Release(); } queue1->lock.Release(); }

This is dangerous, since it may (but does not always) lead to deadlock. If one thread transfers from A to B, and another transfers from B to C and another from C to A, then you can get deadlock if they all acquire the lock on the first queue before any of them acquire the second.

The best fix from the point of view of maximizing concurrency is to use resource ordering -- to acquire both locks at the beginning of the routine, lowest addressed queue first:

Problem 5: (15 points)

A c o u n t e r m e a s u r e is a strategy by which a user (or an application) exploits the characteristics of the CPU scheduling policy to get as much of the CPU time as possible. For example, if the CPU scheduler trusts users to give accurate estimates of how long each job will run so that it can give high priority to short jobs, then a countermeasure would be for the user to tell the system that the user’s jobs are always short (even if untrue).

Devise a countermeasure strategy for each of the following CPU scheduling policies; your strategy should minimize an individual application’s response time (even if it hurts overall performance). You may assume perfect knowledge -- for example, your strategy can be based on which jobs will arrive in the future, where your application is in the queue and how long the jobs ahead of you will run before blocking. Your strategy should also be robust -- it should work properly even if there are no other jobs in the system, or there are only short jobs, or only long running jobs, etc. If no strategy will improve your application’s response time, then indicate that.

Note that this question asked how you could improve a single job’s response time, not how you could improve average response time across all jobs. Also note that the question is impractical in that it assumes you can have perfect knowledge; in practice, countermeasures are not as efficient as the ones described below.

(a) last in first out

If you assume LIFO is non-preemptive, then the best countermeasure is to insert your job onto the queue just before the currently running job finishes. The problem with this is what if your job does I/O? It would be hard to arrange for your I/O to complete just before another job finishes, which leads to the kill & restart approach outlined below.

If you assume LIFO is preemptive, then if another thread preempts your job (and that thread will take a long time), then you can kill your job and restart it -- the restarted job will go on the front of the queue. Alternatively, you might have your job, right before it is preempted, create a copy of itself and kill the original. If the copy does a brief Alarm::WaitUntil, it can leap over the arriving job back to the front of the line.

(b) round robin, assuming jobs are always put at the end of the ready list when they become ready to run

We gave full credit if you said no countermeasure possible. However, a few of you came up with clever solutions -- for instance, carving up your job into pieces, each of which runs for less than a time slice.

(c) multilevel feedback queues, where jobs are always put on the highest priority queue when they become ready to run

Similar to (a), one approach is, right before a higher priority job would preempt you, fork a job to run the remaining part of your job (or do a little bit of I/O at this point) This way, your job would always be high priority. Many suggested always doing I/O right before getting time-sliced, but this is inefficient if you are the only runnable job -- remember multilevel feedback timeslices every job to drop its priority, even if there is no one else at that priority level. We gave a large share of credit for this. Some simply said, do lots of I/O; again, that will work, but it is inefficient.

As with (b), another possible countermeasure is carving up your job into pieces, each of which runs for less than the minimum time slice.

Problem 6 (a) Here is a proposed solution to the “H20” problem:

int numHydrogen = 0; Semaphore pairOfHydrogen(0); // init 0 Semaphore oxygen(0); // initially 0

void hReady() { numHydrogen++; if ((numHydrogen % 2) == 0) { pairOfHydrogen->V(); } oxygen->P(); }

void oReady() { pairOfHydrogen->P(); makeWater(); oxygen->V(); oxygen->V(); }

This solution is dangerous. Threads calling hReady() access shared data without holding a lock! Many of you talked about what would happen if you got a timeslice after the increment and before the test, but remember that you can also get a timeslice in the middle of a C statement. For example, you could have N threads both increment numHydrogen, and because they do so without a lock, the result could be 1 instead of N -- in other words, no water would be made regardless of how many H’s arrived.

The solution is to put a lock acquire before the first line in hReady, and release before the Semaphore::P. Some put the lock acquire after the increment, and that simply doesn’t work!

Problem 6 (b) Another proposed solution to the “H20” problem:

Semaphore hPresent(0); // initially 0 Semaphore waitForWater(0); // initially 0

void hReady() { hPresent->V(); waitForWater->P(); }

void oReady() { hPresent->P(); hPresent->P(); makeWater(); waitForWater->V(); waitForWater->V(); }

This is dangerous, since it may lead to starvation. If two H’s arrive, then the value of the hPresent semaphore will be 2. If two O’s arrive, then they can each decrement hPresent, before either can decrement it twice. So no water is made, even though enough atoms have arrived.

The fix is to put a lock acquire before the first line in oReady, and a lock release after the two semaphore:P’s. This way, only one oxygen looks for waiting H’s at a time -- if there aren’t enough H’s for the first oxygen, there won’t be enough for any of the later oxygens either.