Filosofos Comensales
Enviado por mdiego88 • 20 de Agosto de 2012 • 1.567 Palabras (7 Páginas) • 599 Visitas
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#include "msecond.h"
#include "random_int.h"
#define NUM_PHILOSOPHERS 5 /* Must be 5 */
#define MEAN_THINK_TIME 1000 /* avg think time in milliseconds */
#define MEAN_EAT_TIME 750 /* avg eat time in milliseconds */
/*
* Global (shared) variables.
*/
float total_time_spent_waiting = 0.0;
int total_number_of_meals = 0;
/*--------------------------------------------------------------------------*/
/*
* Macros to encapsulate the POSIX semaphore functions.
*/
#define semaphore_create(s,v) sem_init( &s, 0, v )
#define semaphore_wait(s) sem_wait( &s )
#define semaphore_signal(s) sem_post( &s )
#define semaphore_release(s) sem_destroy( &s )
typedef sem_t semaphore;
/*
* Each chopstick is represented by a semaphore. We also need a semaphore
* to control screen accesses so that only one thread at a time can write to
* it, and another semaphore to control modifications of the shared variables.
*/
semaphore chopstick[NUM_PHILOSOPHERS];
semaphore screen;
semaphore mutex;
/*--------------------------------------------------------------------------*/
/*
* Define data and routines for screen management
*/
int screen_row[NUM_PHILOSOPHERS] = { 6, 2, 2, 6, 10 };
int screen_col[NUM_PHILOSOPHERS] = { 31, 36, 44, 49, 40 };
int chopstick_row[5] = { 9, 4, 3, 4, 9 };
int chopstick_col[5] = { 35, 33, 40, 47, 45 };
char chopstick_sym[5] = { '/', '\', '|', '/', '\' };
/*
* The following macros are used for screen management using ANSI escape
* sequences. In the case of position_flush() a trailing newline is sent to
* flush the output stream - of course this changes the cursor location.
*/
#define cls() printf( "