<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=FedericoP</id>
	<title>Sistemi Operativi - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=FedericoP"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php/Special:Contributions/FedericoP"/>
	<updated>2026-05-05T06:15:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2014.07.16&amp;diff=1721</id>
		<title>Prova teorica 2014.07.16</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2014.07.16&amp;diff=1721"/>
		<updated>2017-01-15T09:37:40Z</updated>

		<summary type="html">&lt;p&gt;FedericoP: /* Esercizio c.2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testo: [http://www.cs.unibo.it/~renzo/so/compiti/2014.07.16.tot.pdf]&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Esercizio c.1: scrivere il monitor mMbb che realizzi un meccanismo di un minmax bounded buffer.&lt;br /&gt;
 * Dopo le prime MIN operazioni di scrittura, un minmax bounded buffer deve sempre avere almeno MIN elementi e mai piu' di MAX&lt;br /&gt;
 * elementi (e' quindi limitato sia nell'ampiezza massima, sia nell'ampiezza minima).&lt;br /&gt;
 * Le funzioni (procedure entry) read e write del minmax bounded buffer hanno gli stessi argomenti e valori di ritorno di quelle del&lt;br /&gt;
 * producer/consumer o del bounded buffer ordinario.&lt;br /&gt;
 */&lt;br /&gt;
#define MIN&lt;br /&gt;
#define MAX&lt;br /&gt;
#define SIZE // con SIZE &amp;gt;= MAX&lt;br /&gt;
&lt;br /&gt;
monitor mMbb{&lt;br /&gt;
	&lt;br /&gt;
	generic_type bb[];&lt;br /&gt;
	int front, rear, count;&lt;br /&gt;
	condition R, W;&lt;br /&gt;
	&lt;br /&gt;
	void mMbb(void){&lt;br /&gt;
		bb = new generic_type[SIZE];&lt;br /&gt;
		front = rear = count = 0;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry generic_type read(){&lt;br /&gt;
		while(count &amp;lt; MIN)	// Controllo che il buffer abbia MIN elementi per poter leggere&lt;br /&gt;
			R.wait();&lt;br /&gt;
		&lt;br /&gt;
		generic_type ret = bb[rear];&lt;br /&gt;
		count--;&lt;br /&gt;
		rear = (rear + 1) % SIZE;&lt;br /&gt;
		W.signal();&lt;br /&gt;
		return ret;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry write(generic_type c){&lt;br /&gt;
		while(count &amp;gt;= MAX)	// Controllo che il buffer non abbia MAX elementi per poter scrivere&lt;br /&gt;
			W.wait();&lt;br /&gt;
		&lt;br /&gt;
		bb[front] = c;&lt;br /&gt;
		count++;&lt;br /&gt;
		front = (front + 1) % SIZE;&lt;br /&gt;
		R.signal();	&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[User:S.G|S.G]] ([[User talk:S.G|talk]]) 17:21, 2 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
Non &amp;amp;egrave; fifo. Perch&amp;amp;eacute;? [[User:Renzo|Renzo]] ([[User talk:Renzo|talk]]) 07:43, 3 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
Nel caso in cui MIN vale 3 e arrivino tre processi lettori nel seguente ordine, L1, L2, L3, avremmo che la coda di R è la seguente:&lt;br /&gt;
&lt;br /&gt;
R: L1, L2, L3&lt;br /&gt;
&lt;br /&gt;
Ora arriva un processo scrittore S1, viene eseguito liberando un lettore (R.signal) cioè L1. A questo punto L1 riprende la sua esecuzione, riesegue il ciclo e rientra nello stato di attesa. Quindi ora la coda di R è la seguente:&lt;br /&gt;
&lt;br /&gt;
R: L2, L3, L1&lt;br /&gt;
&lt;br /&gt;
Si potrebbe risolvere il problema modificando le funzioni write e read nel seguente modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
procedure_entry generic_type read(){&lt;br /&gt;
		if(count &amp;lt; MIN)	// Controllo che il buffer abbia MIN elementi per poter leggere&lt;br /&gt;
			R.wait();&lt;br /&gt;
		&lt;br /&gt;
		generic_type ret = bb[rear];&lt;br /&gt;
		count--;&lt;br /&gt;
		rear = (rear + 1) % SIZE;&lt;br /&gt;
		if(count &amp;lt; MAX)&lt;br /&gt;
			W.signal();&lt;br /&gt;
		return ret;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry write(generic_type c){&lt;br /&gt;
		if(count &amp;gt;= MAX)	// Controllo che il buffer non abbia MAX elementi per poter scrivere&lt;br /&gt;
			W.wait();&lt;br /&gt;
		&lt;br /&gt;
		bb[front] = c;&lt;br /&gt;
		count++;&lt;br /&gt;
		front = (front + 1) % SIZE;&lt;br /&gt;
		if(count &amp;gt; MIN)&lt;br /&gt;
			R.signal();	&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[User:S.G|S.G]] ([[User talk:S.G|talk]]) 10:37, 3 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.2 ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Esercizio c.2: Facendo uso di semafori scrivere la funzione rendezvous che consenta ai processi di sincronizzarsi secondo le&lt;br /&gt;
 * seguenti specifiche:&lt;br /&gt;
 * – Ogni processo indica come parametro della funzione rendezvous con quanti altri processi vuole sincronizzarsi.&lt;br /&gt;
 * – M processi che chiamano la rendezvous con parametro N rimangono bloccati se M&amp;lt;N.&lt;br /&gt;
 * – Quando l'N-mo processo richiama la rendezvous specificando N come parametro, lui e gli N-1 sospesi devono proseguire&lt;br /&gt;
 * nelle propria esecuzione&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
struct S {&lt;br /&gt;
	Semaphore sem;&lt;br /&gt;
	int N; // parametro della funzione rendezvous con quanti altri processi vuole sincronizzarsi&lt;br /&gt;
	int I; // Contatore dei processi che richiamano rendezvous con paramentro N&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
List&amp;lt;S&amp;gt; listS = new List[]; // Lista di possibili semafori&lt;br /&gt;
&lt;br /&gt;
Semaphore mutex = new Semaphore(1);		// Variabile che consente la mutua esclusione&lt;br /&gt;
&lt;br /&gt;
rendevouz(int n){&lt;br /&gt;
	mutex.P();	// Ottengo la mutua esclusione&lt;br /&gt;
	&lt;br /&gt;
	find = FALSE;&lt;br /&gt;
	for(s in listS){	// Cerco il semaforo nella lista&lt;br /&gt;
		if(s.N == n){	&lt;br /&gt;
			find = TRUE;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	if(find == FALSE){	// se non è presente nella lista lo creo&lt;br /&gt;
		struct S s;&lt;br /&gt;
		s.sem = new semaphore(0);&lt;br /&gt;
		s.N = n;&lt;br /&gt;
		s.I = 0;&lt;br /&gt;
		listS.insert(s);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	s.I++;&lt;br /&gt;
	if(s.I == s.N){		// Quando l'N-mo processo richiama la rendezvous specificando N come parametro&lt;br /&gt;
		while(s.I--)	// lui e gli N-1 sospesi devono proseguire nelle propria esecuzione&lt;br /&gt;
			s.sem.V();&lt;br /&gt;
	}&lt;br /&gt;
	else if(s.I &amp;lt; s.N){	// M processi che chiamano la rendezvous con parametro N rimangono bloccati se M&amp;lt;N&lt;br /&gt;
		s.sem.P();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	mutex.V();	// Rilascio la mutua esclusione&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Ho qualche dubbio su questa soluzione, riguardo la linea presente l'istruzione s.sem.P(), il processo viene immediatamente bloccato? Se si dovrei rilasciare la mutua esclusione prima quindi? [[User:S.G|S.G]] ([[User talk:S.G|talk]]) 16:43, 3 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
Si, la mutua esclusione va, in linea di principio, sempre rilasaciata prima di un' operazione P() poichè il processo che la chiama potrebbe bloccarsi tenendo con se la mutua esclusione e causando quindi deadlock.--[[User:FedericoP|FedericoP]] ([[User talk:FedericoP|talk]]) 10:37, 15 January 2017 (CET)&lt;/div&gt;</summary>
		<author><name>FedericoP</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2014.07.16&amp;diff=1720</id>
		<title>Prova teorica 2014.07.16</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2014.07.16&amp;diff=1720"/>
		<updated>2017-01-15T09:33:50Z</updated>

		<summary type="html">&lt;p&gt;FedericoP: /* Esercizio c.2 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testo: [http://www.cs.unibo.it/~renzo/so/compiti/2014.07.16.tot.pdf]&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.1 ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Esercizio c.1: scrivere il monitor mMbb che realizzi un meccanismo di un minmax bounded buffer.&lt;br /&gt;
 * Dopo le prime MIN operazioni di scrittura, un minmax bounded buffer deve sempre avere almeno MIN elementi e mai piu' di MAX&lt;br /&gt;
 * elementi (e' quindi limitato sia nell'ampiezza massima, sia nell'ampiezza minima).&lt;br /&gt;
 * Le funzioni (procedure entry) read e write del minmax bounded buffer hanno gli stessi argomenti e valori di ritorno di quelle del&lt;br /&gt;
 * producer/consumer o del bounded buffer ordinario.&lt;br /&gt;
 */&lt;br /&gt;
#define MIN&lt;br /&gt;
#define MAX&lt;br /&gt;
#define SIZE // con SIZE &amp;gt;= MAX&lt;br /&gt;
&lt;br /&gt;
monitor mMbb{&lt;br /&gt;
	&lt;br /&gt;
	generic_type bb[];&lt;br /&gt;
	int front, rear, count;&lt;br /&gt;
	condition R, W;&lt;br /&gt;
	&lt;br /&gt;
	void mMbb(void){&lt;br /&gt;
		bb = new generic_type[SIZE];&lt;br /&gt;
		front = rear = count = 0;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry generic_type read(){&lt;br /&gt;
		while(count &amp;lt; MIN)	// Controllo che il buffer abbia MIN elementi per poter leggere&lt;br /&gt;
			R.wait();&lt;br /&gt;
		&lt;br /&gt;
		generic_type ret = bb[rear];&lt;br /&gt;
		count--;&lt;br /&gt;
		rear = (rear + 1) % SIZE;&lt;br /&gt;
		W.signal();&lt;br /&gt;
		return ret;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry write(generic_type c){&lt;br /&gt;
		while(count &amp;gt;= MAX)	// Controllo che il buffer non abbia MAX elementi per poter scrivere&lt;br /&gt;
			W.wait();&lt;br /&gt;
		&lt;br /&gt;
		bb[front] = c;&lt;br /&gt;
		count++;&lt;br /&gt;
		front = (front + 1) % SIZE;&lt;br /&gt;
		R.signal();	&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[User:S.G|S.G]] ([[User talk:S.G|talk]]) 17:21, 2 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
Non &amp;amp;egrave; fifo. Perch&amp;amp;eacute;? [[User:Renzo|Renzo]] ([[User talk:Renzo|talk]]) 07:43, 3 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
Nel caso in cui MIN vale 3 e arrivino tre processi lettori nel seguente ordine, L1, L2, L3, avremmo che la coda di R è la seguente:&lt;br /&gt;
&lt;br /&gt;
R: L1, L2, L3&lt;br /&gt;
&lt;br /&gt;
Ora arriva un processo scrittore S1, viene eseguito liberando un lettore (R.signal) cioè L1. A questo punto L1 riprende la sua esecuzione, riesegue il ciclo e rientra nello stato di attesa. Quindi ora la coda di R è la seguente:&lt;br /&gt;
&lt;br /&gt;
R: L2, L3, L1&lt;br /&gt;
&lt;br /&gt;
Si potrebbe risolvere il problema modificando le funzioni write e read nel seguente modo:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
procedure_entry generic_type read(){&lt;br /&gt;
		if(count &amp;lt; MIN)	// Controllo che il buffer abbia MIN elementi per poter leggere&lt;br /&gt;
			R.wait();&lt;br /&gt;
		&lt;br /&gt;
		generic_type ret = bb[rear];&lt;br /&gt;
		count--;&lt;br /&gt;
		rear = (rear + 1) % SIZE;&lt;br /&gt;
		if(count &amp;lt; MAX)&lt;br /&gt;
			W.signal();&lt;br /&gt;
		return ret;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure_entry write(generic_type c){&lt;br /&gt;
		if(count &amp;gt;= MAX)	// Controllo che il buffer non abbia MAX elementi per poter scrivere&lt;br /&gt;
			W.wait();&lt;br /&gt;
		&lt;br /&gt;
		bb[front] = c;&lt;br /&gt;
		count++;&lt;br /&gt;
		front = (front + 1) % SIZE;&lt;br /&gt;
		if(count &amp;gt; MIN)&lt;br /&gt;
			R.signal();	&lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[User:S.G|S.G]] ([[User talk:S.G|talk]]) 10:37, 3 December 2016 (CET)&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.2 ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Esercizio c.2: Facendo uso di semafori scrivere la funzione rendezvous che consenta ai processi di sincronizzarsi secondo le&lt;br /&gt;
 * seguenti specifiche:&lt;br /&gt;
 * – Ogni processo indica come parametro della funzione rendezvous con quanti altri processi vuole sincronizzarsi.&lt;br /&gt;
 * – M processi che chiamano la rendezvous con parametro N rimangono bloccati se M&amp;lt;N.&lt;br /&gt;
 * – Quando l'N-mo processo richiama la rendezvous specificando N come parametro, lui e gli N-1 sospesi devono proseguire&lt;br /&gt;
 * nelle propria esecuzione&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
struct S {&lt;br /&gt;
	Semaphore sem;&lt;br /&gt;
	int N; // parametro della funzione rendezvous con quanti altri processi vuole sincronizzarsi&lt;br /&gt;
	int I; // Contatore dei processi che richiamano rendezvous con paramentro N&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
List&amp;lt;S&amp;gt; listS = new List[]; // Lista di possibili semafori&lt;br /&gt;
&lt;br /&gt;
Semaphore mutex = new Semaphore(1);		// Variabile che consente la mutua esclusione&lt;br /&gt;
&lt;br /&gt;
rendevouz(int n){&lt;br /&gt;
	mutex.P();	// Ottengo la mutua esclusione&lt;br /&gt;
	&lt;br /&gt;
	find = FALSE;&lt;br /&gt;
	for(s in listS){	// Cerco il semaforo nella lista&lt;br /&gt;
		if(s.N == n){	&lt;br /&gt;
			find = TRUE;&lt;br /&gt;
			break;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	if(find == FALSE){	// se non è presente nella lista lo creo&lt;br /&gt;
		struct S s;&lt;br /&gt;
		s.sem = new semaphore(0);&lt;br /&gt;
		s.N = n;&lt;br /&gt;
		s.I = 0;&lt;br /&gt;
		listS.insert(s);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	s.I++;&lt;br /&gt;
	if(s.I == s.N){		// Quando l'N-mo processo richiama la rendezvous specificando N come parametro&lt;br /&gt;
		while(s.I--)	// lui e gli N-1 sospesi devono proseguire nelle propria esecuzione&lt;br /&gt;
			s.sem.V();&lt;br /&gt;
	}&lt;br /&gt;
	else if(s.I &amp;lt; s.N){	// M processi che chiamano la rendezvous con parametro N rimangono bloccati se M&amp;lt;N&lt;br /&gt;
		s.sem.P();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	mutex.V();	// Rilascio la mutua esclusione&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Ho qualche dubbio su questa soluzione, riguardo la linea presente l'istruzione s.sem.P(), il processo viene immediatamente bloccato? Se si dovrei rilasciare la mutua esclusione prima quindi? [[User:S.G|S.G]] ([[User talk:S.G|talk]]) 16:43, 3 December 2016 (CET)&lt;br /&gt;
Si, la mutua esclusione va, in linea di principio, sempre rilasaciata prima di un' operazione P() poichè il processo che la chiama potrebbe bloccarsi tenendo con se la mutua esclusione e causando quindi deadlock.&lt;/div&gt;</summary>
		<author><name>FedericoP</name></author>
	</entry>
</feed>