
















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!

















think clearly about a problem, you will probably have time to write your answer legibly.
I have not received advance information on the content of this 15-410 mid-term exam by dis- cussing it with anybody who took part in the conflict exam session or via any other avenue.
Signature: Date
One feature of monitors is that the compiler automatically adds most necessary synchronization code in a boring, mechanical way to avoid programmer mistakes. However, it is arguably possible to obtain much of the same benefit even in C without automatic compiler support by coding according to a “monitor pattern.” In this approach, C macros inserted in standard places will generate the synchronization code in a regular way. As long as the programmer uses the macros correctly, the result should be correct code (at least as far as synchronization is concerned!).
Here are the basic macros for monitor setup, entry, and exit.
Macro Purpose M DECL() appears in monitor source file before any declarations or code M INIT() must be called before the monitor is used M ENTER programmer must insert this at every monitor entry point M RET programmer must use this instead of “return;” M RETURN(t,v) t is the C type of value v; programmer must use this instead of “return(v);”
These macros are used if condition variables are necessary.
MC DECL(name) declares a monitor condition variable MC INIT(name) initializes a declared monitor condition variable MC WAIT(name) waits for a monitor condition variable to be signalled MC NOTIFY(name) signals a monitor condition variable
The code example below will further explain the meanings and usages of the macros we will consider.
/* Monitor for doing work in background:
#define TRUE 1 #define FALSE 0
M_DECL(); MC_DECL(activity);
item_t in_box, out_box; int in_box_empty = TRUE, out_box_full = FALSE;
/* provide some work to be done in background */ void work_dropoff(item_t item) { M_ENTER;
while (!in_box_empty) MC_WAIT(activity); in_box = item; in_box_empty = FALSE; MC_NOTIFY(activity);
M_RET; }
/* get the results as soon as they are ready */ void work_pickup(item_t *item) { M_ENTER;
while (!out_box_full) MC_WAIT(activity); *item = out_box; out_box_full = FALSE; MC_NOTIFY(activity);
M_RET; }
(b) 2 points Please show the code for M DECL() and M INIT(). Don’t worry too much about the exact macro syntax (multi-statement macros in C are not intuitive), but make it very clear to your grader what each macro does and that it can be done in the C language.
(c) 3 points Please show the code for M ENTER, M RET, and M RETURN(t,v). Don’t worry too much about the exact macro syntax (multi-statement macros in C are not intuitive), but make it very clear to your grader what each macro does and that it can be done in the C language.
Imagine that a special-purpose multi-threaded application will never have more than 31 threads, which will always have thread id’s ranging from 1 to 31 (inclusive). A developer for that system proposes the following lock-acquisition code, inspired by the famous Bakery Algorithm, but tuned according to the observation that the number of threads happens to fit in the number of bits in a word.
In this algorithm, the bits of one integer, want, indicate the set of threads attempting to acquire the lock. The thread that is granted the lock is indicated by the variable turn, which at any time has exactly one bit set; the position of that bit indicates the id of the thread owning the lock. This bit is rotated through the possible thread id’s to ensure fair selection.
Because this system is very limited in scope, it contains exactly one critical region and thus one lock. Whenever a thread performs an operation on the single lock, it passes in its thread id (ranging from 1 to 31).
int want = 0; /* set of threads wanting the lock, begins empty / int turn = 1; / id-mask of thread that owning the lock; a value
#define set(x, i) ((x) |= (1 << (i))) #define clr(x, i) ((x) &= ~((1 << (i)))) #define mybit(i) (1 << (i))
/* acquire a lock, given a thread id from 1 to 31 */ void lock_acquire(int thread_id) { int self = mybit(thread_id);
set(want, thread_id); /* indicate we are waiting for lock */
/* spin waiting if it is someone else’s turn */ while ((turn != self) && (turn != 1)) continue;
if (turn == 1) /* it’s no one’s turn / turn = self; / now it’s my turn / / postcondition: turn == self */ }
/* release a lock / void lock_release(int thread_id) { clr(want, thread_id); int new_turn = turn; if (want) { / give lock to next waiting thread. Threads
For each of the critical-section algorithm requirements, either argue that it does or describe a scenario in which it does not. In the latter case, use the format presented in class, as exemplified in the table below. If you are trying to show a repeating pattern, be sure to show “enough” steps that the pattern is evident. Likewise, you do not need to show all 31 threads; you need to show enough to make your point but need not show any more. T0 T set(want, id) clr(want, id);
(b) 5 points Does this algorithm ensure progress? Briefly argue that it does, or show an execution trace indicating that it does not.
(c) 5 points Does this algorithm ensure bounded waiting? Briefly argue that it does, or show an execution trace indicating that it does not.
Yesterday Cluster Services began a project to construct a new Linux “collaboration mini-cluster” on campus (yay!). While they have a design in hand, they are uncertain as to how well it will work. Sensing an opportunity to make use of a room full of concurrency experts, they have asked Prof. Eckhardt to subject their design to analysis by an exam session full of 410 students.
Here is a diagram showing the layout of the proposed cluster.
S S S
C /|
C P C | | C C \ / C
C = Computer, P = Projector, S = Server
Note that some resources are physically adjacent, represented by edges in the graph above. The projector is plugged into one computer and can project images from only that computer. All computers in the room are connected by a wireless network (which contributes no lines to the diagram). Each workstation computer, server, and projector can be used by only one person at a time.
Different groups utilize the cluster. OS students work in two-person teams, and require two adjacent computers so they can collaborate closely. From time to time Networks students will also come in and use the cluster. Each two-person Networks team requires one server and two workstation computers, but the computers do not need to be adjacent (the whole point of a Networks class is for computers which are not adjacent to send each other messages; in this case, each Networks group runs an IRC chat application server on the server computer they own).
The behavior of the OS students is as follows. One group member comes into the cluster and randomly selects a pair of adjacent computers, whether or not they are in use. If one of the selected computers is connected to the projector, the student will wait for that computer to open up, acquire that computer, wait for the projector to become available, watch one movie, release the projector, and then wait patiently for the partner to arrive. If neither computer in the pair selected is connected to the projector, the first student will simply wait for the left-most (or “clock-wise”) computer to become available, acquire it, and wait patiently for the partner. The partner, upon arriving, will wait for the first team member’s acquisition steps to complete, and then will wait for the other computer in the pair to become available, and acquire it. At this point the two students will work furiously on their OS project until their computron supply is exhausted, at which they will give up their computers and stagger out of the cluster.
The behavior of Networks students is as follows. The first partner of a team arriving at the cluster will wait until any server in the server pool is available. Once a server is acquired, the student will wait until any cluster computer is or becomes available, and will allocate it. The student will read cmu.misc.market until the partner arrives and acquires any other cluster computer (the computers need not be adjacent).
(b) 5 points Assume that there is a week in the semester during which a Networks assign- ment is in progress but no OS assignment is.^2 Either demonstrate (through the use of a process/resource graph and an associated resource-request event trace) that Networks students can deadlock, or explain why they cannot (i.e., state which deadlock condition(s) they avoid).
(^2) Please excuse the counter-factual assumption.
(c) 10 points Due to a scheduling oversight, the due date for the Networks routing-daemon project and the OS thread-library project are the same. As a result, both Networks and OS students attempt to use the cluster at the same time. Either demonstrate (through the use of a process/resource graph and an associated resource-request event trace) that the mixture of OS and Networks students can deadlock, or explain why they cannot (i.e., state which deadlock condition(s) they avoid).