Informatica 3: Novità

Quest'area è dedicata agli aggiornamenti relativi al corso di Informatica 3.

Voti 13/02/2009

Ho finito di correggere i compiti. Sono andati molto male. Voto massimo: 25. Percentuale di promossi: 35%. I voti sono in bacheca al DEI, la visione dei compiti sara' giovedi' 19/2 alle 11:15 in aula PT1. Faro' anche una spiegazione delle soluzioni.

G.

· 2009/02/16 10:59 · Giovanni Agosta

Esercitazione saltata...

Scusate per l'esercitazione saltata di stamattina. Recupereremo almeno un'ora lunedi' prossimo, per la seconda vedremo.

G.

· 2009/01/07 10:06 · Giovanni Agosta

Rinvio della lezione del 3/11

Salve a tutti,

visto che riesco a rientrare a Milano per il 12/11, la lezione del 3/11 e' rinviata al 12. Il calendario del corso e' stato aggiornato di conseguenza.

Saluti, G.

Lecture of 3/11 moved

Hi all,

I'll be able to be back in Milan by 12/11, so the lecture of 3/11 is rescheduled to November, 12th. The course calendar has been updated accordingly.

Regards, G.

Mutua Esclusione e Threading

In questa lezione abbiamo visto come l'uso dei thread porti ad aver bisogno di accedere ai dati in modo esclusivo e di sincronizzare i thread. Per questo, abbiamo introdotto eventi, lock e condition variables.

Inoltre, abbiamo introdotto i concetti di pila, coda e lista che approfondiremo nelle prossime lezioni.

Trovate in fondo alla pagina i due esempi di codice visti a lezione.

Threading and Mutual Exclusion

In this lecture we have shown how threading leads to a need for mutual exclusion in data accesses and thread synchronization. To this end, we have introduced events, locks and condition variables.

Producer and Consumers

#!/usr/bin/python
 
from threading import Thread, Lock, currentThread, enumerate, Event, Condition,
activeCount
from random import randint, choice
 
outputLock = Lock()
endThreads = Event()
 
class Buffer :
        def __init__(self):
                self.data = []
                self.lock= Condition()
 
        def enqueue(self,datum):
                self.lock.acquire()
                self.data.append(datum)
                self.lock.notify()
                self.lock.release()
 
        def dequeue(self):
                self.lock.acquire()
                if not len(self.data) : self.lock.wait()
                datum=self.data.pop()
                self.lock.release()
                return datum
 
        def notify_end(self):
                self.lock.acquire()
                self.lock.notifyAll()
                self.lock.release()
 
        def __len__(self):
                return len(self.data)
 
 
class BufferUser(Thread):
        def __init__(self, buffer):
                Thread.__init__(self)
                self.buffer=buffer
 
 
class Consumer(BufferUser):
        def run(self):
                while not endThreads.isSet() :
                        datum = self.buffer.dequeue()
                        outputLock.acquire()
                        print currentThread(), datum
                        outputLock.release()
                print 'Ending', currentThread()
 
class Producer(BufferUser):
        def run(self):
                while not endThreads.isSet() :
                        datum = currentThread(), randint(1,100)
                        self.buffer.enqueue(datum)
                print 'Ending', currentThread()
 
 
buffers = [ Buffer() for i in range(3) ]
 
class Manager(Thread):
        def run(self):
                raw_input("Stop the system...")
                outputLock.acquire()
                threads = enumerate()
                print 'Active Threads:', threads
                endThreads.set()
                for b in buffers :
                        b.notify_end()
                outputLock.release()
 
for t in range(3):
        Producer(choice(buffers)).start()
        Consumer(choice(buffers)).start()
Manager().start()
for t in enumerate() :
        if t!=currentThread():
                t.join()
print 'All threads killed!'
print 'Buffers entry remaining:',
for b in buffers :
        print len(b),
print ''

Barber's Problem

#!/usr/bin/python
 
from threading import Thread, Lock, currentThread, enumerate, Event, Condition, activeCount
from random import randint, choice
from time import sleep
 
endThreads = Event()
 
class BarberShop :
        def __init__(self,nchairs):
                self.nchairs = nchairs
                self.busychairs = []
                self.lock = Condition()
 
        def sit(self):
                self.lock.acquire()
                if len(self.busychairs)>=self.nchairs : self.lock.wait()
                event = Event()
                self.busychairs.append(event)
                self.lock.release()
                event.wait()
 
        def cut(self):
                self.lock.acquire()
                event = self.busychairs.pop()
                self.lock.notify()
                self.lock.release()
                sleep(randint(0,3))
                event.set()
 
        def notify_end(self):
                self.lock.acquire()
                self.lock.notifyAll()
                self.lock.release()
 
        def busy(self):
                return len(self.busychairs)>0
 
class BarberShopUser(Thread):
        def __init__(self, shop):
                Thread.__init__(self)
                self.shop=shop
 
class Barber(BarberShopUser):
        def run(self):
                while not endThreads.isSet() :
                        if self.shop.busy() :
                                print 'Barber', currentThread(), 'working!'
                                self.shop.cut()
                        else :
                                print 'Barber', currentThread(), 'sleeping!'
                                sleep(randint(0,3))
                print 'Ending', currentThread()
 
class Customer(BarberShopUser):
        def run(self):
                print 'Customer', currentThread(), 'entering barber shop!'
                self.shop.sit()
                print 'Customer', currentThread(), 'served!'
 
barbershop=BarberShop(4)
 
class Manager(Thread):
        def run(self):
                raw_input("Stop the system...")
                threads = enumerate()
                print 'Active Threads:', threads
                endThreads.set()
                barbershop.notify_end()
 
for t in range(2):
        Barber(barbershop).start()
Manager().start()
for t in range(15):
        sleep(randint(0,2))
        Customer(barbershop).start()
 
for t in enumerate() :
        if t!=currentThread():
                t.join()
print 'All threads killed!'

Concorrenza e Threading

In questa lezione abbiamo visto i principi fondamentali della concorrenza, e piu' i particolare il modello di programmazione a thread. Dal punto di vista teorico, abbiamo introdotto i concetti di memoria condivisa e passaggio di messaggi, nonche' alcuni modelli di concorrenza (spazio di tuple, modello ad attori, algebre dei processi).

In fondo alla pagina trovate gli esempi di codice visti a lezione.

Concurrency & Threading

In this lecture we have studied the fundamentals of concurrency, and specifically the thread programming model. On the theoretical side, we've introduced the concepts of shared memory and message passing, as well as some concurrency models (tuple space, actor model, process calculus).

Esempi di programmazione con i Thread/Code samples for Threading

Hello, World

#!/usr/bin/python
from threading import Thread
from time import sleep
from random import randint
 
def hello():
        sleep(randint(0,3))
        print 'Hello'
 
def world():
        sleep(randint(0,3))
        print 'World'
 
Thread(target=hello).start()
Thread(target=world).start()

A Producer/Consumer Example

#!/usr/bin/python
 
from threading import Thread, Event
from random import randint
 
d=0
e=[Event(),Event()]
e[1].set()
 
def hello(e):
        for i in range(5) :
                e[1].wait()
                global d
                d=d+1
                e[0].set()
                e[1].clear()
 
def world(e):
        for i in range(5) :
                e[0].wait()
                global d
                print d*2
                e[0].clear()
                e[1].set()
 
Thread(target=hello, args=(e,)).start()
Thread(target=world, args=(e,)).start()
· 2008/10/20 14:46 · Giovanni Agosta
teaching/info3/blog.txt · Last modified: 2007/09/13 16:50 by agosta
Recent changes RSS feed Creative Commons License Donate Driven by DokuWiki