1 / 22

4330 THIRD ASSIGNMENT

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.

cira
Download Presentation

4330 THIRD ASSIGNMENT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4330 THIRD ASSIGNMENT Summer 2014

  2. 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

  3. A very simplified view I can let three male patrons in for each female patron who shows up

  4. 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

  5. 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)

  6. Underlying idea (I) • The current value of the semaphore admit represents the current number of available admission tokens Admit one male patron

  7. 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

  8. 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

  9. A better solution (I) • Two constraints: • Cannot prevent female patrons from leaving • Cannot quick out male patrons

  10. 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.

  11. 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

  12. Pseudocode • nfemales +=1 # inside a critical sectionncanadmit = 3*nfemales – nmalesfor i in range(0, ncanadmit) : V(&admit)

  13. 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

  14. Pseudocode • nfemales –=1 # inside a critical sectionntoomany = nmales – 3*nfemalesfor i in range(0, ntoomany): if value(&admit) > 0 : # decrement value(&admit) P(&admit)

  15. Male patron arrival • Do P(&admit) • Increment number of male patrons

  16. Pseudocode • P(&admit)nmales +=1

  17. 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

  18. Pseudocode • nmales –= 1 # inside a critical sectionncanadmit = 3*nfemales – nmales if ncanadmit > 0 : V(&admit)

  19. 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( )

  20. 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

  21. 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

  22. 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

More Related