Vous êtes sur la page 1sur 8

BARBERO DORMILON

#include <stdio.h>

#include <unistd.h>

#include <pthread.h>

#define TRUE 1

#define FALSE 0

#define CHAIRS 3 //Sillas Disponibles

#define CANT_CUST 5 //Cantidad de Clientes

#define T_CUST 0 //Tiempo de llegada de Clientes

#define T_CORTE 3 //Tiempo de corte de pelo

typedef int semaphore;

//Prototipo de funciones

void *customer (void *);

void *barber (void *);

void up (semaphore *);

void down (semaphore *);

semaphore sem_barber=0, sem_customer=0,sem_mutex=1;

int waiting=0;

//Main function

int main (void) {

pthread_t barb_t,cust_t[CANT_CUST];

int i;

pthread_create(&barb_t,NULL,barber,NULL);

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

sleep(T_CUST);

pthread_create(&cust_t[i],NULL,customer,NULL);
}

pthread_join(barb_t,NULL);

return(0);

void *customer (void *n) {

printf ("Cliente:entrando hay %d esperando\n",waiting);

down (&sem_mutex);

if (waiting < CHAIRS) {

waiting++;

up (&sem_customer);

up (&sem_mutex);

down (&sem_barber);

printf ("Cliente:Que bien esta quedando mi pelo.\n");

else {

up (&sem_mutex);

printf ("Cliente:No hay espacio en la peluqueria me voy !!!!.\n");

void *barber (void *j) {

printf ("Barbero:Empiezo a trabajar\n");

while (TRUE) {

down (&sem_customer);

down (&sem_mutex);

waiting--;

up (&sem_barber);

up (&sem_mutex);

printf ("Barbero:Comienzo el corte de pelo de un cliente quedan %d


esperando.\n",waiting);
sleep (T_CORTE);

printf ("Barbero:Termine de cortar el pelo de un cliente quedan %d


esperando.\n",waiting);

void up (semaphore *sem) {

*sem+=1;

void down (semaphore *sem) {

while (*sem<=0){};

*sem-=1;

FILOSOFOS COMENSALES
#include <stdio.h>

#include <unistd.h>

#include <pthread.h>

#define TRUE1

#define N5

#define LEFT(x)(((x)-1)%N)

#define RIGHT(X)(((X)+1)%N)

typedef struct

int value;

/* Lista de procesos. */

semaphore;

typedef enum

THINKING, HUNGRY, EATING

}
status;

/* Estado de cada filosofo. */

status estado[N];

/* Semaforo para exclusion mutua. */

semaphore mutex,

/* Semaforo para bloquear los filosofos adyacentes. */

s[N];

main ()

extern status estado[N];

extern semaphore mutex, s[N];

int i, sw = 1;

void Philosopher (int);

/* Inicializamos los semaforos. */

InitSemaphore (mutex, 1);

for (i = 0; i < N; i++)

InitSemaphore (s[i], 0);

/* Inicializamos los estados. */

for (i = 0; i < N; i++)

estado[i] = THINKING;

/* Inicializamos los filosofos. */

for (i = 0; (i < N) && (sw); i++)

if (!(sw = fork ()))

Philosopher (i);

void

Philosopher (int i)

void think (), eat (), TakeForks (int), PutForks (int);

while (TRUE)

think ();
/* Obtiene dos tenedores o se bloquea. */

TakeForks (i);

eat ();

PutForks (i);

void

TakeForks (int i)

void test (int);

extern semaphore mutex, s[N];

extern status estado[N];

/* Acceso a seccion critica. */

wait (mutex);

estado[i] = HUNGRY;

/* Intenta tomar los dos tenedores. */

test (i);

signal (mutex);

/* Se bloquea si no consiguio los tenedores. */

wait (s[i]);

void

PutForks (int i)

void test (int);

extern semaphore mutex;

extern status estado[N];

/* Acceso a seccion critica. */

wait (mutex);

estado[i] = THINKING;

/* Comprueba si el vecino izquierdo puede comer ahora. */

test (LEFT (i));


/* Comprueba si el vecino derecho puede comer ahora. */

test (RIGHT (i));

signal (mutex);

void

test (int i)

extern semaphore s[N];

extern status estado[N];

if (estado[i] == HUNGRY && estado[LEFT (i)] != EATING &&

estado[RIGHT (i)] != EATING)

estado[i] = EATING;

signal (s[i]);

LECTORES Y ESCRITORES
#include <stdlib.h>

#include <stdio.h>

#include <semaphore.h>

#include <pthread.h>

#include <math.h>

#include <sys/sem.h>

#include <time.h>

#define TRUE 1

semforo mutex_r; // Controla exclusin mutua para el n de lectores

semforo mutex_w; // Controla exclusin mutua para el n de escritores

semforo lectores; //Controla entrada de lectores

semforo escritores; //Controla entrada de escritores


int nw = 0; //Contabiliza n de escritores

int nr = 0; //Contabiliza n de lectores

main(){

IniciarSemaforo (mutex_r, 1);

IniciarSemaforo (mutex_w, 1);

IniciarSemaforo (lectores, 1);

IniciarSemaforo (escritores, 1);

void Lector ()

extern SEMAFORO mutex_r, lectores, escritores;

extern int nr;

while (TRUE)

wait(lectores);

wait (mutex_r);

nr++;

if(nr==1)

wait(escritores);

signal(mutex_r);

signal(lectores);

LeerDatos (); //Seccion Critica

wait (mutex_r);

nr;

if (nr == 0)

signal (escritores);

signal (mutex_r);

void Escritor ()

extern SEMAFORO mutex_w, lectores, escritores;


extern int nw;

while (TRUE)

wait (mutex_w);

nw++;

if(nw==1)

wait(lectores);

signal(mutex_w);

wait(escritores);

EscribirDatos (); //Seccion Critica

signal (escritores);

wait(mutex_w);

nw;

if(nw==0)

signal(lectores);

signal(mutex_w)

Vous aimerez peut-être aussi