hw2sol

hw2sol - HW 2 SOLUTIONS(1 Solution to Orphan problem...

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

HW 2 SOLUTIONS (1) Solution to Orphan problem: Semaphore empty = new Semaphore(0); Semaphore mutex = new Semaphore(1); Semaphore cook = new Semaphore (0); integer M = MAX CAPACITY; integer S = M; procedure O RPHAN CODE while true do mutex.P(); if S == 0 then cook.V(); empty.P(); end if S = S - 1; mutex.V(); eat(); end while end procedure procedure C OOK CODE while true do cook.P(); S = M; empty.V(); fillPot(); end while end procedure (2) Solutions to Searchers, Inserters & Deleters Semaphore mutex = new Semaphore(1); Semaphore listSem = new Sempahore(1); Semaphore insertMutex =new Semaphore(1); integer active = 0; procedure S EARCHER THREAD while true do mutex.P(); active = active +1; if active == 1 then listSem.P(); end if 1

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

View Full Document
2 HW 2 SOLUTIONS mutex.V(); search(); mutex.P(); active = active -1; if active == 0 then listSem.V(); end if mutex.V(); use data(); end while end procedure procedure I NSERTER THREAD while true do insertMutex.P(); mutex.P(); active = active +1; if active == 1 then listSem.P(); end if mutex.V(); search(); mutex.P(); active = active -1; if active == 0 then listSem.V(); end if mutex.V(); use data(); insertMutex.V();
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