Unit2 1
Unit2 1
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer process
item nextProduced;
while (1) {
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Consumer process
item nextConsumed;
while (1) {
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
The statements
counter++;
counter--;
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 – 1
counter = register2
int turn;
Boolean flag[2];
Process Pi
do {
while (TestAndSet(lock)) ;
critical section
lock = false;
remainder section
}
Operating System Concepts 7.15 Silberschatz, Galvin and Gagne 2002
Mutual Exclusion with Swap
Atomically swaps two variables.
void Swap(boolean &a, boolean &b) {
boolean temp = a;
a = b;
b = temp;
}
Process Pi
do {
key = true;
while (key == true)
Swap(lock,key);
critical section
lock = false;
remainder section
}
Wait(s)
{ while(s<=0); no-operation
s--;
}
Signal(s)
{
s++; }
Shared data:
semaphore mutex; //initially mutex = 1
Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
S1; P1
Signal(sync);
Wait(sync); P2
S2;
Operating System Concepts 7.19 Silberschatz, Galvin and Gagne 2002
Semaphore Implementation
signal(S):
S.value++;
if (S.value > 0) {
remove a
process P from S.L;
wakeup(P);
}
Dining-Philosophers Problem
Shared data
Initially:
do { do {
… wait(full);
wait(empty);
wait(mutex); wait(mutex);
…
add next item to …
remove an
item
uffer from
… buffer
signal(mutex);
…
signal(full);
signal(mutex);
} while (1);
Operating System Concepts 7.26 signal(empty);…
Silberschatz, Galvin and Gagne 2002
Readers-Writers Problem
Initially
wait(wrt);
…
writing is performed
…
signal(wrt);
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Operating System Concepts 7.29 Silberschatz, Galvin and Gagne 2002
Dining-Philosophers Problem
Consider 5 philosophers
spend their time in living
and thinking. When a
philosopher thinks, he doe
not interact with anybody,
but when he feels hungry,
he takes both the
chopsticks and starts
eating. He cannot picku
the chopsticks if they are
in the data
Shared hands of his
neighbours.
semaphore chopstick[5];
Initially all values are 1
Process A: region V1 do
region V1 do begin
begin { Do other stuff. }
{ Do some stuff. } end;
end;
region V2 do
begin
{ Do more stuff. }
end;
Here process A can be executing inside its V2 region while process
B is executing inside its V1 region, but if they both want to execute
inside their respective V1 regions only one will be permitted to
proceed. Each shared variable (V1 and V2 above) has a queue
associated with it. Once one process is executing code inside a
region tagged with a shared variable, any other processes that
attempt to enter a region tagged with the same variable are
blocked
Operating Systemand put in the queue. 7.33
Concepts Silberschatz, Galvin and Gagne 2002
Conditional Critical Regions Critical regions :
end;
where B is a boolean expression (usually B will refer to
v). Conditional critical regions work as follows: 1. A
process wanting to enter a region for v must obtain the
mutex lock. If it cannot, then it is queued. 2. Once the
lock is obtained the boolean expression B is tested. If B
evaluates to true then the process proceeds, otherwise it
Operating System Concepts 7.34 Silberschatz, Galvin and Gagne 2002
Example – Bounded Buffer
Shared data:
struct buffer {
int pool[n];
int count, in, out;
}
monitor monitor-name
{
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
Operating System Concepts
} 7.38 Silberschatz, Galvin and Gagne 2002
Monitors