50 lines
3.5 KiB
HTML
50 lines
3.5 KiB
HTML
<style>
|
|
corpo {
|
|
--indentazione: 2;
|
|
|
|
width: 90%;
|
|
}
|
|
</style>
|
|
<corpo>
|
|
<small>
|
|
<codeblock>
|
|
<name>readers and writers</name>: <kw>monitor</kw> <comment>// il monitor dei lettori e scrittori</comment><br />
|
|
<idt></idt><kw>begin</kw> <name>readercount</name> : integer;<br />
|
|
<idt></idt><idt></idt><name>busy</name>: Boolean; <comment>// detto prima del controllo se qualcuno scrive</comment><br />
|
|
<idt></idt><idt></idt><name>OKtoread</name>, <name>OKtowrite</name>: condition; <comment>// condizioni</comment><br />
|
|
<br />
|
|
<idt></idt><idt></idt><kw>procedure</kw> <name>startread</name>; <comment>// inizia la procedura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>begin</kw><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><kw>if</kw> <name>busy</name> || <name>OKtowrite</name>.queue <kw>then</kw> <name>OKtoread</name>.wait; <comment>// controlla se c'è una scrittura in attesa per bloccare il prossimo lettore</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><name>readercount</name> := <name>readercount</name> + 1;<br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><name>OKtoread</name>.signal;<br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><comment>// dopo che un lettore inizia iniziano quelli in coda dopo</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>end</kw> <name>startread</name>; <comment>// fine iniziolettura</comment><br />
|
|
<br />
|
|
<idt></idt><idt></idt><kw>procedure</kw> <name>endread</name>; <comment>// finelettura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>begin</kw><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><name>readercount</name> := <name>readercount</name> - 1 ; <comment>// leva i lettori in coda</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><kw>if</kw> <name>readercount</name> = 0 <kw>then</kw> <name>OKtowrite</name>.signal <comment>// appena finiscono si scrive</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>end</kw> <name>endread</name>; <comment>// fine finelettura</comment><br />
|
|
<br />
|
|
<idt></idt><idt></idt><kw>procedure</kw> <name>startwrite</name>; <comment>// inizio scrittura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>begin</kw><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><kw>if</kw> <name>readercount</name> != 0 || <name>busy</name> <kw>then</kw> <name>OKtowrite</name>.wait <comment>// se ci sono lettori aspetta la scrittura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><name>busy</name> := true<br />
|
|
<idt></idt><idt></idt><idt></idt><kw>end</kw> <name>startwrite</name>; <comment>// fine inizioscrittura</comment><br />
|
|
<br />
|
|
<idt></idt><idt></idt><kw>procedure</kw> <name>endwrite</name>; <comment>// procedura finescrittura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>begin</kw><br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><name>busy</name> := false;<br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><kw>if</kw> <name>OKtoread</name>.queue <kw>then</kw> <name>OKtoread</name>.signal<br />
|
|
<idt></idt><idt></idt><idt></idt><idt></idt><kw>else</kw> <name>OKtowrite</name>.signal <comment>// continua a fare entrare in scrittura</comment><br />
|
|
<idt></idt><idt></idt><idt></idt><kw>end</kw> <name>endwrite</name>; <comment>// fine finescrittura</comment><br />
|
|
<br />
|
|
<idt></idt><idt></idt><name>readercount</name> := 0;<br />
|
|
<idt></idt><idt></idt><name>busy</name> := false;<br />
|
|
<idt></idt><kw>end</kw> <name>readers and writers</name>; <comment>// finito tutto</comment><br />
|
|
</codeblock>
|
|
</small>
|
|
</corpo>
|
|
<autore>Lorenzo Viola</autore>
|