Skip to content

Instantly share code, notes, and snippets.

@millimoose
Created November 14, 2012 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millimoose/4075391 to your computer and use it in GitHub Desktop.
Save millimoose/4075391 to your computer and use it in GitHub Desktop.
#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
}
}
int sem_getval(int semid) {
union semun s;
return semctl(semid, 0, GETVAL, s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment