ProvaTeorica 2005.11.04

From Sistemi Operativi
Jump to navigation Jump to search

TESTO COMPITO

CONCORRENZA

Esercizio 1

monitor casino
{
	bool playing, exEquo;
	int max, n, leaving;
	condition wantToPlay, player;

	playing = exEquo = false;
	n = 0;

	void misiedo()
	{
		if (playing)
			wantToPlay.wait();
		// Rendez-vous mechanism
		if (++n < 5)
			player.wait();
		else
		{
			playing = true;
			max = 0;			
		}
		player.signal();
	}

	int gioca(int number)
	{
		if (number == max)
			exEquo = true;
		else if (number > max)
		{
			exEquo = false;
			max = number;
		}	
		if (--n > 0)
			player.wait();
		else
			leaving = 5;
		player.signal();
		return (!exEquo && number == max)? 4 : -1;
	}

	void mialzo()
	{
		if (--leaving == 0)
			playing = false;
		wantToPlay.signal();
	}
}

Esercizio 2

def ssend2(m, d1, d2):
	msg = ( SND, getpid(), d1, d2, m )
	asend(msg, server)
	return arecv(server)

def sreceive():
	msg = ( RCV, getpid(), None, None, None )
	asend(msg, server)
	return arecv(server)

def server():
	while True:
		flag, src, d1, d2, msg = arecv(*)
		if flag == RCV:
			msg, sender, _, _ = dbSnd.retrieve(query = '(?, ?, src, ?) or (?, ?, ?, src)')
			if msg:
				asend(msg, src)
				asend(src, sender)
			else:
				dbRcv.insert(src)
		else:
			if dbRcv.retrieve(d1):
				asend(msg, d1)
				asend(d1, src)
			elif dbRcv.retrieve(d2):
				asend(msg, d2)
				asend(d2, src)
			else:
				dbSnd.insert((msg, src, d1, d2))

Esercizio 3