<?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=Prove_scritte_2005</id>
	<title>Prove scritte 2005 - 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=Prove_scritte_2005"/>
	<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prove_scritte_2005&amp;action=history"/>
	<updated>2026-05-16T06:58:40Z</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=Prove_scritte_2005&amp;diff=2580&amp;oldid=prev</id>
		<title>Acsor: Created page with &quot;== Esame di Concorrenza del 13/01/2005 == Il testo degli esercizi è disponibile alla pagina http://www.cs.unibo.it/~renzo/so/compiti/2005-01-13.con.pdf.  === Esercizio 1 (da ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://so.v2.cs.unibo.it/wiki/index.php?title=Prove_scritte_2005&amp;diff=2580&amp;oldid=prev"/>
		<updated>2020-08-30T08:12:04Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Esame di Concorrenza del 13/01/2005 == Il testo degli esercizi è disponibile alla pagina http://www.cs.unibo.it/~renzo/so/compiti/2005-01-13.con.pdf.  === Esercizio 1 (da ...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Esame di Concorrenza del 13/01/2005 ==&lt;br /&gt;
Il testo degli esercizi è disponibile alla pagina http://www.cs.unibo.it/~renzo/so/compiti/2005-01-13.con.pdf.&lt;br /&gt;
&lt;br /&gt;
=== Esercizio 1 (da controllare) ===&lt;br /&gt;
Il codice di questo esercizio può essere eseguito scaricando il sorgente Python e l'archivio relativo agli [[Tool_per_semafori_e_monitor|strumenti di concorrenza per il linguaggio Python]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import threading&lt;br /&gt;
import random&lt;br /&gt;
import time&lt;br /&gt;
from pysm.semaphore import semaphore&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class mod_counter:&lt;br /&gt;
    def __init__(self, mod, start=0):&lt;br /&gt;
        self._mod = mod&lt;br /&gt;
        self._val = start&lt;br /&gt;
&lt;br /&gt;
    def inc(self):&lt;br /&gt;
        self._val = (self._val + 1) % self._mod&lt;br /&gt;
&lt;br /&gt;
    def get(self):&lt;br /&gt;
        return self._val&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class process(threading.Thread):&lt;br /&gt;
    def __init__(self, to_print, c: mod_counter, a, t, r):&lt;br /&gt;
        threading.Thread.__init__(self, name=&amp;quot;proc&amp;quot; + to_print)&lt;br /&gt;
&lt;br /&gt;
        if to_print not in &amp;quot;tar&amp;quot;:&lt;br /&gt;
            raise ValueError(&amp;quot;Unexpected printable character:&amp;quot;, to_print)&lt;br /&gt;
&lt;br /&gt;
        self._counter = c&lt;br /&gt;
        self._x = to_print&lt;br /&gt;
        self._semaphores = {'t': t, 'a': a, 'r': r}&lt;br /&gt;
        self._enter = True&lt;br /&gt;
&lt;br /&gt;
        if self._x == 't':&lt;br /&gt;
            self._semaphores[self._x].V()&lt;br /&gt;
&lt;br /&gt;
    def run(self):&lt;br /&gt;
        while True:&lt;br /&gt;
            self.sync()&lt;br /&gt;
            print(&amp;quot;[%s] %c&amp;quot; % (self.name, self._x))&lt;br /&gt;
&lt;br /&gt;
            # Print a trailing newline&lt;br /&gt;
            if (self._counter.get() == 7):&lt;br /&gt;
                print()&lt;br /&gt;
&lt;br /&gt;
            time.sleep(random.random() * 0.5)&lt;br /&gt;
&lt;br /&gt;
    def sync(self):&lt;br /&gt;
        if (self._enter):&lt;br /&gt;
            self._semaphores[self._x].P()&lt;br /&gt;
            self._enter = False&lt;br /&gt;
        else:&lt;br /&gt;
            self._counter.inc()&lt;br /&gt;
            curr = &amp;quot;taratata&amp;quot;[self._counter.get()]&lt;br /&gt;
&lt;br /&gt;
            self._semaphores[curr].V()&lt;br /&gt;
&lt;br /&gt;
            self._enter = True&lt;br /&gt;
            self.sync()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    counter = mod_counter(8)&lt;br /&gt;
    semaphores = (semaphore(0), semaphore(0), semaphore(0))&lt;br /&gt;
    procs = [process(c, counter, *semaphores) for c in &amp;quot;tar&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
    for p in procs:&lt;br /&gt;
        p.daemon = True&lt;br /&gt;
        p.start()&lt;br /&gt;
&lt;br /&gt;
    for p in procs:&lt;br /&gt;
        p.join()&lt;br /&gt;
&lt;br /&gt;
    return 0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    exit(main())&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Acsor</name></author>
	</entry>
</feed>