Created
November 14, 2012 22:45
-
-
Save millimoose/4075391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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