<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=ProvaTeoria_2009.01.30</id>
	<title>ProvaTeoria 2009.01.30 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://so.v2.cs.unibo.it/wiki/index.php?action=history&amp;feed=atom&amp;title=ProvaTeoria_2009.01.30"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaTeoria_2009.01.30&amp;action=history"/>
	<updated>2026-04-16T18:37:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.5</generator>
	<entry>
		<id>https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaTeoria_2009.01.30&amp;diff=681&amp;oldid=prev</id>
		<title>Stefano 92: Created page with &quot;=[http://www.cs.unibo.it/~renzo/so/compiti/2009-01-30.tot.pdf TESTO COMPITO]=  ==Esercizio 1== &lt;syntaxhighlight lang=&quot;C&quot;&gt; /* Esercizio 1: Scrivere un monitor sbb che implement...&quot;</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=ProvaTeoria_2009.01.30&amp;diff=681&amp;oldid=prev"/>
		<updated>2014-05-05T12:07:48Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=[http://www.cs.unibo.it/~renzo/so/compiti/2009-01-30.tot.pdf TESTO COMPITO]=  ==Esercizio 1== &amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt; /* Esercizio 1: Scrivere un monitor sbb che implement...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=[http://www.cs.unibo.it/~renzo/so/compiti/2009-01-30.tot.pdf TESTO COMPITO]=&lt;br /&gt;
&lt;br /&gt;
==Esercizio 1==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;C&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
Esercizio 1: Scrivere un monitor sbb che implementi un buffer limitato di caratteri.&lt;br /&gt;
Il monitor deve implmementare le seguenti procedure entry:&lt;br /&gt;
 int sbb.enqueue(char *s);&lt;br /&gt;
 int sbb.dequeue(char *buf, len l);&lt;br /&gt;
Il buffer contiene MAX caratteri. Le stringhe passate alla enqueue sono classiche stringhe del linguaggio C con il carattere &lt;br /&gt;
nullo come terminatore e devono avere lunghezza minore di MAX. Nel caso la stringa s abbia lunghezza maggiore o &lt;br /&gt;
uguale a MAX la stringa non viene caricata nel buffer. Il valore di ritorno e' la lunghezza della stringa inserita nel buffer, -1 &lt;br /&gt;
in caso di errore.&lt;br /&gt;
La dequeue deve restituire una stringa alla volta scrivendola nel buffer. Il parametro l contiene l'ampiezza del buffer (in &lt;br /&gt;
byte in modo simile alla system call &amp;quot;read&amp;quot;).&lt;br /&gt;
Se la stringa non puo' essere memorizzata interamente nel buffer perche' il buffer e' di lunghezza insufficiente viene &lt;br /&gt;
tornato un errore (-1), in modo che il processo chiamante provveda a chiamare nuovamente la funzione specificando un &lt;br /&gt;
buffer di ampiezza maggiore. (N.B. In caso di errore di buffer insufficiente la stringa non deve essere perduta!).&lt;br /&gt;
Il valore di ritorno e' la lunghezza effettiva della stringa presa dal buffer.&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
struct attesa_t&lt;br /&gt;
{&lt;br /&gt;
	int mySize;&lt;br /&gt;
	condition oktowrite;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
monitor sbb&lt;br /&gt;
{&lt;br /&gt;
	attesa_t * coda_w=queue()&lt;br /&gt;
	int size=0;&lt;br /&gt;
	char **buffer=NULL;&lt;br /&gt;
	procedure entry int enqueue(char*s)&lt;br /&gt;
	{&lt;br /&gt;
		int need=strlen(s);&lt;br /&gt;
		if (need&amp;gt;MAX)&lt;br /&gt;
			return -1; //ho specificato una stringa troppo lunga che non potrà mai essere inserita.&lt;br /&gt;
		else if (MAX-size &amp;lt; need) //non c'è spazio per scrivere: attendo che si liberi&lt;br /&gt;
			coda_w.enqueue(need).wait();&lt;br /&gt;
		insert(buffer,s); //scrivo nel buffer la mia stringa s&lt;br /&gt;
		return need; &lt;br /&gt;
	}&lt;br /&gt;
	procedure entry int dequeue(char*buf,len l)&lt;br /&gt;
	{&lt;br /&gt;
		if (isEmpty(buffer))&lt;br /&gt;
			oktoread.wait(); //buffer vuoto: aspetto che ci sia un elemento.&lt;br /&gt;
		if (strlen(head(buffer))&amp;gt;l)&lt;br /&gt;
			return -1; //spazio insufficiente: aspetto che si liberi. Nel frattempo per evitare la perdita della stringa non faccio signal&lt;br /&gt;
		else&lt;br /&gt;
			{&lt;br /&gt;
			buf=dequeue(buffer); //prelevo la mia stringa e la metto nel buffer&lt;br /&gt;
			if (!isEmpty(buffer)) &lt;br /&gt;
				oktoread.signal();	//buffer non vuoto: sveglio un lettore&lt;br /&gt;
			while(ris!=-1)&lt;br /&gt;
				(searchFor(MAX-size)).signal(); //se c'è spazio per far scrivere qualcuno, lo risveglio.&lt;br /&gt;
											//la searchFor() prende in input un intero n e cerca il primo scrittore&lt;br /&gt;
											//in coda che voglia scrivere meno di n byte. L'operazione continua finché&lt;br /&gt;
											//non ci sono più scrittori disponibili (return -1)&lt;br /&gt;
			return strlen(buf);&lt;br /&gt;
			}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
-stefano92&lt;/div&gt;</summary>
		<author><name>Stefano 92</name></author>
	</entry>
</feed>