Documentos de Académico
Documentos de Profesional
Documentos de Cultura
OPERATIVOS
DEPARTAMENTO DE
CIENCIAS DE LA COMPUTACION
UNIVERSIDAD NACIONAL
DEL COMAHUE
CAPITULO 8
Sincronización
de
Procesos
TEMARIO
Motivaciones
El problema de Productores/Consumidores
El problema de la Sección Crítica
Hardware de sincronización
Semáforos
Problemas clásicos de sincronización
Construcciones de lenguaje
Restricciones:
Datos compartidos:
type item=....;
var buffer: array [0..n-1] of item;
in, out: 0..n-1;
counter: 0..n;
in, out, counter:=0;
repeat repeat
.... while counter=0 do no-op;
producir un item en nextp nextc:=buffer[out];
.... out:=out+1 mod n;
while counter=n do no-op; counter:=counter-1;
buffer[in]:=nextp; ....
in:=in+1 mod n; consumir item de nextc
counter:=counter+1; ....
until false; until false;
Las instrucciones
counter:=counter+1; productor
counter:=counter-1; consumidor
counter:=counter+1 counter:=counter-1
1. Exclusión mutua
2. Progreso
3. Espera definida
Exclusión mutua:
Progreso:
Espera definida:
Espera definida:
repeat
sección de entrada
sección crítica
sección de salida
sección restante
until false;
Variables compartidas:
int turno;
inicialmente turno=0
turno=i ⇒ Pi puede entrar en su SC
Proceso Pi :
repeat
while (turno !=i) do no-op;
sección crítica
turno=j;
sección restante
until false;
Proceso Pi :
repeat
flag[i]:=true;
while (flag[j]) do no-op;
sección crítica
flag[i]=false;
sección restante
until false;
Proceso Pi :
repeat
flag[i]:=true;
turno=j;
while (flag[j] and turno=j) do no-op;
sección crítica
flag[i]=false;
sección restante
until false;
El algoritmo de la panadería:
Inhibir interrupciones
TestAndSet
Swap
Proceso Pi :
repeat
inhibir interrupciones;
sección crítica
desinhibir interrupciones;
sección restante
until false;
Variables compartidas:
var lock:boolean; inicialmente en false
Proceso Pi :
repeat
while TestAndSet(lock) do no-op;
sección crítica
lock:=false;
sección restante
until false;
Proceso Pi :
var key:boolean;
repeat
key:=true;
repeat
swap(lock,key);
until key=false;
sección crítica
lock:=false;
sección restante
until false;
wait (S):
while S≤0 do no-op;
S:=S-1;
signal (S):
S:=S+1;
Proceso Pi :
repeat
wait(mutex);
sección crítica
signal(mutex);
sección restante
until false;
Wait(S) Signal(S)
S.value:=S.value-1; S.value:=S.value+1;
if (S.value < 0) if (S.value <= 0)
then begin then begin
agregar proceso P remover proceso
a S.L; P de S.L;
block(); wakeup(P);
end; end;
Pi Pj
M M
A wait (flag)
signal (flag) B
Deadlock
Starvation
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
M M
signal(S); signal(Q);
signal(Q); signal(S);
Productores/Consumidores
Lectores/Escritores
Filósofos cenando
type item=.....
var buffer=.....
full, empty, mutex: semaphore;
nextp,nextc: item;
repeat repeat
… wait(full);
producir un item en wait(mutex);
nextp …
… mover un item de buffer a nextc
wait(empty); …
wait(mutex); signal(mutex);
… signal(empty);
agregar nextp a buffer …
… consumir item de nextc
signal(mutex); …
signal(full); until false;
until false;
Restricciones:
Inicialmente
wait(wrt); wait(mutex);
…. readcount:=readcount+1;
escribir if readcount = 1 then wait(wrt);
…. signal(mutex);
signal(wrt); …
leer
…
wait(mutex);
readcount :=readcount-1;
if readcount =0 then signal(wrt);
signal(mutex):
La cantidad de arroz es
ilimitada.
Regiones críticas
Regiones críticas condicionales
Monitores
Procedimientos
Variables de condición
begin
código de inicialización
end
monitor dp
{
enum {thinking, hungry, eating} state[5];
condition self[5];
init() {
for (int i = 0; i < 5; i++)
state[i] = thinking;
}
}
void pickup(int i) {
state[i] = hungry;
test[i];
if (state[i] != eating)
self[i].wait();
}
void putdown(int i) {
state[i] = thinking;
// testea a derecha e izquierda
test((i+4) % 5);
test((i+1) % 5);
}
void test(int i) {
if ( (state[(I + 4) % 5] != eating) &&
(state[i] == hungry) &&
(state[(i + 1) % 5] != eating)) {
state[i] = eating;
self[i].signal();
}
}
Variables
wait(mutex);
…
Proci
…
if (next-count > 0)
signal(next)
else
signal(mutex);
x.wait x.signal