Documentos de Académico
Documentos de Profesional
Documentos de Cultura
20 de abril de 2007
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Asignacin
V = {NewCell Valor_inicial}
Asignacin
Asignacin
Asignacin
Asignacin
Asignacin
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Arreglos
Un arreglo es un conjunto de celdas indexadas A = {NewArray +LowI +HighI InitX } {Array.put A I X} Ai := X {Array.get A I } Ai {Array.exchange A +I OldVal NewVal} Actualiza Ai
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Diccionarios
Un diccionario es una lista de parejas (llave, valor) D = {NewDictionary} {Dictionary.put D +LI X} En la llave LI coloca el valor X {Dictionary.get +D +LI X} Liga X al valor que tiene la llave LI
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Tuple Spaces
Un espacio de tuplas es un conjunto de tuplas etiquetadas write: {TS write T} Escribe la tupla T en el espacio TS read: {TS read(L T)} Espera a que el espacio TS tenga una tupla con etiqueta L, cuando suceda esto liga T a dicha tupla
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
meth readnonblock ( L ? Tuple ?B ) lock { s e l f EnsurePresent ( L ) } i f { @ t u p l e d i c t . L . s i z e } >0 then { @ t u p l e d i c t . L . d e l e t e Tuple } B= t r u e e l s e B= f a l s e end { s e l f Cleanup ( L ) } end end
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Locks - Candados
Los candados permiten proteger varias regiones crticas Podemos implementarlos, aunque Oz tiene una implementacin a bajo nivel (C++) ms eciente
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
f u n { SimpleLock } Token ={ NewCell u n i t } proc { Lock P} Old New i n { Exchange Token Old New} { Wait Old } { P} New= u n i t end in l o c k ( l o c k : Lock ) end
Lock (reentrante)
f u n { NewLock } Token ={ NewCell u n i t } CurThr ={ NewCell u n i t } proc { Lock P} i f { Thread . t h i s }==@CurThr then { P} e l s e Old New i n { Exchange Token Old New} { Wait Old } CurThr : = { Thread . t h i s } t r y { P} f i n a l l y CurThr : = u n i t New= u n i t end end end in l o c k ( l o c k : Lock ) end
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Monitores
Un monitor es un tipo abstracto de datos que protege varias regiones crticas (al igual que un lock), pero permite un mayor control al programador (son ms expresivos), ya que los hilos se pueden noticar eventos entre si Operaciones:
Lock Wait Notify NotifyAll
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Bounded Buffer
Un ejemplo familiar:
class Buffer attr buf f i r s t l a s t n i meth i n i t (N) b u f : = { NewArray 0 N1 n u l l } f i r s t :=0 l a s t :=0 n : =N i :=0 end meth p u t ( X ) . . . % w a i t u n t i l i <n % now add an element : @buf . @last : =X l a s t : = ( @last +1) mod @n i : = @i+1 end meth g e t ( X ) . . . % w a i t u n t i l i >0 % now remove an element : X=@buf . @ f i r s t f i r s t : = ( @ f i r s t +1) mod @n
meth g e t ( X ) {@ l o c k proc { $ } m. i f @i==0 then {@ w a i t } { s e l f g e t ( X ) } m. else X=@buf . @ f i r s t f i r s t : = ( @ f i r s t +1) mod @n i : = @i1 {@ n o t i f y A l l } m. end end } end end
Contenido
Introduccin TADs imperativos Arreglos Diccionarios Tuple Spaces Denicin Ejemplo con Tuple Spaces Implementacin Locks Denicin Implementacin Monitores Denicin Ejemplo Implementacin
Implementacin de Monitor
f u n { NewMonitor } Q={NewQueue } L ={NewGRLock } proc { LockM P} { L . g e t } t r y { P} f i n a l l y { L . r e l e a s e } end end proc { WaitM } X in {Q. i n s e r t X} { L . r e l e a s e } { Wait X} { L . g e t } end proc { N o t i f y M } U={Q. deleteNonBlock } i n case U o f [ X ] then X= u n i t e l s e s k i p end end proc { N o t i f y A l l M } L ={Q. d e l e t e A l l } i n f o r X i n L do X= u n i t end end in m o n i t o r ( l o c k : LockM w a i t : WaitM n o t i f y : N o t i f y M
Cola extendida
f u n { NewQueue } ... f u n { Size } l o c k L then @C. 1 end end fun { D e l e t e A l l } l o c k L then X q ( _ S E)=@C i n C: = q ( 0 X X ) E= n i l S end end f u n { DeleteNonBlock } l o c k L then i f { Size } >0 then [ { D e l e t e } ] e l s e n i l end end end in queue ( i n s e r t : I n s e r t d e l e t e : D e l e t e s i z e : Size d e l e t e A l l : D e l e t e A l l deleteNonBlock : DeleteNonBl
Lock(get-release)
f u n { NewGRLock } Token1 ={ NewCell u n i t } Token2 ={ NewCell u n i t } CurThr ={ NewCell u n i t } proc { GetLock } i f { Thread . t h i s } \ = @CurThr then Old New i n { Exchange Token1 Old New} { Wait Old } Token2 : =New CurThr : = { Thread . t h i s } end end proc { ReleaseLock } CurThr : = u n i t u n i t =@Token2 end in l o c k ( g e t : GetLock r e l e a s e : ReleaseLock ) end