Добавил:
Fragga
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:лабораторки по ОС препод Челноков / 2семестр / wi2 / zubanov / lab41 / barb
.c#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
static int ctrl_c(int);
int msgid;
int main(void)
{
typedef struct msgbuf
{
long mtype;
char buf;
} message;
message mes;
key_t key;
int free=5;
int slp=1;
int n=0;
char t;
int a[5]={'0','1','2','3','4'};
signal(SIGINT,(void *)&ctrl_c);
mes.mtype=1L;
if( (key=ftok("barb",'c'))<0)
{printf("key error\n " );exit(1);
};
if ((msgid=msgget(key,0666|IPC_CREAT))<0)
{printf(" queue error \n");exit(1);}
while (1)
{
n=msgrcv(msgid,&mes,sizeof(message),mes.mtype,0);
if (n>0)
{
printf("Became %c client\'s\nFree chair's %d \n",mes.buf,free);
slp=0;
for (t=0;t<=4;t++)
{if (a[t]==mes.buf)
mes.buf=t;}
if (free>0)
{
if (mes.buf<=free)
{free=free-mes.buf;}
else
{
printf("%d client\'s went out\n",mes.buf-free);
free=0;
}
}
else
{
printf("Not enough chairs\n");
}
printf("%d free chairs\n",free);
sleep(1);
if (free<=4)
{
free++;
}
printf("%d free chairs\n",free);
}
if (free==5) slp=1;
}
return 0;
}
static int ctrl_c(int sig)
{
signal(SIGINT,(void *)&ctrl_c);
msgctl(msgid,IPC_RMID,0);
printf("Barber going out\n");
exit(0);
}