The CriticalSection Problem TwoProcess Solution z Algorithm 1
The Critical-Section Problem (Two-Process Solution) z. Algorithm 1 //turn is global and initialized to 0 repeat while turn<>i do no-op; //enters critical section turn : = j; //remainder section until false; z. What is the problem here? 1/6/2022 1
The Critical-Section Problem (Two-Process Solution) z. Algorithm 2 var flag: array [0. . 1] of boolean; repeat flag[i] = true; while flag[j] do no-op; //enters critical section flag[i] = false; //remainder section until false; z. What is the problem here? 1/6/2022 2
The Critical-Section Problem (Two-Process Solution) z. Algorithm 3 var flag: arry [0. . 1] of boolean; turn: 0. . 1; repeat flag[i] = true; turn = j; while (flag[j] and turn = j) do no-op; //enters critical section flag[i] = false; //remainder section until false; z. Why does this solution work? z. Does it satisfy all three properties? 1/6/2022 3
The Critical-Section Problem (using Test-and-Set) z. What does this hardware primitive do? (how is it defined? ) z. How do we use it to solve the critical section problem? 1/6/2022 4
The Critical-Section Problem (using Test-and-Set) z. Solution (p. 165 in the book) //lock is global and initialized to false repeat while Test-and-Set(lock) do no-op; //enters critical section lock = false; //remainder section until false; z. Does it satisfy all three properties? 1/6/2022 5
The Critical-Section Problem (using Semaphores) z Semaphore definition wait(S): while s<=0 do no-op; S : = S-1; signal(S): S : = S+1; z Usage for solving critical section problem //mutex is a semaphore and is initialized to 1 repeat wait(mutex); //enters critical section signal(mutex); //remainder section until false; z What about changing semaphore definition to: wait(S): S : = S-1; signal(S): S : = S+1; z Does it satisfy all three properties? 1/6/2022 6
The Bounded-Buffer Problem (using Semaphores) z Algorithm //mutex, empty, full are all semaphores //initially, mutex=1; empty=n; full=0; //producer repeat //produce r P(empty); P(mutex); add r to buffer; V(mutex); V(full); until false; //consumer repeat P(full); P(mutex); remove r from buffer; V(mutex); V(empty); process r; until false; 1/6/2022 7
The Bounded-Buffer Problem (using Semaphores) z Question: Is this solution efficient? (Imagine a buffer with n slots) z How can it be changed to improve its efficiency? 1/6/2022 8
More on Semaphores. . . z. Historical background First introduced by Dijkstra when describing the THE system (Edsger W. Dijkstra. The Structure of the "THE" Multiprogramming System. Communications of the ACM 11(5), May 1968. ) z. What is the problem with semaphores? Semaphores only work when all processes are wellbehaved! 1/6/2022 9
More on Semaphores. . . z How to overcome the problem with “untrustworthy” semaphores? y Critical regions y Monitors y Using message buffering and message queues for each process y (Per Brinch Hansen. The Nucleus of a Multiprogramming System. Communications of the ACM 13(4), April 1970. ) xsend message (receiver, message, buffer); xwait message (sender, message, buffer); xsend answer (result, answer, buffer); xwait answer (result, answer, buffer); x. What are the potential complications here? 1/6/2022 10
- Slides: 10