Structure of process Pi Each process has a code segment called the critical

# Structure of process pi each process has a code

• edwardtoo165
• 58

This preview shows page 13 - 22 out of 58 pages.

± Structure of process P i ---- Each process has a code segment, called the critical section, in which the shared data is accessed. repeat entry section /* enter critical section */ critical section /* access shared variables */ exit section /* leave critical section */ remainder section /* do other work */ until false ° Problem ± Ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.
Solution: Critical Section Problem - Requirements ± Mutual Exclusion ± If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. ± Progress ± If no process is executing in its critical section and there exists some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. ± Bounded Waiting ± A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted.
Solution: Critical Section Problem - Requirements ° Assume that each process executes at a nonzero speed. ° No assumption concerning relative speed of the n processes.
Solution: Critical Section Problem -- Initial Attempt ° Only 2 processes, P0 and P1 ° General structure of process Pi (Pj) repeat entry section critical section exit section remainder section until false ° Processes may share some common variables to synchronize their actions.
Algorithm 1 ± Shared Variables: ° var turn : (0..1); initially turn = 0; ° turn = i ³ P i can enter its critical section ± Process P i repeat while turn <> i do no-op; critical section turn := j; remainder section until false Satisfies mutual exclusion, but not progress.
Algorithm 2 ± Shared Variables ° var flag : array (0..1) of boolean; initially flag[0] = flag[1] = false; ° flag[i] = true ³ Pi ready to enter its critical section ± Process P i repeat flag [ i ] := true ; while flag[j] do no-op; critical section flag[i] := false; remainder section until false Can block indefinitely…. Progress requirement not met.
Algorithm 3 ± Shared Variables ° var flag : array (0..1) of boolean; initially flag[0] = flag[1] = false; ° flag[i] = true ³ Pi ready to enter its critical section ± Process P i repeat while flag[j] do no-op; flag [ i ] := true ; critical section flag[i] := false; remainder section until false Does not satisfy mutual exclusion requirement ….
Algorithm 4 ± Combined Shared Variables of algorithms 1 and 2 ± Process Pi repeat flag [ i ] := true ; turn := j ; while ( flag [ j ] and turn = j) do no-op; critical section flag[i] := false; remainder section until false YES!!! Meets all three requirements, solves the critical section problem for 2 processes.
Bakery Algorithm ° Critical section for n processes ° Before entering its critical section, process receives a number. Holder of the smallest number enters critical section.

#### You've reached the end of your free preview.

Want to read all 58 pages?

• One '20
• producer, monitor, Semaphore

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