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 of R can never be inter mixed i
.e. any process Pi that would like to use the
resource R 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 some specific algorithm(s). -
2Resource Sharing Towards Enforcing Mutual
Exclusion - 3
- A. Hardware means Several options exist out of
which the obvious ones are illustrated below - A.1 Disabling / Enabling Interrupts.
- A.2 Usage of specialized Hardware
Instructions - ( Normally privileged ones ) .
-
3 Resource Sharing Towards Enforcing of Mutual
Exclusion-4
- B. Software means This is the oldest option
which had evolved through several stages that
ideally aims to take care of safety , lifeness
as well as fairness issues . The most suitable
ones adopts the following techniques - - Separate sets of variables employed to
represent - I) Resource Quantity / Instances.
- ii) Process Identifiers.
- iii) Resource Allocation Status.
- - Separate ways to address Mutual
Exclusion (Safety) , - Deadlock ( Lifeness ) Bounded Waiting (
Fairness ) issues . -
4 Resource Sharing Towards Enforcing of Mutual
Exclusion-5
- C. System / O.S. Support The ideal option
which has exploited the current state of the art
facilities as provided by both Hardware as well
as Software. With ages this has evolved as the
most acceptable solution that ideally take care
of safety , Lifeness as well as Fairness issues
. -
5 Resource SharingThe Hardware Means - 1
- Pi REPEAT FOREVER
- DISABLE INTERRUPTS ( ) /
Disabling Interrupt, System Call / - IF (not free) THEN go to WAIT Queue /
Process BLOCKED Line 1 / - ELSE BEGIN / ELSE Resource
has become free OR is Free Line 2/ - free FALSE 0 / Reserve
Resource Line 3/ - ENABLE INTERRUPTS ( ) / Enabling
Interrupt System Call / - ltCritical Section Use Resource gt / Line
4 / - / Process in Blocked State for
Peripheral Usage comes out of Blocked State
through H/W Interrupt OR Process in Running
State for Shared Memory / Consumable Resource
Usage can either Voluntarily Release or is Timed
Out / - / Pi must be in Running State to do the
following tasks / - DISABLE INTERRUPTS ( ) / Disabling
Interrupt, System Call / - free TRUE
1 / Release Resource Line 5/ - ENABLE INTERRUPTS ( ) / Enabling Interrupt
System Call / -
- END / ELSE /
- / Subsequent Resource allocation is made as soon
as the Currently Running Process starts using it
or alternately requests for it/ -
6 DISABLING ENABLING INTERRUPTThe Possible
Consequences- 1
- Disables ALL types of Interrupts before testing
resource status hence effectively stopping all
types of I/O Operations Enabling these only
after setting status . - The period of I/O operations may stretch too
long depending on the amount of time a process
spends in the wait state i.e. process dependent
. - Failure of any process within any
DISABLE_INTERRUPT ( ) - ENABLE_INTERRUPT ( ) pairing may cause
permanent blocking of - ALL interrupts. This effectively takes out
ALL control recovery - mechanism available.
- Comment DISABLING, ENABLING INTERRUPT , may
cause - permanent damage in system management .
System Speed is - dictated by the slowest peripheral.
7 Resource Sharing The Hardware Means - 2
- Availability of special assembly / machine level
instructions Any one of the following 2 (two)
instructions are widely used - a) TEST-SET Instruction TestSet ltMemory
Location gt -
- e.g. TestSet S
-
- b) SWAP Instruction SWAP ltMemory Location
Ptrgt,ltGPRgt - e.g. SWAP S, ri
-
8 Resource Sharing The TestSet Instruction
- TestSet ltMemory Locationgt e.g. TestSet S
- Equivalent Function
- unsigned short int TestSet (unsigned short int
S) - / Tests Sets / reverses the resource
Status accordingly / - // begin TestSet
- if ( S 0)
- S 1
- else / S 1/
- S 0
- return (S)
- // end TestSet
-
9Implementing Mutual Exclusion (Safety criteria)
using TestSet Instruction
- shared unsigned short int Lock 0 / Resource
is FREE / - co begin / par begin P1,P2, ,,,,, Pn /
Set of Concurrent Processes / - Pi REPEAT FOREVER
- IF (NOT TestSet ( Lock) ) ENQ ( Pi ,
WAIT Queue) / Resource is Not Free WAIT/ - / Resource gets LOCKED , if it was
not Free Process gets BLOCKED / - ELSE / TestSet (Lock)
Resource is FREE / - BEGIN / ELSE Resource
has become free OR is Free Line 2/ - ltCritical Section Use Resource gt / Line
3 / - / Process in Blocked State for Peripheral
Usage comes out through H/W Interrupt OR
Process in Running State for Shared Memory /
Consumable Resource Usage can either
Voluntarily Release or is Timed Out / - / Pi must be in Running State to do the
following tasks / - TestSet (Lock)
/ Release Resource Line 4 / - END / ELSE /
- co end / par end
- / Subsequent Resource allocation is made as soon
as the Currently Running Process starts using it
or alternately requests for it/
10Resource Sharing The SWAP Instruction
- SWAP ltMemory Locationgt ,GPR e.g. SWAP S, ri
- Equivalent Function
- void SWAP (unsigned short int A, B)
- //begin SWAP
- unsigned short int Temp
- Temp A
- A B
- B Temp
- return
- //end SWAP
11Implementing Mutual Exclusion (Safety criteria)
using SWAP Instruction
- shared unsigned short int free 1 / Resource
is FREE / - co begin / par begin P1,P2, ,,,,, Pn /
Set of Concurrent Processes / - Pi REPEAT FOREVER
- ri? 0 SWAP free , ri / Bring
in Resource Status , LOCK Resource / - IF (ri 0 ) THEN ENQ ( Pi , WAIT
Queue) / Resource is Not Free WAIT in Q/ - ELSE / ri 1 /
- BEGIN / ELSE Resource
has become free OR is Free Line 2/ - ltCritical Section Use Resource gt / Line
3 / - / Process in Blocked State for Peripheral
Usage comes out through H/W Interrupt OR
Process in Running State for Shared Memory /
Consumable Resource Usage can either
Voluntarily Release or is Timed Out / - / Pi must be in Running State to do the
following tasks / - SWAP free , ri
/ ri was 1 hence it will Release Resource
Line 4 / - END / ELSE /
- co end / par end
- / Subsequent Resource allocation is made as soon
as the Currently Running Process starts using it
or alternately requests for it/
12 Resource Sharing Implementing Mutual Exclusion
(Safety) as well as Bounded Waiting
(Fairness)
- Usage of special assembly instructions like
TestSet Swap helps to implement atomicity in
testing as well as setting the resource status. - In order to avoid busy waiting in case of the
resource being occupied, the concerned process is
put in a Wait queue . - Fairness / Bounded Waiting is achieved by
scheduling one among the waiting processes to RUN
state where it can use the freed resource. - This Wait Queue is a shared Data Structure among
the Waiting Processes, any type of access to
this WAIT Queue must also be concurrently non
sharable i.e. must be made mutually exclusive.
13 Resources Wait Queue Structure Access
Mechanism - 1
- Each WAIT Queue happens to be a queue of
Process Control Block PCB pointers of all those
processes that has found all instances of the
requested resource to be occupied and hence
forced to WAIT for some instance of the resource
to be released. - At one point of time only one process can be
enqueued provided the Wait Queue is NOT full OR
alternately only one process can be dequeued
provided the Wait Queue is NOT empty. - One may use special instruction like SWAP to
achieve atomicity while accessing the Wait Queue
but if the Queue is being accessed by some other
process at that time then the current process
must loop in busy waiting mode for entry into the
WAIT queue. No more Queuing for waiting to get an
entry in the WAIT queue. - Size of the WAIT Queue happens to be finite say
max. N ( a ve non zero integer) signifying max.
N number of processes can afford to wait for the
resource.
14 Resources Wait Queue The Data Structure
- define N // lt Queue Size gt
- typedef struct lt gt PCBType // Process
Control Block - typedef PCBType Process_PTr_Type // Process
Pointers - typedef Process_Ptr_Type Wait_List_TypeN //
Array of -
// Waiting Processes - typedef struct // begin Queue Structure Type
- unsigned short int Lock
// Wait Queue Status - Wait_List_Type Wait_ List
// Waiting List - unsigned short int
Rear,Front // Queue Pointers - int Count // Number of
Waiting processes - Q_Type
- SHARED Q_Type WAIT_Q // Shared Variable
15Resources Wait Queue Accessing Functions - 1
- unsigned short int Is_Empty_Q (Q_Type WAIT_Q)
- // begin Is_Empty_Q
- unsigned short int Value
- if (WAIT_Q.Count 0) Value 1
- else Value 0
- return (Value)
- // end Is_Empty_Q
16 Resources Wait Queue Accessing Functions - 2
- unsigned short int Is_Full_Q (Q_Type WAIT_Q)
- // begin Is_Full_Q
- unsigned short int Value
- if (WAIT_Q.Count N ) Value 1
- else Value 0
- return (Value)
- // end Is_Full_Q
17 Resources Wait Queue Accessing Functions - 3
- void ENQ ( Q_Type WAIT_Q, Process_Ptr_Type Pi)
- // Enqueuing Function
- // begin ENQ
- Key_W ? 1 // Set Lock / Block
Status - Swap ( WAIT_Q.Lock, Key_W) //
Test Lock Out Wait Queue - while (Key_W) do Swap ( WAIT_Q.Lock
, Key_W) //Busy Wait - if ( NOT Is_Full_Q (WAIT_Q)
) // Here Key_W ? 0 - WAIT_Q.Wait_List
WAIT_Q. Rear Pi - WAIT_Q.Count
// Increment Waiting Proc. Count - else // begin else 1
- printf ( Too Many
Processes Waiting \n) - Abort (Pi) // System
Call //end else 1 - Swap ( WAIT_Q_Lock, Key_W) //
Release Queue - return // end ENQ
-
-
18Resources Wait Queue Accessing Functions - 4
- void DEQ ( Q_Type WAIT_Q, Process_Ptr_Type Pi)
- // De_QUEUE Function (Mainly Not Used)
- // begin DEQ
- Key_W ? 1 // Set Lock Out
Status - Swap ( WAIT_Q.Lock , Key_W) //
Test Lock out Queue - while (Key_W) do Swap ( WAIT_Q.Lock, Key_W)
//Busy Wait - if ( NOT Is_Empty_Q (WAIT_Q)
) // Here Key_W ? 0 - // begin then 1
- Pj ? WAIT_Q.Wait_List WAIT_Q.Front //
Take out a Process - WAIT_Q.Count - - //
Decrement Waiting Proc. Count - // end then 1
- Swap ( WAIT_Q.Lock, Key_W) //
Release Queue - return //end DEQ
-
-
-
19 Resource Sharing Satisfying Non Deadlock /
Lifeness Criteria
- Consider the following case
- There exists two concurrent processes P1 P2.
- There exists two concurrently non sharable
resources R1 R2. - Each of the two processes P1 P2 require both
the resources R1 R2. - Initially both the concurrently non sharable
resources R1 R2 were free. - The process P1 occupies resource R1 while the
process P2 occupies resource R2 concurrently
with absolute safety.. - After sometime the process P1 requests for
resource R2 while occupying resource R1 which
cannot be granted . - After sometime the process P2 requests for
resource R1 while occupying resource R2 which
also cannot be granted . - NO PROGRESS CAN BE MADE , DEADLOCK.
-
20 Resource sharing among Several Processes
Software Means of Allocation
- Petersons Algorithm ( N processes case)
- define N ltValuegt / Number of Processes /
- shared unsigned short int Lock / Resource
Status / - shared unsigned short int Lock_W / Waiting
List Status / - shared unsigned short int Waiting N /
Waiting Processes List / - void main
- // begin main
- unsigned short int I , J
- Lock 0 / Free the Resource /
- for ( I 0 I ltN I ) Waiting I
0 / No Waiting Process / - cobegin / parbegin ( P0,P1, . , PN)
- // end main
-
21 Petersons Algorithm Contd.
- // Process Pi
- void P ( int I)
- // begin Pi
- unsigned short int j / 1 .. .. N
1 / - unsigned short int Key
- repeat
- Key 1 / Ready to Block Shared
Waiting Array / - swap (Lock_W, Key) / Block
Shared Waiting Array / - while ( Key) swap (Lock_W, Key)
/ Waiting to record Request/ - Waiting I 1 / Waiting for
resource / - swap (Lock_W, Key) / Release
Waiting List / -
22 Petersons Algorithm Contd.
- // Process Pi
- Key 1 / Ready to Block Resource /
- swap (Lock, Key) / Block resource for
testing / -
- if ( Key ! 0) // Resource is busy
- // begin THEN 1
- Block (P (I) ) // System Call
- CALL Scheduler // Pick up New
Process - // end THEN 1
23 Petersons Algorithm Contd.
- else // Key 0 Resource is FREE
- // begin ELSE 1
- Waiting I 0 // No more
Waiting - / Critical Section /
- Use Resource
- / Search for other Waiting
Processes in a Cyclic Fashion / - J (I1)N
- Key 1 / Ready to Block Shared
Waiting List / - swap (Lock_W, Key) / Block
Shared Waiting List / - while ( Key) swap (Lock_W, Key) /
Waiting to search Next Waiting/ - while ((J ! I) (Waiting J
!1)) - J (J1) N Key 0
swap (Lock_W, Key) / Free List / -
24 Petersons Algorithm Contd.
- // Process Pi (Contd.)
- else // Key 0 Resource is FREE
- if ( J I) / No other Waiting
process Found / - Swap ( Lock, Key ) //
Free Resource - else // other Waiting Process Found
- // begin ELSE 2
- Swap ( Lock, Key) // Free
Resource - Waiting J 0 // No
more Waiting - Allocate Resource
- Key1 Swap ( Lock, Key)
// Block Resource - // end ELSE 2
- // end ELSE 1
- forever
25Petersons Algorithm /Performance Analysis
- Mutual Exclusion Guaranteed since the usage
of SWAP ensures that . - No busy waiting.
- Deadlock / Lifeness NOT addressed.
- Progress Criteria
- A process on exiting the critical section always
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 in the WAIT list , in a round
robin fashion and and picks up the front end
process Pj by setting Waitingj 0 thereby
ensuring as soon as the resource becomes free the
first process in waiting can acquire the
resource.