Sei sulla pagina 1di 12

Implementacin de la

exclusin mutua

in_mutex

Seccion_critica

out_mutex

Requisito:

in_mutexyout_mutexsonprimitivosoatmico(nose
descomponenmponenloscomandos)

Semforos
Def.
Sonvariablesprotegidas,slopuedenseraccedidas
conlasfuncionessem_wait(),sem_post()y
sem_init()

sem_wait(s)

Noescorrectoif(s>0)

Funciones que manejan


los semforos

sem_wait(s)

sis>0entoncess=s1
sinowait(s)
finsi

sem_init(s,valor)

S=0luzroja

S=1luzverde

sem_post(s)
Si hay un proceso
esperado a S
entonces signal(s)
Sino s=s+1
Finsi

Modelo de estados

Ejecucin

listo

Wait(s)

Bloqueado
S
Signal(s)

Semforos POSIX
sem_init(sem_t *sem, int shared, int val);

Inicializaunsemforosinnombre
int sem_destroy(sem_t *sem);

Destruyeunsemforosinnombre
sem_t *sem_open(char *name,int flag,mode_t mode,int val);

Abre(crea)unsemforoconnombre.
int sem_close(sem_t *sem);

Cierraunsemforoconnombre.
int sem_unlink(char *name);

Borraunsemforoconnombre.
int sem_wait(sem_t *sem);

RealizalaoperacinP(sem)sobreunsemforo.
int sem_post(sem_t *sem);

RealizalaoperacinV(sem)sobreunsemforo.

Productor-Consumidor
(buffer circular acotado)

P ro d u c to r

C o n s u m id o r

Productor-consumidor
/* crear los procesos
ligeros (hilos) */
pthread_create(&th1,
NULL, Productor, NULL);
pthread_create(&th2,
NULL, Consumidor, NULL);

#define MAX_BUFFER 1024


#define DATOS_A_PRODUCIR 100000
sem_t elementos; /* elementos buffer */
sem_t huecos; /* huecos buffer */
sem_t mutex; /* controla excl.mutua */
int buffer[MAX_BUFFER];/*buffer comn*/
void main(void)
{
pthread_t th1, th2; /* id. threads*/
/* inicializar los semaforos */
sem_init(&elementos, 0, 0);
sem_init(&huecos, 0, MAX_BUFFER);
sem_init(&mutex, 0, 1);

/* esperar su
finalizacion */
pthread_join(th1, NULL);
pthread_join(th2, NULL);
sem_destroy(&huecos);
sem_destroy(&elementos);
sem_destroy(&mutex);
exit(0);
}

Productor-consumidor
void Productor(void)
/* cdigo del productor */
{
int pos = 0; /* posicin dentro del buffer */
int dato;
/* dato a producir */
int i;
for(i=0; i < DATOS_A_PRODUCIR; i++ ) {
dato = i;
/* producir dato */
sem_wait(&huecos); /* un hueco menos */
sem_wait(&mutex);
/* entra en la seccin crtica */
buffer[pos] = dato;
pos = (pos + 1) % MAX_BUFFER;
sem_post(&mutex); /* deja la seccin crtica */
sem_post(&elementos); /* un elemento ms */
}
pthread_exit(0);
}

Productor-consumidor
void Consumidor(void)
{
int pos = 0;
int dato;
int i;

/* cdigo del Consumidor */

for(i=0; i < DATOS_A_PRODUCIR; i++ ) {


sem_wait(&elementos);
/* un elemento menos */
sem_wait(&mutex);
/* entra en la seccin crtica */
dato = buffer[pos];
pos = (pos + 1) % MAX_BUFFER;
sem_post(&mutex); /* deja la seccin crtica */
sem_post(&huecos);
/* un hueco ms */
/* consumir dato */
}
pthread_exit(0);
}

Lectores-escritores
int dato = 5; /* recurso */
int n_lectores = 0;/* num.
Lectores */
sem_t sem_lec; /* acceso
n_lectores */
sem_t mutex; /* acceso a dato */

pthread_create(&th3, NULL,
Lector, NULL);
pthread_create(&th4, NULL,
Escritor, NULL);
pthread_join(th1,
pthread_join(th2,
pthread_join(th3,
pthread_join(th4,

void main(void)
{
pthread_t th1, th2, th3, th4;

/*cerrar todos los


semaforos*/
sem_destroy(&mutex);
sem_destroy(&sem_lec);

sem_init(&mutex, 0, 1);
sem_init(&sem_lec, 0, 1);
pthread_create(&th1, NULL,
Lector, NULL);
pthread_create(&th2, NULL,
Escritor, NULL);

NULL);
NULL);
NULL);
NULL);

exit(0);
}

Lectores-escritores
void Lector(void) /* cdigo del lector */
{
sem_wait(&sem_lec);
n_lectores = n_lectores + 1;
if (n_lectores == 1)
sem_wait(&mutex);
sem_post(&sem_lec);
printf(``%d\n'', dato);

/* leer dato */

sem_wait(&sem_lec);
n_lectores = n_lectores - 1;
if (n_lectores == 0)
sem_post(&mutex);
sem_post(&sem_lec);
pthread_exit(0);
}

Lectores-escritores
void Escritor(void)
escritor */
{
sem_wait(&mutex);
dato = dato + 2;
recurso */
sem_post(&mutex);
pthread_exit(0);
}

/* cdigo del

/* modificar el

Potrebbero piacerti anche