February 7 2007

Anything you guys want to go over in particular? We can talk about homework problems or the midterm review posted on the class webpage if you like.

- (leftover from lecture) Suppose we have the following code
int x = 0; void AddToX() { for(i=0; i < 100; i++) { x++; } } void SubFromX() { for(j=0; j < 100; j++) { x--; } }

and we have one thread running

`AddToX()`while another thread runs`SubFromX()`. What is the range of possible final values for x? - (leftover from lecture)
*Event Synchronization*:`Event::Wait()`blocks if and only if the Event is unsignaled.`Event::Signal()`marks the Event as signaled, and wakes up blocked threads. Once signaled, an Event remains signaled. Define`Event::Wait()`and`Event::Signal()`with a monitor, then with semaphores. *Restaurant*: We have a restaurant with N chairs and no tables. Customers come and go, but they arrive in parties of M customers. A party must always be seated at the same time, so if there are 4 chairs available, and a party of 5 arrives, the party must wait. On the other hand, if a party of 3 arrives, and 4 chairs are available, they can pull together 3 chairs and sit.After the party is seated, they eat for a while, and then they leave, allowing more customers to enter. Clearly, there can never be more than N seated customers at any time, because the restaurant only has N chairs.

Each party of M customers is represented by a thread that executes the following code:

SitDown(M) // eat StandUp(M)

`SitDown(M)`indicates that a party of size M wants to sit down. A party will start eating as soon as`SitDown`returns, so`SitDown`must block until M seats are available.`StandUp(M)`indicates that a party of size M wants to stand up, freeing M chairs for waiting customers.Define

`SitDown`and`StandUp`first using monitors, then using semaphores.Does your solution guarantee that parties of the same size are served in FIFO order? That is, if a party of 2 arrives, then later another party of 2 arrives, can you guarantee that the first party of 2 will always sit first? If not, how would you enforce this policy?