Temas Variados / Filosofos Comensales

Filosofos Comensales

Ensayos Gratis: Filosofos Comensales
Ensayos de Calidad, Tareas, Monografias - busque más de 1.928.000+ documentos.

Enviado por:  mdiego88  20 agosto 2012
Tags: 
Palabras: 1567   |   Páginas: 7
Views: 222

#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( "33[H33[J" )

#define position(row,col) printf( "33[%d;%dH", (row), (col) )

#define position_flush(

row,col) printf( "33[%d;%dHn", (row), (col) )

void init_screen( void )

/* Draw an initial representation of the philosophers' "world" on the screen */

{

int i;

/*

* Draw rice bowl

*/

cls();

position( 6, 37 );

printf( "\ /" );

position( 7, 38 );

printf( "\___/" );

/*

* Print philosopher numbers at their locations. Show "eat_count" as 0.

*/

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

{

position( screen_row[i], screen_col[i] );

printf( "%d", i );

position( screen_row[i] + 1, screen_col[i] - 1 );

printf( "(%d)", 0 );

position( chopstick_row[i], chopstick_col[i] );

printf( "%c", chopstick_sym[i] );

}

position_flush( 13, 1 );

}

void draw_thinking( int n )

/* Display T for "thinking" at the appropriate location */

{

semaphore_wait( screen );

position( screen_row[n], screen_col[n] ...



Suscríbase a ClubEnsayos

Suscríbase a ClubEnsayos - busque más de 1.928.000+ documentos