<?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=MarcoNegrini</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=MarcoNegrini"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php/Special:Contributions/MarcoNegrini"/>
	<updated>2026-05-04T18:03:46Z</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_2015.02.14&amp;diff=1908</id>
		<title>Prova teorica 2015.02.14</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2015.02.14&amp;diff=1908"/>
		<updated>2017-06-06T10:08:40Z</updated>

		<summary type="html">&lt;p&gt;MarcoNegrini: /* Soluzione di MarcoNegrini */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testo: [http://www.cs.unibo.it/~renzo/so/compiti/2015.02.14.tot.pdf]&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.1 ==&lt;br /&gt;
===Soluzione di Silas===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#define MAX &lt;br /&gt;
&lt;br /&gt;
monitor altcolbb{&lt;br /&gt;
	queue buff;&lt;br /&gt;
	color_t last_color; //0: red, 1:blue, -1:&amp;quot;superstate&amp;quot;, means that both red and blue are accepted&lt;br /&gt;
	int wait_red, wait_blue;&lt;br /&gt;
	condition ok2read, redok, blueok;&lt;br /&gt;
&lt;br /&gt;
	void altcolbb(void){&lt;br /&gt;
		buff = new queue();&lt;br /&gt;
		last_color = -1;&lt;br /&gt;
		wait_red = wait_blue = 0;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry void write(color_t color, generic_type val){&lt;br /&gt;
		if(last_color == color || buff.length() == MAX){ //we can't enqueue if the colors are the same or if the buffer is full&lt;br /&gt;
			if(color == 0){ //enqueue to &amp;quot;reds&amp;quot;&lt;br /&gt;
				wait_red++;&lt;br /&gt;
				redok.wait();&lt;br /&gt;
				wait_red--;&lt;br /&gt;
			}else{ //enqueue to &amp;quot;blues&amp;quot;&lt;br /&gt;
				wait_blue++;&lt;br /&gt;
				blueok.wait();&lt;br /&gt;
				wait_blue--;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		buff.enqueue(val); //append val and update last_color&lt;br /&gt;
		last_color = color;&lt;br /&gt;
		ok2read.signal();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry generic_type read(void){&lt;br /&gt;
		if(buff.length() == 0)&lt;br /&gt;
			ok2read.wait();&lt;br /&gt;
		generic_type ret = buff.dequeue();&lt;br /&gt;
		if(buff.length() == 0)&lt;br /&gt;
			last_color = -1; //if buff is empty both colors can now be added&lt;br /&gt;
		switch(last_color){&lt;br /&gt;
			case -1: {&lt;br /&gt;
					if(wait_red&amp;gt;wait_blue) //if there are more &amp;quot;reds&amp;quot; waiting to write we signal them&lt;br /&gt;
						redok.signal();&lt;br /&gt;
					else&lt;br /&gt;
						blueok.signal(); //otherwise we signal &amp;quot;blues&amp;quot;&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
			case 0: {&lt;br /&gt;
					blueok.signal();&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
			case 1: {&lt;br /&gt;
					redok.signal();&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Soluzione di MarcoNegrini===&lt;br /&gt;
I corrected Silas's version&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define MAX &lt;br /&gt;
 &lt;br /&gt;
monitor altcolbb{&lt;br /&gt;
	queue buff;&lt;br /&gt;
	color_t last_color; //0: red, 1:blue, -1:&amp;quot;superstate&amp;quot;, means that both red and blue are accepted&lt;br /&gt;
	condition ok2read, ok2write, redok, blueok;&lt;br /&gt;
&lt;br /&gt;
	void altcolbb(void){&lt;br /&gt;
		buff = new queue();&lt;br /&gt;
		last_color = -1;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	procedure entry void write(color_t color, generic_type val){&lt;br /&gt;
		if(buff.length() == MAX){ //here waits if buff is full&lt;br /&gt;
			ok2write.wait();&lt;br /&gt;
		if(last_color==-1){ //if buff was empty it can just enqueue and exit&lt;br /&gt;
			buff.enqueue(val);&lt;br /&gt;
			last_color = color;&lt;br /&gt;
		}&lt;br /&gt;
		else{	&lt;br /&gt;
			if(color == 0){ //red section&lt;br /&gt;
				if (last_color == 0) //if last was red it needs to wait&lt;br /&gt;
					redok.wait();&lt;br /&gt;
				buff.enqueue(val); &lt;br /&gt;
				last_color = color; // it must set last color before signaling other writers&lt;br /&gt;
				blueok.signal(); //blue can now enqueue his value&lt;br /&gt;
			}else{ //blue section, same as above&lt;br /&gt;
				if (last_color == 1)&lt;br /&gt;
					blueok.wait();&lt;br /&gt;
				buff.enqueue(val);&lt;br /&gt;
				last_color = color;&lt;br /&gt;
				redok.signal();&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		ok2read.signal(); // reader can now read&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	procedure entry generic_type read(void){&lt;br /&gt;
		//here waits if needed, then dequeue&lt;br /&gt;
		if(buff.length() == 0) &lt;br /&gt;
			ok2read.wait();&lt;br /&gt;
		generic_type ret = buff.dequeue();&lt;br /&gt;
&lt;br /&gt;
		//here signal the one that has been waiting for more time&lt;br /&gt;
		// it MUST be of the last_color color&lt;br /&gt;
		if(buff.length() == 0){&lt;br /&gt;
			color_t tmp=last_color;&lt;br /&gt;
			last_color = -1; &lt;br /&gt;
			// last color MUST be set to -1 here because &lt;br /&gt;
			// if there were writers waiting on ok2write.wait() (full buffer case)&lt;br /&gt;
			// they woundn't be signaled here, this happens if MAX=1&lt;br /&gt;
			&lt;br /&gt;
			if (tmp == 1) //&lt;br /&gt;
				blueok.signal();&lt;br /&gt;
			else&lt;br /&gt;
				redok.signal();&lt;br /&gt;
		}&lt;br /&gt;
		ok2write.signal();&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.2 ==&lt;br /&gt;
===Soluzione di Silas===&lt;br /&gt;
I semafori garantiscono che A e B vengano eseguiti in maniera sequenziale e che accedano in maniera mutualmente esclusiva ad n, quindi l'unica variazione possibile è il thread di inizio per la sequenza.&lt;br /&gt;
Nel caso sia A ad accedere per primo alla CS si ha: n = ((1*2)+2)*3 = 12&lt;br /&gt;
Nel caso sia invece B ad accedere per primo alla C si ha: n = (((0*2)+1)*3)+2 = 5&lt;br /&gt;
Quindi n = 12 e n = 5 sono i due possibili valori.&lt;/div&gt;</summary>
		<author><name>MarcoNegrini</name></author>
	</entry>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2015.02.14&amp;diff=1907</id>
		<title>Prova teorica 2015.02.14</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prova_teorica_2015.02.14&amp;diff=1907"/>
		<updated>2017-06-06T10:07:22Z</updated>

		<summary type="html">&lt;p&gt;MarcoNegrini: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Testo: [http://www.cs.unibo.it/~renzo/so/compiti/2015.02.14.tot.pdf]&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.1 ==&lt;br /&gt;
===Soluzione di Silas===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
#define MAX &lt;br /&gt;
&lt;br /&gt;
monitor altcolbb{&lt;br /&gt;
	queue buff;&lt;br /&gt;
	color_t last_color; //0: red, 1:blue, -1:&amp;quot;superstate&amp;quot;, means that both red and blue are accepted&lt;br /&gt;
	int wait_red, wait_blue;&lt;br /&gt;
	condition ok2read, redok, blueok;&lt;br /&gt;
&lt;br /&gt;
	void altcolbb(void){&lt;br /&gt;
		buff = new queue();&lt;br /&gt;
		last_color = -1;&lt;br /&gt;
		wait_red = wait_blue = 0;&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry void write(color_t color, generic_type val){&lt;br /&gt;
		if(last_color == color || buff.length() == MAX){ //we can't enqueue if the colors are the same or if the buffer is full&lt;br /&gt;
			if(color == 0){ //enqueue to &amp;quot;reds&amp;quot;&lt;br /&gt;
				wait_red++;&lt;br /&gt;
				redok.wait();&lt;br /&gt;
				wait_red--;&lt;br /&gt;
			}else{ //enqueue to &amp;quot;blues&amp;quot;&lt;br /&gt;
				wait_blue++;&lt;br /&gt;
				blueok.wait();&lt;br /&gt;
				wait_blue--;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		buff.enqueue(val); //append val and update last_color&lt;br /&gt;
		last_color = color;&lt;br /&gt;
		ok2read.signal();&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	procedure entry generic_type read(void){&lt;br /&gt;
		if(buff.length() == 0)&lt;br /&gt;
			ok2read.wait();&lt;br /&gt;
		generic_type ret = buff.dequeue();&lt;br /&gt;
		if(buff.length() == 0)&lt;br /&gt;
			last_color = -1; //if buff is empty both colors can now be added&lt;br /&gt;
		switch(last_color){&lt;br /&gt;
			case -1: {&lt;br /&gt;
					if(wait_red&amp;gt;wait_blue) //if there are more &amp;quot;reds&amp;quot; waiting to write we signal them&lt;br /&gt;
						redok.signal();&lt;br /&gt;
					else&lt;br /&gt;
						blueok.signal(); //otherwise we signal &amp;quot;blues&amp;quot;&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
			case 0: {&lt;br /&gt;
					blueok.signal();&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
			case 1: {&lt;br /&gt;
					redok.signal();&lt;br /&gt;
					break;&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Soluzione di MarcoNegrini===&lt;br /&gt;
I corrected Silas's version&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#define MAX &lt;br /&gt;
 &lt;br /&gt;
monitor altcolbb{&lt;br /&gt;
	queue buff;&lt;br /&gt;
	color_t last_color; //0: red, 1:blue, -1:&amp;quot;superstate&amp;quot;, means that both red and blue are accepted&lt;br /&gt;
	condition ok2read, ok2write, redok, blueok;&lt;br /&gt;
&lt;br /&gt;
	void altcolbb(void){&lt;br /&gt;
		buff = new queue();&lt;br /&gt;
		last_color = -1;&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	procedure entry void write(color_t color, generic_type val){&lt;br /&gt;
		if(buff.length() == MAX){ //here waits if buff is full&lt;br /&gt;
			ok2write.wait();&lt;br /&gt;
		if(last_color==-1){ //if buff was empty it can just enqueue and exit&lt;br /&gt;
			buff.enqueue(val);&lt;br /&gt;
			last_color = color;&lt;br /&gt;
		}&lt;br /&gt;
		else{	&lt;br /&gt;
			if(color == 0){ //red section&lt;br /&gt;
				if (last_color == 0) //if last was red it needs to wait&lt;br /&gt;
					redok.wait();&lt;br /&gt;
				buff.enqueue(val); &lt;br /&gt;
				last_color = color; // it must set last color before signaling other writers&lt;br /&gt;
				blueok.signal(); //blue can now enqueue his value&lt;br /&gt;
			}else{ //blue section, same as above&lt;br /&gt;
				if (last_color == 1)&lt;br /&gt;
					blueok.wait();&lt;br /&gt;
				buff.enqueue(val);&lt;br /&gt;
				last_color = color;&lt;br /&gt;
				redok.signal();&lt;br /&gt;
			}&lt;br /&gt;
		}&lt;br /&gt;
		ok2read.signal(); // reader can now read&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
	procedure entry generic_type read(void){&lt;br /&gt;
		//here waits if needed, then dequeue&lt;br /&gt;
		if(buff.length() == 0) &lt;br /&gt;
			ok2read.wait();&lt;br /&gt;
		generic_type ret = buff.dequeue();&lt;br /&gt;
&lt;br /&gt;
		//here signal the one that has been waiting for more time&lt;br /&gt;
		// it MUST be of the last_color color&lt;br /&gt;
		if(buff.length() == 0){&lt;br /&gt;
			color_t tmp=last_color;&lt;br /&gt;
			last_color = -1; &lt;br /&gt;
			// last color MUST be set to -1 here because &lt;br /&gt;
			// if there was writers waiting on ok2write.wait() (full buffer case)&lt;br /&gt;
			// they woundn't be signaled here, this happens if MAX=1&lt;br /&gt;
			&lt;br /&gt;
			if (tmp == 1) //&lt;br /&gt;
				blueok.signal();&lt;br /&gt;
			else&lt;br /&gt;
				redok.signal();&lt;br /&gt;
		}&lt;br /&gt;
		ok2write.signal();&lt;br /&gt;
	}&lt;br /&gt;
 &lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Esercizio c.2 ==&lt;br /&gt;
===Soluzione di Silas===&lt;br /&gt;
I semafori garantiscono che A e B vengano eseguiti in maniera sequenziale e che accedano in maniera mutualmente esclusiva ad n, quindi l'unica variazione possibile è il thread di inizio per la sequenza.&lt;br /&gt;
Nel caso sia A ad accedere per primo alla CS si ha: n = ((1*2)+2)*3 = 12&lt;br /&gt;
Nel caso sia invece B ad accedere per primo alla C si ha: n = (((0*2)+1)*3)+2 = 5&lt;br /&gt;
Quindi n = 12 e n = 5 sono i due possibili valori.&lt;/div&gt;</summary>
		<author><name>MarcoNegrini</name></author>
	</entry>
</feed>