Filosofos Comensales
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/sem.h>#define NUM_FILOSOFOS 5
#define PENSANDO 5
#define COMIENDO 5
#define LIBRE 5
#defineUSADO 5
typedef struct datos_filosofos {
int filosofos[NUM_FILOSOFOS];
int pids[NUM_FILOSOFOS];
intsemaforo_id,semaforo_id2;
int memoria_id;
} datos_filosofos;
struct datos_filosofos *p_filosofos;
//Reservar semáforos y memoria compartida
int crea_filosofos(void)
{
int i;
int sem_id,sem_id2,mem_id;// Crea el área de memoria compartida
if((mem_id=shmget(IPC_PRIVATE,sizeof(datos_filosofos),0774 | IPC_CREAT))==-1)
return -1;if(!(p_filosofos=(datos_filosofos *)shmat(mem_id,(char *)0,0)))
return -2;
// Crea el set de semáforos para los cubiertosif((sem_id=semget(IPC_PRIVATE,NUM_FILOSOFOS,0774 | IPC_CREAT))==-1)
return -3;
// Crea el semáforo que evitará el interbloqueoif((sem_id2=semget(IPC_PRIVATE,1,0774 | IPC_CREAT))==-1)
return -3;
// Inicializa el área de memoria compartidafor(i=0;i<NUM_FILOSOFOS;i++)
{
p_filosofos->filosofos[i]=PENSANDO;
p_filosofos->pids[i]=0;
semctl(sem_id,i,SETVAL,LIBRE);...
Regístrate para leer el documento completo.