Title: Semaphores System O'S' Tools
1Semaphores System / O.S. Tools
- Proposed by Dijkstra in 1968
- BASIC STRUCTURE An integer S with the following
features - It is global sharable among the processes.
- One semaphore S denotes status of one type of
resource in the following way - 1. It is initialized to a value
indicating that the concerned resource is free. - 2. Allocation and/or De-Allocation of the
resource to a requesting process is achieved
through the use of dedicated, atomic O.S.
functions as illustrated later.
2Allowable Semaphore Operations
- i) Associate one semaphore S with each type
of resources . - ii) Initialize S with the current resource
status with which the semaphore is associated. - iii) Wait (S) P(S) from the Dutch word
proberen meaning to Test. used to test whether
the concerned resource is free, if not the
resource is booked/occupied. - iv) Signal(S) V(S) from the Dutch word
verhogen meaning to increment. this is used to
release the resource. - N.B The Operations Wait(S) Signal(S) both will
have to be made concurrently non sharable .
3Binary (Two Valued) Semaphore Functions
- WAIT(Mutex)
- \\ begin Wait
- while (Mutex 0) do no operation /
Resource is Occupied / -
/ Busy Waiting / - / Otherwise Resource is Free Mutex
equals 1 / - Mutex ? 0 / Book Resource /
- \\end Wait
- --------------------------------------------------
---------------------- - SIGNAL (Mutex)
- \\begin Signal
- Mutex ? 1 / Free Resource /
- //end Signal
4Problems Suggested Solutions
- Making the functions Wait ( ) Signal ( )
mutually exclusive. - - Use special Hardware Instructions.
- Ensuring mutual exclusion among processes while
accessing the shared semaphore - - Use Busy Waiting loop before
allowing access to semaphore operations Non
Harmful because of short semaphore occupancy time
. - Avoid Busy Waiting while accessing resource.
- - Associate the Waiting Queue with the
semaphore.
5Ways to Implement Binary Semaphore - 1
- typedef struct
- unsigned short int Value
// Resource Status - // Value 1 ? Resource Free Value 0 ?
Resource Busy - unsigned short int Flag
// Semaphore Status - // Flag 0 ? Semaphore NOT in USE Flag
1 ? In Use - Q_Type WAIT_Q // Queue
of Waiting - // Processes as illustrated
in earlier section - B_SemT // Binary
Semaphore Type - shared B_SemT Mutex // Binary Semaphore
6Ways to Implement Binary Semaphore - 2
- void WAIT (Mutex)
- // begin WAIT
- unsigned short int Key 1 // Ready to
Block Semaphore - while (Key) Swap (Key,Mutex.Flag) // Busy
Wait - // Here Mutex. Flag 0 hence Binary Semaphore
becoming Free - if (Mutex.Value 0 ) // Resource
is NOT free - // begin THEN 1
- SYSCALL ( ENQ (Mutex
.WAIT_Q, Pi)) // Wait in Queue - // This Queue is Exclusively
associated with the Semaphore - Pi . State ? Blocked
- SYSCALL (Scheduler) //
Select new process to RUN - // end THEN 1
- else / Mutex.Value Equals 1/
Mutex.Value 0 // Book Resource - Key 0 Swap (Key , Mutex.Flag)
// Release Semaphore - return
- // end WAIT
7Ways to Implement Binary Semaphore - 3
- void SIGNAL (Mutex)
- // begin SIGNAL executed by a Process while
in RUNNING State - unsigned short int Key 1// Ready to
Block Semaphore - while (Key) Swap (Key,Mutex.Flag) //
Busy Wait - // Here Mutex. Flag 0 hence Binary
Semaphore becoming Free - Mutex.Value 1 // Free Resource
-
- Key 0 Swap (Key , Mutex.Flag) //
Release Semaphore - return
- // end SIGNAL
-
8 Make All Waiting Processes Ready
Part of Peripheral Interrupt Handler
- Almost always follows SIGNAL
- while (NOT IS_EMPTY(S.WAIT_Q)
- // Repeat Till Wait Queue
becomes empty - // begin while 1
- SYSCALL ( DEQ (S .WAIT_Q,
Pi)) // Take out one Process -
// from Wait Q
- SYSCALL ( ENQ (READY,
Pi)) // Put back that Process in -
// Ready Queue - Pi . State ? READY
- //end while 1
9Counting Semaphores ( Originally Proposed )
- BASIC STRUCTURE An integer S with the following
features - It is global sharable among the processes.
- One semaphore S denotes status of one type of
resource in the following way - 1. It is initialized to a ve value
indicating number of existing instances of that
resource e.g. S ? 5 implies there exists 5
instances of the concerned resource. - 2. During the course of allocation of
resource instances to various requesting
processes number of resources ( value of S ) will
go down , ultimately becoming 0 signifying non
availability of the associated resource..
10Counting Semaphore As Originally Proposed
- Wait(S) / P(S)
- // begin
- S ? S - 1 / One Less Resource Instance
/ - while ( S lt 0) do S ? 0
- / No Resource Instance available /
- / Busy waiting /
- // end
- --------------------------------------------------
------------------- - Signal (S) / V(S)
- \\ begin
- S ? S 1 / One More Instance Available
/ - \\ end
-
11Way to Implement Counting Semaphore Main Issues
- Making the functions Wait ( ) Signal ( )
concurrently non sharable. - - Use special Hardware Instructions.
- Ensuring mutual exclusion among processes while
accessing the shared semaphore - - Use Busy Waiting loop before
allowing access to semaphore operations Non
Harmful because of short semaphore occupancy time
. - Avoid Busy Waiting while accessing resource.
- - Associate the Waiting Queue with the
semaphore.
12Ways to Implement Counting Semaphore - 1
- typedef struct
- unsigned short int Count
// Resource Status - // Count gt 0 ? Some Instance Free Count 0
? All Busy - Count lt 0 ? Some Process already Waiting
- unsigned short int Flag
// Semaphore Status - // Flag 0 ? Semaphore NOT in USE Flag
1 ? In Use - Q_Type WAIT_Q // Queue
of Waiting - // Processes as illustrated
in earlier section - Counting_SemT //
Counting Semaphore Type - shared Counting_SemT S // Counting Semaphore
13Ways to Implement Counting Semaphore - 2
- void WAIT (S)
- // begin WAIT
- unsigned short int Key 1 // Ready to
Block Semaphore - while (Key) Swap (Key,S.Flag) // Busy Wait
- // Here S. Flag 0 hence Semaphore becoming
Free - S.Count -- // Decrement Resource
Count , Wants to Book One - if (S.Count lt 0 ) // Last Resource
Instance already used up - // begin THEN 1
- SYSCALL ( ENQ (S .WAIT_Q, Pi))
// Put Process in the - //
Exclusive Wait Q - Pi . State ? Blocked S.Count ?
0 // No Resource is Available - SYSCALL (Scheduler) // Select new
process to RUN - Key 0 Swap (Key, S.Flag) //
Release Semaphore - // end THEN 1
- return
- // end WAIT
14Ways to Implement Counting Semaphore - 3
- void SIGNAL (S)
- // begin SIGNAL Executed by a Process while
in Running State - unsigned short int Key 1// Ready to
Block Semaphore - while (Key) Swap (Key,S.Flag) // Busy
Wait - // Here S. Flag 0 hence Semaphore
becoming Free - S.Count // Free One Resource Instance
- Key 0 Swap (Key, S.Flag) // Release
Semaphore - return
- // end SIGNAL
-
15 Make All Waiting Processes Ready
Part of Peripheral Interrupt Handler
- Follows SIGNAL
- while (NOT IS_EMPTY(S.WAIT_Q)
- // Repeat Till Wait Queue
becomes empty - // begin while 1
- SYSCALL ( DEQ (S .WAIT_Q,
Pi)) // Take out one Process -
// from Wait Q
- SYSCALL ( ENQ (READY,
Pi)) // Put back that Process in -
// Ready Queue - Pi . State ? READY
- //end while 1
16Resource Sharing using Semaphore
- shared Semaphore S / Assuming Counting
Semaphore / - co begin / par begin P1,P2, ,,,,, Pn /
Set of Concurrent Processes / - Pi REPEAT FOREVER / Initiated by System
Call / - WAIT (S) // Wait For Resource
Availability OR Usage . Process GETS Blocked . - // For peripheral
Usage Process ALWAYS gets Blocked . - // For Shared Memory/Consumable Process may
remain in Running State if resource is AVAIL. - // New Process gets
allocated to the CPU . - ltCritical Section Use Resource gt Via
Device Drivers? ( to be explored) -
- / 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 . All WAITING
processes made READY / -
- / Pi , when scheduled to run ,does the
following task FIRST / - SIGNAL (S) / Release Resource.
/ - / Make ALL Waiting Processes Ready to
Enable them to ask for the Now freed Resource/
- co end / par end
17Resource Sharing using Semaphore Key
Observations
- Any type of Resource Request is made by any User
Process by executing a System CALL / Software
Interrupt. - All the Process Algorithm Outline as has been
illustrated is to be executed by each every
process in KERNEL /System Mode as part of the
Software Interrupt / System CALL processing. - As is apparent each Process must execute an
WAIT (S) to check current Resource Availability
status , always waits if resource is NOT
available (for peripheral usage may get blocked
even during resource usage). - As soon as resource usage is over , the
concerned peripheral raises a Hardware Interrupt,
signifying the completion of the concerned
resource usage. In response ALL the processes
waiting for that resource, including the process
last using it , if was waiting, are made READY
, so that the process last using the resource,
whenever scheduled for RUNNING , can release
it by executing SIGNAL (S). - Other WAITING processes are allocated the
resource on issuing fresh request as and when
they start running after being scheduled. - The Deadlock / Lifeness Issue is still NOT
addressed. More elaboration needed for
Consumable Resources.
18 Special Case of Resource Sharing SPOOLING (
Simultaneous Peripheral Operation On Line )
- Used to minimize waiting time of processes for
slow peripheral like printer operations by
creating a virtual peripheral on a relatively
faster one like disk. - While running , a process P makes a reasonable
long printing request via system call / software
interrupt. - The Print Spooler module of the O.S., spools
all such printing jobs by creating print images
on disk ( a part of the file system ??) first
employing DMA then subsequently invokes the
printer driver to transfer these images one by
one to the printer buffer whenever it becomes
available. - So at some point of time after any of the Print
requests from any one of the processes one may
find that while printer is currently printing the
Job A on its own, interrupting the system to draw
its attention , while the O.S. employs the DMA
creates the print image of all requesting jobs on
the disk. Two peripherals operating
simultaneously . - On receiving the Printer Interrupt, the O.S.
copies the next unprinted Image part from the
Disk onto the Printers Buffer area after which
the printing operation is restarted.
19SPOOLING -2( Simultaneous Peripheral Operation
On LineHandling of Post Printing Request )
- The currently running process comes to KERNEL
state. - It saves the current status return address in
system stack. - The printing request is treated as a Disk
Operation Request , hence print spooler is
invoked which starts DMA mode data transfer to
create print image on disk in burst mode. - The requesting process goes to WAIT state.
- On completion of Disk Write / Image Creation of
some printer pages the Disk Controller interrupts
the CPU. - This will invoke the actual printing phase by
calling the printer driver followed by filling of
printer buffer from the already created print
image on disk as and when the printer becomes
free. - After completion of actual printing of its buffer
content, the printer sends a device interrupt to
the CPU. - Printer Interrupt Handler may either stop
printing or alternately may reload the yet to be
printed pages to the Printer Buffer from Disk
followed by restarting the printer .