Resources: Semaphores, Synchronization


Problem 1 - The Dining Savages

page=115

Problem

Assume one thread (the cook) bulk reading requests from disk into a buffer (the pot) for a number of worker threads (the savages) to handle. When the pot is empty the cook must refill the pot. See the attached introduction to the “dining savages problem” from the little book of semaphores. (Page 121-122 of The Little book of Semaphores) Your task is to implement this system (pseudo-code is ok) using semaphores (this is hardest), monitors (like java or posix) and Adas protected objects.

1)

Problem

Show how you can make the synchronization between the savages and the cook with semaphores.

servings = 0
emptyPot = Semaphore(0)
fullPot = Semaphore(0)
mutex = Semaphore(1)
while True:
	emptyPot.wait()
	fillPot(n_servings)
	fullPot.signal()
	
While True:
	mutex.wait()
		if serving == 0:
			emptyPot.signal()
			fullPot.wait()
			servings = n_servings
		servings -= 1
		getServingFromPot()
	mutex.signal()
	eat()

2)

Problem

Show how you can make the synchronization between the savages and the cook with java, using synchronized, wait and notfy/notifyAll (posix version is also acceptable if this is more familiar to you)

Start one thread for the cook, and number of threads for the savage depending on the amount of savages:

int main();
	pthread_start(cook)
	
	for(i=0; i < M; i++)
		pthread_start(savage)
		
	while(1):
		doNothing
		
	return 0

^Not what the LF was looking for…

3)

Problem

Show how you can make the synchronization between the savages and the cook with Ada using protected objects; functions, procedures, entries w. guards.

???

Problem 2 - Cigarette smokers problem

page=101

1)

Problem

Read the attached page from “The little book of semaphores” about “Cigarette smokers problem”. (Page 107 of the little book of semaphores) Write pseudo-code with Adas protected objects to solve this problem. You are to sketch the protected object that has the responsibility to wake the correct “smoker”-thread. The agent is given and unchangeable; Assume that it calls a random pair of the three functions, procedures or entries (your choice) — Tobacco(), Paper() or Matches() — in our object to signal that the two given resources are avaiable each time the table is empty. You need to write only the protected object, but it should be clear how the “smoker”- threads are awoken.