# hw2 - HW#2 SYNCHRONIZATION Choose three of the ve problems...

This preview shows pages 1–2. Sign up to view the full content.

HW #2 SYNCHRONIZATION Choose three of the five problems and provide a synchronization solution using Semaphores. Where appropriate we have given some stub-functions, you do not have to use the same code in your solution. As a solution, you may submit either pseudo-code, real code, or nat- ural language. If you submit a natural language solution, it must explicitly state where the P’s and V’s of your mutexes and sempahores should go. If you code your solution using JNachos, and it runs fine, this may not indicate your solution is perfect as pre-emption can only happen at specific times, unlike a real machine, where pre-emption can happen at any time. (1) Oliver Twist: There are N orphans at an orphanage. During meal time there is a communal pot full of gruel (food), that the orphans help themselves to. The pot has an integer maximum capacity of M , and a current amount S , initially the pot is full. The orphans run a loop where they fill their bowls and then eat (decreasing the contents of the communal pot) ( S - 1). If the pot becomes empty ( S == 0), then orphans must wait for the cook to come along and refill the pot ( S = M ). 1: procedure Orphan code (UnSynchronized) 2: while true do 3: getServing(); 4: eat(); 5: end while 6: end procedure 7: 1: procedure Cook code (UnSynchronized) 2: while true do 3: fillPot(); 4: end while 5: end procedure Synchronized constraints: Constraints: The cook can only invoke fillPot(), if the pot is empty.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern