Documentos de Académico
Documentos de Profesional
Documentos de Cultura
#include <windows.h>
#include <stdio.h>
#define VIAJEROS 4
//MACROS
#define PERROR(message, value) \
{
\
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,0,NULL );
fprintf(stderr,"%s:%s\n",message,lpMsgBuf);
LocalFree( lpMsgBuf );
return(value);
}
\
\
\
\
\
\
\
\
\
\
\
//ESTRUCTURAS
typedef enum enum_personas {Mu, Mo, R1, R2} PERSONA;
typedef enum enum_lugares {ESPERANDO, ASCENSOR, ESCALERA} LUGAR;
struct datos
{
HANDLE ascensor, robus, control;
int ipEnEspera[VIAJEROS];
int ipEnAscensor[VIAJEROS];
int ipEnEscalera[VIAJEROS];
};
typedef struct PARAMETRO
{
")
")
")
")
:
:
:
:
printf("
printf("
printf("
printf("
");
");
");
");
printf("\tAscensor: ");
(info->ipEnAscensor[Mu]
(info->ipEnAscensor[Mo]
(info->ipEnAscensor[R1]
(info->ipEnAscensor[R2]
==
==
==
==
1)
1)
1)
1)
?
?
?
?
printf("Mu
printf("Mo
printf("R1
printf("R2
")
")
")
")
:
:
:
:
printf("
printf("
printf("
printf("
");
");
");
");
printf("\tEscalera: ");
(info->ipEnEscalera[Mu]
(info->ipEnEscalera[Mo]
(info->ipEnEscalera[R1]
(info->ipEnEscalera[R2]
==
==
==
==
1)
1)
1)
1)
?
?
?
?
printf("Mu
printf("Mo
printf("R1
printf("R2
")
")
")
")
:
:
:
:
printf("
printf("
printf("
printf("
");
");
");
");
printf("\n");
fflush(stdout);
}
void cambiarEstado(struct datos *info, PERSONA persona, LUGAR lugar)
{
switch(lugar)
{
case ESPERANDO:
info->ipEnEspera[persona] = 1;
info->ipEnAscensor[persona] = 0;
info->ipEnEscalera[persona] = 0;
break;
case ASCENSOR:
info->ipEnEspera[persona] = 0;
info->ipEnAscensor[persona] = 1;
info->ipEnEscalera[persona] = 0;
break;
case ESCALERA:
info->ipEnEspera[persona] = 0;
info->ipEnAscensor[persona] = 0;
info->ipEnEscalera[persona] = 1;
break;
default:
fprintf(stderr, "Lugar no especificado");
exit(100);
}
}
DWORD WINAPI fnHilo(LPVOID param)
{
INT val, peso;
PERSONA persona;
struct datos *info;
LPPARAMENTRO pParametro = (LPPARAMENTRO) param;
info = pParametro->lpDatos;
persona = pParametro->persona;
peso = pParametro->peso;
while(TRUE)
{
val = WaitForSingleObject(info->ascensor, INFINITE);
if(WAIT_FAILED == val)
PERROR("Decrementar semaforo ascensor", 11);
val = WaitForSingleObject(info->control,INFINITE);
if(WAIT_FAILED == val)
PERROR("Decrementar semaforo control", 12);
cambiarEstado(info, persona, ASCENSOR);
escribir(info);
val = ReleaseSemaphore(info->control, 1, NULL);
if(!val)
PERROR("Incrementar semaforo control", 13);
Sleep(3000);
val = WaitForSingleObject(info->control, INFINITE);
if(WAIT_FAILED == val)
PERROR("Decrementar semaforo control", 14);
cambiarEstado(info, persona, ESCALERA);
escribir(info);
val = ReleaseSemaphore(info->control, 1, NULL);
if(!val)
PERROR("Incrementar semaforo control", 15);
val = ReleaseSemaphore(info->ascensor, 1, NULL);
if(!val)
PERROR("Incrementar semaforo control", 16);
Sleep(1000);
val = WaitForSingleObject(info->control,INFINITE);
if(WAIT_FAILED == val)
PERROR("Decrementar semaforo control", 17);
cambiarEstado(info, persona, ESPERANDO);
escribir(info);
val = ReleaseSemaphore(info->control, 1,NULL);
if(!val)
PERROR("Incrementar semaforo control\n", 18);
}
}
int main (int argc, char *argv[])
{
INT i;
struct datos *info;
HANDLE lpProcess[4];
DWORD IdMu, IdMo, IdR1, IdR2;
DWORD salidaProcess;
LPPARAMENTRO pParametro[VIAJEROS];
for(i=0;i<VIAJEROS;i++)
pParametro[i] = (LPPARAMENTRO) malloc(sizeof(struct PARAMETRO));
if (argc != 1)
fprintf(stderr,"Numero de parametros incorrecto\n");
info = (struct datos *) malloc(sizeof(struct datos));
if (NULL == info)
PERROR("Error al reservar memoria", 2);
info->ipEnEspera[Mu]
info->ipEnEspera[Mo]
info->ipEnEspera[R1]
info->ipEnEspera[R2]
=
=
=
=
1;
1;
1;
1;