Title: The Resource Sharing Issues
1The Resource Sharing Issues
- Consider several concurrent processes P1, P2, .
Pn executing on a System that possesses a single
concurrently non sharable resource R. - Sharing Policy
- - Usage can never be inter mixed i .e. any
process Pi that would like to use the resource
must be given exclusive access to the resource R. - - If more than one process wishes to use the
resource R then for any process Pi one has to
adopt the following algorithm(s). -
2Mutual Exclusion Algorithm - 1
- Var free Boolean / Two State Shared
Variable Indicating Resource Status/ - co begin / par begin P1,P2, ,,,,, Pn /
Set of Concurrent Processes / - free TRUE / Resource Status indicates
that the Resource is Free / - Pi REPEAT
- IF (not free) THEN go to WAIT
Queue / Line 1 / - ELSE / Line 2 /
- BEGIN / ELSE Resource
has become free OR is Free / - free FALSE / Reserve
Resource / - ltCritical Section Use Resource gt
- free
TRUE / Release Resource / - Pi passive
- END / ELSE /
- FOREVER
- co end / par end
3Performance Analysis of algorithm - 1
- Each process forced to wait for the resource in a
waiting Queue. - Resource status is indicated in a shared system
variable Free. - Safety hence mutual exclusion among the various
processes is not guaranteed, since the testing of
resource status if (free) and choosing one
among THEN part or ELSE part is not indivisible /
is non atomic.
4Mutual Exclusion Protocol -- IV
- Both P1 and P2 are executing concurrently so
both p1 and p2 may refer to free variable at
the same time, finds it (true and start using it. - Mutual exclusion is violated.
5Mutual Exclusion Protocol -- V
- Solution 1
- Replace shared status variable free by another
integer variable P_turn initializated to integer
values if P_turn I ? process Pi allowed to
executive in its critical section. - For two processes P1 P2 the variables P_turn
will be initialize to either 1 or 2.
6Mutual Exclusion Protocol -- VI
- Co-operating Process
- var P_turn Integer
- begin
- P_Turn 1
- Co begin
- P1 repeat
- while P_turn 1 do no-op / waiting
/ - ltCritical section, Use Rgt
- P_Turn 2 / Release resource /
- P1 passive
- forever
7Mutual Exclusion Protocol -- VI
- P2 repeat
- while P_turn 2 do no-op / waiting
/ - ltCritical section, Use Rgt
- P_Turn 1 / Release resource /
- P1 passive
- forever
- Co end
- Prob 1) Fixed preassigned sequence of P1,
P2, P1, P2, - 2) Busy Waiting.
8Mutual Exclusion Protocol -- VII
- Sharing of Unshareable resource criteria
- Mutual exclusion If process Pi is executing in
its critical section, no other process can
execute in its critical section i.e. the resource
in question can be used by one process at time at
must. - Progress When the resource is requested
simultaneously by several processed and it is not
in use then it must be granted to one of the
competing processes (which are in their remainder
sections ) within a finite time. - Bounded Waiting a) A process should not
consume processing time when it is waiting to
acquire the resource - b) After acquiring the resource a process must
release it within finite time thereby preventing
starvation. - c) No assumption is made about the relative
speeds of the processes, processes may even be
stopped when they are not using any critical
resource.
9Concept of MUTUAL EXCLUSION
10Resource Sharing Policy- Algorithm-IIAnalysis-III
- Test Set Operations
- High level
- while (NOT (Free)) do (no-operation) /
Test / - Free ? FALSE / Set /
- Low Level / Assembly Level Equivalent.
- Statement No. Label Statement
- (1) Loop LOAD
R!, Free - (2) . . . . . CMP R1, 0 Test for
False - (3) . . . . . JZ Loop
Busy wait - (4) . . . . . MVI R2, 0 Set
Segment - (5) . . . . . . STORE Free, R2
11Resource Sharing Policy Algorithm-I
Analysis-3
- Possible Mutual Exclusion Violation
- Process P1 is executing the test segment, finds
Free to be TRUE while testing and is
interrupted before completing set operation i.e.
Before executing statement no (5). - Process P2 executes the test segment, finds
Free to be TRUE while testing and then - Proceeds to set Free ? False , and is
interrupted. - Process P1 resumes, find the value stored in its
Register R1 (status of Free) to be TRUE.
12Resource Allocation Policy Algorithm-4
- Petersons Algorithm
- Shared Var flag Array12 of boolean
- Shared var P_Turn Integer / 1 .. 2 say /
- begin
- flag 1 flag2 ? FALSE
- P_Turn ? ½ / Declears which one will use first
/ - parbegin
- Process Pi
- / Locals /
- Var j .. .. n 1
- Var key Bolean
13Contd..
- repeat
- waiting i ? TRUE / Waiting for Resource
/ - key ? TRUE / Signals Intension /
- while (waitingi ) and key do
- key Test_Set (Lock)
- / Repeatedly check it Resource is free /
-
14Process Allocation Algorithm (contd)
- Function Test_Set (Var I Boolean) Boolean
- begin
- Test_Set ? I
- I ? TRUE
- end
- Hence waiting process will come out of waiting
loop if resource is Free Key ? FALSE, come out
of the wait Loop - Waitingi ? FALSE / No more waiting /
- ltCritical Section gt
15Process Allocation Algorithm (Contd.)
- Ensuring Process Bounded Waiting
- j (i1) mod n
- while ( j i) and (not wait j) do j (j1)
mod n - / searches the waiting array in a cycle
order i1 n - to find the first process with waiting
j TRUE i.e. - waiting for the Resource /
- / Selection of any process requesting for
Resource is done within n-1 turns / - if ( j i) / No process founds /
- then
- Lock False / Make resource Free /
- else / some process found /
- Waitingj False / Allow it capture
resource / - ltRemainder section gt
- Forever
16Performance Analysis
- Mutual Exclusion Guaranteed since the useage
of Test-set functions ensures that - Process Creation
- A process on exiting the critical section either
makes - the resources free (Lock ? False) allowing other
process to use the resource as soon as it goes to
wait state. - Bounded Waiting Criteria
- On exiting the critical section it looks for any
waiting processes and ends its waiting by
Waitingi False thereby ensuring as soon as
the resource becomes free (Key ? Test_Set (Lock))
sets - Key ? False) the process in waiting can
acquire the resource. Busy waiting is still
involved.
17Problems associated with current / presented
- Non uniform ways to achieve mutual exclusion,
like some cases software a few other cases
employing Hardware support. - Busy waiting is unavoidable
18Semaphores Synchronization Tool
- Proposed by Dijkstra in 1968
- BASIC STRUCTURE
- An integer S that processes the following
characteristics - It is global sharable among the processes
- One semaphores denotes status of one resource
and/or variable. - Allowable operations (Atomic only).
- Test wait (P from the ditch word probeven
meaning to Test). - Increment / Single C V from the ditch word
verhugen to increments.
19SEMAPHORES Contd..
- P(s) / Wait(s)
- begin
- while S ? 0 do no operation / wait /
- S ? S - 1
- end
- V(s) / signal (s)
- begin
- S ? S 1
- end
-
20BINARY SEMAPHORE
- WAITS(S)
- begin
- while (s 1) do no operation
- S ? 0
- end
- SIGNALS (S)
- begin
- S ? 1
- end
21Implementing Semaphore
- Using special Instruction Test-Set
- Counting Semaphore
- type Semaphore record
- value Integer
- Mutex Boolean / Mutual Exclusion of
Semaphore manipulation / - Hold Boolean / Process waiting status /
- Shared Var S Semaphore
- Initialization
- S.MUTEX FALSE S.Value ltInitial Valuegt
- S.Hold True
22Implementing Counting Symaphore
- Wait (S)
- begin
- while (Test_Set (Mutex))
- / Critical section to manipulated semaphore
parts / - S.Value ? S.Value 1
- if (S.Value lt 0) then
- begin
- S.Mutex FALSE
- WHILE (Test.set (Hold)) / Wait for
Semaphore / - end
- else
- S_MUTEX FALSE / Release Semaphore /
23Implementing Semaphore III (Contd..)
- Signal (S)
- begin
- while (Test_Set (Mutex))
- S.Value ? S.Value 1
- if (S.Value ? 0) then
- begin
- while (!S.hold) / To takle the case when
signal functions occur consecutively, before
any wait ( ) / A process P believes that it is
blocked in wait but signal encounter S.Hold
True / - S.hold FALSE,
- end
- S.Mulex FASLE
- end
24Implementing Binary Semaphore
- type semaphore record
- value 0 . . . 1
- Mutes Boolean
- Hold Boolean
- end
- shared var B S Semaphore
- wait ( B-S)
- begin
- while (Test_Set (B_S.Mutux))
- if (B-S.value 0) then
- begin
- B_S.Value ? 1
- B_S.Mutux ? FALSE
- While (Test-set (B-S.Mutes))
- if (B.S.Value 0) then
- begin
- B_S.Value ? 1
- B-S.Mutex ? FALSE
25Contd..
- While (Test-Set (Hold)
- end
- else
- B.S.Mutux FALSE
- end
- Signal (B_S)
- begin
- while (Test_set (B_S.Mutux)
- B_S.Value ? 1
- while (! B_S.Hold)
- B_S.Hold ? FALSE
- B_S.MUTEX ? FALSE
- end
-
26Problems
- Busy Waiting on S.Hold / B.S.Hold Semaphore
inside each operation - Alternative way Instead of waiting the
following things can be done - Put itself in a waiting queue associated with the
semaphore. - Block itself
- Invokes dispatcher / Scheduler for selecting a
new Process from Ready Queue.
27Re definition of Semaphore
- Type Semaphore record
- value Integer
- Mutux Boolean
- Hold Boolean
- L List of Processes / Queue ptr
- end
- Shared Var S Semaphore
- Waits (S)
- begin
- while (Test_Set (S.Mutes))
- S.Value ? S.Value 1
- if (S.Value lt 0) / several process are waiting
/ - then while (Test.Set (S.Hold) begin
- Enq (Pi, S.L) / Put in the Queue /
-
-
28Contd
- BLOCK (Pi) / Blodk it /
- S.Mutux ? FALSE
- Yield ( , Seceduler)
- end
- else
- S.Mutex FALSE
- Signals (S)
- begin
- while (Test_Set (S.Mutux))
- S.Value ? S.Value 1
- if (S.Value ? 0) then
- while (!S.Hold)
29- Begin
- DEQ (Pi, S.L)
- Wakeup(Pi) / Put Pi in Ready Queue /
- S.Hold ? FALSE
- end
- S.Mutex ? FALSE
- end
- Redefination of Binary Semaphore
- Const n ltNumber of Proceses gt
- Share Var S Semaphore
- begin
- S ? 1
- parbegin
- Procedure Pi
- repeat
- wait (S)
- ltCritical Sectiongt
- Signals (S)
- forever
30Multi Process Case
- Shared Var Waiting Array 0 . (n-1) of
Boolean - Shared Var S Semaphore
- begin
- S ? 1
- parbegin
- Process Pi
- Var j 0 n-1
- repeat
- WaitingI ? TRUE
- Wait (S)
- Waiting I ? FALSE
31Contd..
- ltCritical Sectiongt
- j ? (i 1) mod n
- while (j ? i ) and (not waiting j) do
- j ? (j1) mod n
- if ( j i )
- then
- signal (S)
- else
- waiting j ? FALSE
- forever
32(No Transcript)