Download Producer-Consumer: Shared Memory - Operating Systems | CMSC 412 and more Study notes Operating Systems in PDF only on Docsity!
Announcements
z^
Program #2– Due in Tuesday at 9:00 AM
z^
Reading– Chapter 7
Shared Memory
Process 1
Process 2 Shared Region
z^
Like Threads, but only part of memory shared
z^
Allows communication without needing kernel action– Kernel calls setup shared region
Problems with the Producer-Consumer
Shared Memory Solution
z^
Consider the three address code for the counterCounter Increment
Counter Decrement
reg
= counter 1
reg
= counter 2
reg
= reg 1
reg
= reg 2
counter = reg
1
counter = reg
2
z^
Now consider an ordering of these instructionsT
0
producer
reg
= counter 1
{ reg
T^1
producer
reg
= reg 1
{ reg
T^2
consumer
reg
= counter 2
{ reg
T^3
consumer
reg
= reg 2
{ reg
T^4
producer
counter = reg
1
{ counter = 6 }
T^5
consumer
counter = reg
2
{ counter = 4 }
Thisshouldbe 5!
Definition of terms
z^
Race Condition^ – Where the order of execution of instructions influences the
result produced
- Important cases for race detection are shared objects
- counters: in the last example
z^
Mutual exclusion^ – only one process at a time can be updating shared objects
z^
Critical section^ – region of code that updates or
uses
shared data
- to provide a consistent view of objects need to make sure an update is not in progress when reading the data - need to provide mutual exclusion for a critical section
Critical Section (cont)
z^
May assume that some instructions are atomic–
typically load, store, and test word instructions z^
Algorithm #1 for two processes–
use a shared variable that is either 0 or 1– when P
= k a process may enter the regionk
repeat (while turn != 0);// critical sectionturn = 1;// non-critical section until false;
repeat (while turn != 1);// critical sectionturn = 0;// non-critical section until false;
- this fails the progress requirement since process 0 not being
in the critical section stops process 1.
Critical Section (Algorithm 2)
z^
Keep an array of flags indicating which processeswant to enter the section
bool flag[2];repeat flag[i] = true;while (flag[j]);// critical sectionflag[i] = false;// non-critical section until false;
z^
Both processescould be here atthe same time This does NOT work either!– possible to have both flags set to 1
Critical Section (many processes)
z^
What if we have several processes?
z^
One option is the Bakery algorithm
bool choosing[n];integer number[n];choosing[i] = true;number[i] = max(number[0],..number[n-1])+1;choosing[i] = false;for j = 0 to n-
while choosing[j];while number[j] != 0 and ((number[j], j) < number[i],i); end// critical sectionnumber[i] = 0
Bakery Algorithm - explained
z^
When a process wants to enter critical section, ittakes a number– however, assigning a unique number to each process is not
possible• it requires a critical section!
- however, to break ties we can used the lowest numbered
process id
z^
Each process waits until its number is the highestone– it can then enter the critical section
z^
provides fairness since each process is served in theorder they requested the critical section
Using Test and Set for Mutual Exclusionrepeat
while test-and-set(lock);// critical sectionlock = false;// non-critical section until false; z^
bounded waiting time version repeat
waiting[i] = true;key = true;while waiting[i] and key
key = test-and-set(lock); waiting[i] = false;// critical sectionj = (i + 1) % nwhile (j != i) and (!waiting[j])
j = (j + 1) % n; if (j == i)
lock = false; else
waiting[j] = false; // non-critical section until false;
Note: no priority based on wait time no process waitingrelease process j
look for a waiting process wait until released or no one busy
Semaphores
z^
getting critical section problem correct is difficult– harder to generalize to other synchronization problems– Alternative is semaphores
z^
semaphores– integer variable– only access is through atomic operations
z^
P (or wait)while s <= 0;s = s - 1;
z^
V (or signal)s = s + 1
z^
Two types of Semaphores– Counting (values range from 0 to n)– Binary (values range from 0 to 1)
Implementing semaphores
z^
Busy waiting implementations
z^
Instead of busy waiting, process can block itself– place process into queue associated with semaphore– state of process switched to waiting state– transfer control to CPU scheduler– process gets restarted when some other process executes a
signal operations
Implementing Semaphores
z^
declarationtype semaphore = record
value: integer = 1;L: FIFO list of process; end; z^
P(S):
S.value = S.value -1if S.value < 0 then {
add this process to S.Lblock;
};
z^
V(S):
S.value = S.value+1if S.value <= 0 then {
remove process P from S.Lwakeup(P);
}
Can be neg, if so, indicateshow many waiting
Bounded waiting!!