{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2003fall

# 2003fall - CS414 Prelim 1 10:15 to 11:25 Four problems 25...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS414 Prelim 1. 10:15 to 11:25. Four problems, 25 points each. Closed book. Your name: ___________________Your ID : _______________________ 1. Any Ithaca driver soon becomes familiar with our one-lane bridges. When cars want to cross in both directions, people take turns. All the cars going one way cross, than all the ones going the other way, and so forth. Ithaca being a small city, we never have an “infinite” supply of cars, so this works. One lane bridges have a weight limit. The bridge can only hold 2 cars at one time. Assume that cars are processes. Modify the readers and writers monitor (code is given below to remind you how it looks) into a new monitor with four methods, “ go_north_wait,” “go_north_done,” “go_south_wait”, and “go_south_done.” A car going north calls go_north_wait when it gets to the bridge, then crosses, then calls “go_north_done”, and similarly for a car heading south. int rcnt = 0, rwaiting = 0, wcnt = 0, wwaiting = 0; condition wantread, wantwrite; public StartRead() { if(wcnt > 0 || wwaiting > 0) { ++rwaiting; wantread.wait();--rwaiting; } rcnt++; wantread.signal(); } public StartWrite() { if(wcnt == 1 || rcnt > 0) { ++ wwaiting; wantwrite.wait();--wwaiting; } wcnt = 1; } public EndRead() { if(--rcnt == 0) wantwrite.signal(); } public EndWrite() { wcnt = 0; if(rwaiting > 0) wantread.signal(); else wantwrite.signal(); } int scount=0, ncount=0, nwaiting=0, swaiting=0; condition wantsouth, wantnorth; public go_north_wait() { if((scount > 0)|| (ncount==2)) { ++nwaiting; wantnorth.wait();--nwaiting; } ncount++; if(ncount < 2) wantnorth.signal(); } public go_south_wait() { if((ncount > 0)|| (scount==2)) { ++swaiting; wantsouth.wait();--swaiting; } scount++; if(scount < 2) wantsouth.signal(); } public go_north_done() { ncount--; if (nwaiting > 0) wantnorth.signal(); else if(ncount == 0){ wantsouth.signal(); } public go_south_done() { scount--; if (swaiting > 0) wantsouth.signal(); else if(scount == 0){ wantnorth.signal(); } 2. Below is the Bakery algorithm, using the same code and notation we employed in class. Recall that (a,x)<(b,y) means “(a < b) or (a=b and x<y)”. Assume that N=6 #define true 1 #define false int number[N], choosing[N]; /* Initially number[i] = 0, choosing[i] = false */ Process P i : while (1) { do something else choosing[i] = true; number[i] = max(number[0]…number[N-1])+1; choosing[i] = false; for(k = 0; k < N; ++k) { while(choosing[k] ) loop; while((number[k]!=0)&&((number[k],k) < (number[i],i))) loop; } Critical Section for Process i number[i] := 0; } Suppose that a) process 0 is in the “do something else” code b) process 2 enters the critical section, with number[2] = 5 c) processes 1, 3, 4 and 5 could be anywhere that this code will allow. Now suppose that process 0 finishes the “do something else code” while process 2 is still in the critical section. The next line that process 0 will execute is “choosing[0] =still in the critical section....
View Full Document

{[ snackBarMessage ]}

### Page1 / 11

2003fall - CS414 Prelim 1 10:15 to 11:25 Four problems 25...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online