public
Created

  • Download Gist
main.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#define LOG_PV(func, sem, proc) \
printf("["#proc"] before "#func"("#sem") = %d\n", sem_getval(sem));\
func(sem);\
printf("["#proc"] after "#func"("#sem") = %d\n", sem_getval(sem));
 
#define LOG_P(sem, proc) \
LOG_PV(P, sem, proc)
 
#define LOG_V(sem, proc) \
LOG_PV(V, sem, proc)
 
//Producer process
if(pid > 0)
{
while(1)
{
LOG_P(emptyShelf, Waiter);
 
LOG_P(mutex, Waiter);
waiter_as_producer();
LOG_V(mutex, Waiter);
LOG_V(orderOnShelf, Waiter);
LOG_P(pizzaOnShelf, Waiter);
LOG_P(mutex, Waiter);
waiter_as_consumer();
LOG_V(mutex, Waiter);
LOG_V(emptyShelf, Waiter);
}
}
 
if(pid == 0)
{
while(1)
{
LOG_P(orderOnShelf, Cook); // make sure there is an order on shelf
LOG_P(mutex, Cook); //permission to work
cooker_as_consumer(); // take order and put pizza on shelf
LOG_V(mutex, Cook); //release permission
LOG_V(pizzaOnShelf, Cook); // pizza is now on shelf
}
}
sem.h
C
1 2 3 4
int sem_getval(int semid) {
union semun s;
return semctl(semid, 0, GETVAL, s);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.