#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#define SEM_LINES 0
#define SEM_INAIR 1
#define SEM_HOWMANY 2
#define FALSE 0
#define TRUE 1
#define MYDELAY 6000
/* priorytety */
#define RQ_LAND 1
#define RQ_START 2
#define RQ_FREE 3
/* klucze */
#define SEMKEY 911
#define ALLKEY 997
#define ANSKEY 998
#define FREKEY 999
struct sembuf sembuf1;
struct queue_entry
{
long mtype;
int plane_id;
} entry;
extern int errno;
int sem_id;
void blady(int blad)
{
printf("errno is %d and means ",blad);
switch(blad)
{
case EACCES: printf("EACCES error !\n");break;
case EEXIST: printf("EEXIST error !\n");break;
case EIDRM : printf("EIDRM error !\n");break;
case ENOENT: printf("ENOENT error !\n");break;
case ENOMEM: printf("ENOMEM error !\n");break;
case ENOSPC: printf("ENOSPC error !\n");break;
}
}
void oper_P(int sem)
{
sembuf1.sem_num=sem;
sembuf1.sem_op=-1; /* sem-- */
sembuf1.sem_flg=SEM_UNDO;
semop(sem_id,&sembuf1,1);
}
void oper_V(int sem)
{
sembuf1.sem_num=sem;
sembuf1.sem_op=1; /* sem++ */
sembuf1.sem_flg=SEM_UNDO;
semop(sem_id,&sembuf1,1);
}
int oper_Get(int sem)
{
return semctl(sem_id,sem,GETVAL,0);
}
void czekaj(int czas)
{
int i,j;
if(czas>0)
for(i=0;i<czas;i++)
for(j=0;j<MYDELAY;j++);
}
#include "my.h"
union semun semun1;
struct sembuf sembuf1;
unsigned short semarray[3];
extern int errno;
int sem_id;
int allocq_id;
int answerq_id;
int freeq_id;
int plane_id;
int CONST_M;/* liczba pasow startowych */
int CONST_K;/* ograniczenie nakladane na ilosc samolotow w powietrzu */
void tworz_sem(void)
{ /* init */
if((sem_id=semget(SEMKEY,3,0666|IPC_CREAT|IPC_EXCL))==-1)
{
printf("sem_id == -1\n");
blady(errno);
exit(0);
}
semarray[SEM_LINES]=CONST_M;
semarray[SEM_INAIR]=0;
semarray[SEM_HOWMANY]=0;
semun1.array=semarray;
if(semctl(sem_id,0,SETALL,semun1)==-1)
{
printf("semctl error -1\n");
blady(errno);
exit(0);
}
}
void CTRL_C()
{
semctl(sem_id,0,IPC_RMID,0);
msgctl(allocq_id,IPC_RMID,0);
msgctl(answerq_id,IPC_RMID,0);
msgctl(freeq_id,IPC_RMID,0);
printf("Koncze poniewaz nacisnieto CTRL-C\n");
exit(0);
}
int main(int argc, char **argv)
{
if(argc != 3)
{
printf("wlasciwe uzycie\n %s liczba_pasow ograniczenie\n",argv[0]);
exit(1);
}
CONST_M=atoi(argv[1]);
CONST_K=atoi(argv[2]);
signal(SIGINT,CTRL_C);
tworz_sem();
if(fork()==0)
{
/* zajmowanie */
if((allocq_id=msgget(ALLKEY,0666|IPC_CREAT|IPC_EXCL))==-1) exit(0);
if((answerq_id=msgget(ANSKEY,0666|IPC_CREAT|IPC_EXCL))==-1) exit(0);
while(TRUE)
{
if(oper_Get(SEM_INAIR)>CONST_K)
msgrcv(allocq_id,&entry,sizeof(plane_id),(-1)*RQ_START,0);
else
msgrcv(allocq_id,&entry,sizeof(plane_id),0,0);
oper_P(SEM_LINES);
plane_id=entry.plane_id;
entry.mtype=plane_id;
msgsnd(answerq_id,&entry,sizeof(plane_id),IPC_NOWAIT);
}
}
if(fork()==0)
{
if((freeq_id=msgget(FREKEY,0666|IPC_CREAT|IPC_EXCL))==-1) exit(0);
while(TRUE)
{
msgrcv(freeq_id,&entry,sizeof(plane_id),RQ_FREE,0);
oper_V(SEM_LINES);
}
/* zwalnianie */
}
while(TRUE)
{
printf("\n");
printf("Wolnych jest %d pasow startowych\n",oper_Get(SEM_LINES));
printf("W powietrzu jest %d samolotow\n",oper_Get(SEM_INAIR));
printf("Razem jest %d samolotow\n",oper_Get(SEM_HOWMANY));
czekaj(500);
}
}
#include "my.h"
short onScreen;
union semun semun1,semun2;
int plane_id;
int allocq_id;
int freeq_id;
int answerq_id;
void przerwa(void)
{
printf("Tu %d ",plane_id);
}
int main(void)
{
onScreen=TRUE;
if((sem_id=semget(SEMKEY,3,0666))==-1)
{
printf("sem_id==-1\n");
blady(errno);
exit(0);
}
if((allocq_id=msgget(ALLKEY,0222))==-1)
{
printf("allocq_id==-1\n");
blady(errno);
exit(0);
}
if((freeq_id=msgget(FREKEY,0222))==-1)
{
printf("freeq_id==-1\n");
blady(errno);
exit(0);
}
if((answerq_id=msgget(ANSKEY,0444))==-1)
{
printf("answerq_id==-1\n");
blady(errno);
exit(0);
}
oper_V(SEM_HOWMANY);
plane_id= oper_Get(SEM_HOWMANY);
while(TRUE)
{
/* plane loop */
/* start */
entry.mtype=RQ_START;
entry.plane_id=plane_id;
msgsnd(allocq_id,&entry,sizeof(plane_id),IPC_NOWAIT);
msgrcv(answerq_id,&entry,sizeof(plane_id),plane_id,0);
przerwa();printf(" startuje\n");
czekaj(200);/* startuje */
oper_V(SEM_INAIR);
entry.mtype=RQ_FREE;
entry.plane_id=plane_id;
msgsnd(freeq_id,&entry,sizeof(plane_id),IPC_NOWAIT);
przerwa();printf(" leci\n");
czekaj(1000); /* leci */
/* lot */
entry.mtype=RQ_LAND;
entry.plane_id=plane_id;
msgsnd(allocq_id,&entry,sizeof(plane_id),IPC_NOWAIT);
msgrcv(answerq_id,&entry,sizeof(plane_id),plane_id,0);
oper_P(SEM_INAIR);
przerwa();printf(" laduje\n");
czekaj(200); /* laduje */
entry.mtype=RQ_FREE;
entry.plane_id=plane_id;
msgsnd(freeq_id,&entry,sizeof(plane_id),IPC_NOWAIT);
przerwa();printf(" stoi\n");
czekaj(1000); /* stoi w miejscu */
}
}