Difference between revisions of "ProvaTeorica 2005.11.04"

From Sistemi Operativi
Jump to navigation Jump to search
(Created page with "=[http://www.cs.unibo.it/~renzo/so/compiti/2005-11-04.con.pdf TESTO COMPITO]= =CONCORRENZA= ==Esercizio 1== <syntaxhighlight lang="C"> monitor casino { bool playing, exEquo; ...")
 
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
{
 
{
 
bool playing, exEquo;
 
bool playing, exEquo;
int max, n, leaving;
+
int max, n, waiting;
 
condition wantToPlay, player;
 
condition wantToPlay, player;
  
 
playing = exEquo = false;
 
playing = exEquo = false;
n = 0;
+
n = waiting = 0;
  
 
void misiedo()
 
void misiedo()
 
{
 
{
 
if (playing)
 
if (playing)
 +
{
 +
waiting++;
 
wantToPlay.wait();
 
wantToPlay.wait();
// Rendez-vous mechanism
+
waiting--;
 +
}
 
if (++n < 5)
 
if (++n < 5)
 
player.wait();
 
player.wait();
Line 22: Line 25:
 
{
 
{
 
playing = true;
 
playing = true;
max = 0;
+
max = 0;
 
}
 
}
 
player.signal();
 
player.signal();
Line 39: Line 42:
 
player.wait();
 
player.wait();
 
else
 
else
leaving = 5;
+
n = 5;
 
player.signal();
 
player.signal();
 
return (!exEquo && number == max)? 4 : -1;
 
return (!exEquo && number == max)? 4 : -1;
Line 46: Line 49:
 
void mialzo()
 
void mialzo()
 
{
 
{
if (--leaving == 0)
+
if (--n == 0)
playing = false;
+
{
wantToPlay.signal();
+
while (waiting > 0 && n < 4)
 +
wantToPlay.signal();
 +
 +
if (waiting > 0)
 +
wantToPlay.signal();
 +
else
 +
playing = false
 +
}
 
}
 
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
==Esercizio 2==
 
==Esercizio 2==
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
Line 86: Line 97:
 
==Esercizio 3==
 
==Esercizio 3==
 
<syntaxhighlight lang="Python">
 
<syntaxhighlight lang="Python">
global = 3
 
 
CSenter:
 
while True:
 
local = 2
 
QR(global, local) # QR(x,y) = <y1=x%y; x1=x/y; x=x1; y=y1;>
 
if local == 3/2:
 
break
 
 
CSexit:
 
global = 3
 
  
'''
 
Osservazioni:
 
(1) La funzione atomica risulta idonea a sostituire la Test&Set.
 
(2) La variabile globale assume i seguenti valori:
 
    3, quando la Critical Section e' libera;
 
    1, quando la Critical Section e' !libera.
 
(3) Si alternano i seguenti 4 stati (<global, local>):
 
  (da libero ad occupato)  <3, 2> --> <1, 3/2>
 
  (da occupato ad occupato) <1, 2> --> <1, 1/2>
 
'''
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 20:23, 18 May 2014

TESTO COMPITO

CONCORRENZA

Esercizio 1

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

	playing = exEquo = false;
	n = waiting = 0;

	void misiedo()
	{
		if (playing)
		{
			waiting++;
			wantToPlay.wait();
			waiting--;
		}
		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
			n = 5;
		player.signal();
		return (!exEquo && number == max)? 4 : -1;
	}

	void mialzo()
	{
		if (--n == 0)
		{	
			while (waiting > 0 && n < 4)
				wantToPlay.signal();
			
			if (waiting > 0)
				wantToPlay.signal();
			else
				playing = false
		}
	}
}

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