Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
26.05.2014
Размер:
1.16 Кб
Скачать
#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);
}
Соседние файлы в папке lab41
  • #
    26.05.20141.16 Кб4barb.c
  • #
    26.05.201412.13 Кб4barber
  • #
    26.05.201412.77 Кб4barber.out
  • #
    26.05.2014806 б4cl.c
  • #
    26.05.201412.42 Кб4cl.out
  • #
    26.05.201411.74 Кб4client