220 likes | 381 Views
4330 THIRD ASSIGNMENT. Summer 2014. The problem. The owners of a club want to avoid an excessive number of male patrons They instruct the bouncer to refuse admission to male patrons whenever there would otherwise be more than three male patrons foe each female patron inside the club.
E N D
4330 THIRD ASSIGNMENT Summer 2014
The problem • The owners of a club want to avoid an excessive number of male patrons • They instruct the bouncer to refuse admission to male patrons whenever there would otherwise be more than three male patrons foe each female patron inside the club
A very simplified view I can let three male patrons in for each female patron who shows up
Our approach • A main program will fork processes representing either male or female patrons • No bouncer process • Its role will be simulated by the patron processes themselves
A naïve solution • Have one semaphore admit controlling the admission of male patrons • Initial value is zero • When a male patron arrives, he does P(&admit) • When a female patron arrives, she does three V(&admit) • When a male patron leaves, he does V(&admit)
Underlying idea (I) • The current value of the semaphore admit represents the current number of available admission tokens Admit one male patron
Underlying idea (II) • Male patron must have a token to enter the club • Handed out FCFS by the bouncer • Each time a female patron arrives, the bouncer gets three new tokens • Each time a male patron leaves, he gives back his token to the bouncer
The problem • It does not work because female patrons can leave at any time: • Alice arrives • Bob, Charles and Dean get in • Alice leaves • Emily arrives • Cannot admit Fred, George and Henry
A better solution (I) • Two constraints: • Cannot prevent female patrons from leaving • Cannot quick out male patrons
A better solution • Will restrict • the creation of new tokens when a female patron arrives • the recycling of tokens when a male patron leaves • Will sometimes delete available tokens when a female patron leaves.
Female patron arrival • Updae the number of female patrons • Check how many male patrons can enter the club without having more than three male patrons for each female patrons • Do as many do V() operations on the admit semaphore • Zero, one, two, or three • We limit the creation of new tokens
Pseudocode • nfemales +=1 # inside a critical sectionncanadmit = 3*nfemales – nmalesfor i in range(0, ncanadmit) : V(&admit)
Female patron departure • Update number of female patrons • Check the value of the admit semaphore • Represents the number of pending tokens • If and only if admitting these male patrons would cause result in the admission of too many male patrons, do the required number of P() operations on the admit semaphore. • Will delete the required number of tokens
Pseudocode • nfemales –=1 # inside a critical sectionntoomany = nmales – 3*nfemalesfor i in range(0, ntoomany): if value(&admit) > 0 : # decrement value(&admit) P(&admit)
Male patron arrival • Do P(&admit) • Increment number of male patrons
Pseudocode • P(&admit)nmales +=1
Male patron departures • Update the number of male patrons • Do a V() operation on the admit semaphore if and only it there are enough female patrons in the club • Limit token recycling
Pseudocode • nmales –= 1 # inside a critical sectionncanadmit = 3*nfemales – nmales if ncanadmit > 0 : V(&admit)
Important • Do not forget the –lrt option when you compile your code • Use unique names for your semaphores and shared memory segments • Delete them after each run of your program • Or expect unexpected errors • Terminate promptly your processes using _exit( )
Compiling your program • If the compiler returns error messages such as undefined reference to `sem_open' • You forgot the –lrt flag • Your Linux system does not support Posix semaphores
Debugging your program (I) • If your program crashes after informing you that it cannot create a semaphore or a shared memory segment • Check that nobody else uses the same semaphore names and shared memory key
Debugging your program (II) • If your program does not work anymore as it did before your last change • Delete your semaphores and shared memory segments then retry