SlideShare a Scribd company logo
4
Most read
5
Most read
8
Most read
Operating System 24
Mutex Locks and Semaphores
Prof Neeraj Bhargava
Vaibhav Khanna
Department of Computer Science
School of Engineering and Systems Sciences
Maharshi Dayanand Saraswati University Ajmer
Solution to Critical-Section Problem
1. Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections
2. Progress - If no process is executing in its critical section and
there exist some processes that wish to enter their critical
section, then the selection of the processes that will enter
the critical section next cannot be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of
times that other processes are allowed to enter their critical
sections after a process has made a request to enter its
critical section and before that request is granted
 Assume that each process executes at a nonzero speed
 No assumption concerning relative speed of the n processes
Critical-Section Handling in OS
Two approaches depending on if
kernel is preemptive or non-
preemptive
– Preemptive– allows preemption of
process when running in kernel mode
– Non-preemptive – runs until exits kernel
mode, blocks, or voluntarily yields CPU
• Essentially free of race conditions in kernel
mode
Mutex Locks
Previous solutions are complicated and generally
inaccessible to application programmers
OS designers build software tools to solve critical
section problem
Simplest is mutex lock
Protect a critical section by first acquire() a lock
then release() the lock
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Usually implemented via hardware atomic instructions
But this solution requires busy waiting
This lock therefore called a spinlock
acquire() and release()
• acquire() {
while (!available)
; /* busy wait */
available = false;;
}
• release() {
available = true;
}
• do {
acquire lock
critical section
release lock
remainder section
} while (true);
Semaphore
• Synchronization tool that provides more sophisticated ways (than Mutex locks) for
process to synchronize their activities.
• Semaphore S – integer variable
• Can only be accessed via two indivisible (atomic) operations
– wait() and signal()
• Originally called P() and V()
• Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
• Definition of the signal() operation
signal(S) {
S++;
}
Semaphore Usage
• Counting semaphore – integer value can range over an unrestricted domain
• Binary semaphore – integer value can range only between 0 and 1
– Same as a mutex lock
• Can solve various synchronization problems
• Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
• Can implement a counting semaphore S as a binary semaphore
Semaphore Implementation
• Must guarantee that no two processes can execute the
wait() and signal() on the same semaphore at the same
time
• Thus, the implementation becomes the critical section
problem where the wait and signal code are placed in the
critical section
– Could now have busy waiting in critical section
implementation
• But implementation code is short
• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical
sections and therefore this is not a good solution
Semaphore Implementation with no Busy waiting
• With each semaphore there is an associated waiting
queue
• Each entry in a waiting queue has two data items:
– value (of type integer)
– pointer to next record in the list
• Two operations:
– block – place the process invoking the operation on the
appropriate waiting queue
– wakeup – remove one of processes in the waiting queue
and place it in the ready queue
• typedef struct{
int value;
struct process *list;
} semaphore;
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Deadlock and Starvation
• Deadlock – two or more processes are waiting
indefinitely for an event that can be caused by only
one of the waiting processes
• Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
• Starvation – indefinite blocking
– A process may never be removed from the semaphore queue in which it is suspended
• Priority Inversion – Scheduling problem when
lower-priority process holds a lock needed by
higher-priority process
– Solved via priority-inheritance protocol
Assignment
• Explain the concept of Mutex Locks
• What is a Semaphore. Explain Semaphore
Implementation.

More Related Content

What's hot (20)

PPT
CPU Scheduling Algorithms
Shubhashish Punj
 
PPTX
Real time Operating System
Tech_MX
 
PPTX
Real time Scheduling in Operating System for Msc CS
Thanveen
 
PPT
Disk scheduling
NEERAJ BAGHEL
 
PDF
Distributed Systems Naming
Ahmed Magdy Ezzeldin, MSc.
 
PPTX
RPC: Remote procedure call
Sunita Sahu
 
PPT
Chapter 4 a interprocess communication
AbDul ThaYyal
 
PDF
Linux Memory Management
Anil Kumar Pugalia
 
PPT
Clock synchronization in distributed system
Sunita Sahu
 
PPTX
Distributed operating system
udaya khanal
 
PPTX
Operating system critical section
Harshana Madusanka Jayamaha
 
PPTX
Page replacement algorithms
sangrampatil81
 
PPT
Naming in Distributed Systems
Nandakumar P
 
PPTX
Context switching
DarakhshanNayyab
 
PDF
System calls
AfshanKhan51
 
PPTX
Memory management
Muhammad Fayyaz
 
PPT
Chapter 3 - Processes
Wayne Jones Jnr
 
PDF
Memory management
Rajni Sirohi
 
PPTX
6.distributed shared memory
Gd Goenka University
 
PPTX
Distributed and clustered systems
V.V.Vanniaperumal College for Women
 
CPU Scheduling Algorithms
Shubhashish Punj
 
Real time Operating System
Tech_MX
 
Real time Scheduling in Operating System for Msc CS
Thanveen
 
Disk scheduling
NEERAJ BAGHEL
 
Distributed Systems Naming
Ahmed Magdy Ezzeldin, MSc.
 
RPC: Remote procedure call
Sunita Sahu
 
Chapter 4 a interprocess communication
AbDul ThaYyal
 
Linux Memory Management
Anil Kumar Pugalia
 
Clock synchronization in distributed system
Sunita Sahu
 
Distributed operating system
udaya khanal
 
Operating system critical section
Harshana Madusanka Jayamaha
 
Page replacement algorithms
sangrampatil81
 
Naming in Distributed Systems
Nandakumar P
 
Context switching
DarakhshanNayyab
 
System calls
AfshanKhan51
 
Memory management
Muhammad Fayyaz
 
Chapter 3 - Processes
Wayne Jones Jnr
 
Memory management
Rajni Sirohi
 
6.distributed shared memory
Gd Goenka University
 
Distributed and clustered systems
V.V.Vanniaperumal College for Women
 

Similar to Operating system 24 mutex locks and semaphores (20)

PPT
Lecture18-19 (1).ppt
ssuserf67e3a
 
PPTX
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
PPTX
Semophores and it's types
Nishant Joshi
 
PDF
OS Process synchronization Unit3 synchronization
subhamchy2005
 
PDF
Ch5 process synchronization
Welly Dian Astika
 
PPTX
Operating system 27 semaphores
Vaibhav Khanna
 
PPT
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
ssuser51e3761
 
PPT
Inter process communication
Gift Kaliza
 
PPTX
Interprocess Communication important topic in iOS .pptx
thetale999
 
PPT
Process Synchronization -1.ppt
jayverma27
 
PPTX
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
PDF
Process coordination
Sweta Kumari Barnwal
 
PPTX
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
PDF
CH05.pdf
ImranKhan880955
 
PPTX
PPT 123456617829291912192891semaphores.pptx
kritisrivastav497
 
PPT
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
PPT
Mca ii os u-2 process management & communication
Rai University
 
PPT
Ipc feb4
yashnand
 
Lecture18-19 (1).ppt
ssuserf67e3a
 
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
Semophores and it's types
Nishant Joshi
 
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Ch5 process synchronization
Welly Dian Astika
 
Operating system 27 semaphores
Vaibhav Khanna
 
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
ssuser51e3761
 
Inter process communication
Gift Kaliza
 
Interprocess Communication important topic in iOS .pptx
thetale999
 
Process Synchronization -1.ppt
jayverma27
 
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
Process coordination
Sweta Kumari Barnwal
 
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
CH05.pdf
ImranKhan880955
 
PPT 123456617829291912192891semaphores.pptx
kritisrivastav497
 
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Mca ii os u-2 process management & communication
Rai University
 
Ipc feb4
yashnand
 
Ad

More from Vaibhav Khanna (20)

PPTX
Information and network security 47 authentication applications
Vaibhav Khanna
 
PPTX
Information and network security 46 digital signature algorithm
Vaibhav Khanna
 
PPTX
Information and network security 45 digital signature standard
Vaibhav Khanna
 
PPTX
Information and network security 44 direct digital signatures
Vaibhav Khanna
 
PPTX
Information and network security 43 digital signatures
Vaibhav Khanna
 
PPTX
Information and network security 42 security of message authentication code
Vaibhav Khanna
 
PPTX
Information and network security 41 message authentication code
Vaibhav Khanna
 
PPTX
Information and network security 40 sha3 secure hash algorithm
Vaibhav Khanna
 
PPTX
Information and network security 39 secure hash algorithm
Vaibhav Khanna
 
PPTX
Information and network security 38 birthday attacks and security of hash fun...
Vaibhav Khanna
 
PPTX
Information and network security 37 hash functions and message authentication
Vaibhav Khanna
 
PPTX
Information and network security 35 the chinese remainder theorem
Vaibhav Khanna
 
PPTX
Information and network security 34 primality
Vaibhav Khanna
 
PPTX
Information and network security 33 rsa algorithm
Vaibhav Khanna
 
PPTX
Information and network security 32 principles of public key cryptosystems
Vaibhav Khanna
 
PPTX
Information and network security 31 public key cryptography
Vaibhav Khanna
 
PPTX
Information and network security 30 random numbers
Vaibhav Khanna
 
PPTX
Information and network security 29 international data encryption algorithm
Vaibhav Khanna
 
PPTX
Information and network security 28 blowfish
Vaibhav Khanna
 
PPTX
Information and network security 27 triple des
Vaibhav Khanna
 
Information and network security 47 authentication applications
Vaibhav Khanna
 
Information and network security 46 digital signature algorithm
Vaibhav Khanna
 
Information and network security 45 digital signature standard
Vaibhav Khanna
 
Information and network security 44 direct digital signatures
Vaibhav Khanna
 
Information and network security 43 digital signatures
Vaibhav Khanna
 
Information and network security 42 security of message authentication code
Vaibhav Khanna
 
Information and network security 41 message authentication code
Vaibhav Khanna
 
Information and network security 40 sha3 secure hash algorithm
Vaibhav Khanna
 
Information and network security 39 secure hash algorithm
Vaibhav Khanna
 
Information and network security 38 birthday attacks and security of hash fun...
Vaibhav Khanna
 
Information and network security 37 hash functions and message authentication
Vaibhav Khanna
 
Information and network security 35 the chinese remainder theorem
Vaibhav Khanna
 
Information and network security 34 primality
Vaibhav Khanna
 
Information and network security 33 rsa algorithm
Vaibhav Khanna
 
Information and network security 32 principles of public key cryptosystems
Vaibhav Khanna
 
Information and network security 31 public key cryptography
Vaibhav Khanna
 
Information and network security 30 random numbers
Vaibhav Khanna
 
Information and network security 29 international data encryption algorithm
Vaibhav Khanna
 
Information and network security 28 blowfish
Vaibhav Khanna
 
Information and network security 27 triple des
Vaibhav Khanna
 
Ad

Recently uploaded (20)

PPTX
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
PPTX
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
PPTX
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PDF
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
PDF
Protecting the Digital World Cyber Securit
dnthakkar16
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
PDF
What companies do with Pharo (ESUG 2025)
ESUG
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
ASSIGNMENT_1[1][1][1][1][1] (1) variables.pptx
kr2589474
 
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Presentation about variables and constant.pptx
kr2589474
 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
Protecting the Digital World Cyber Securit
dnthakkar16
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
What companies do with Pharo (ESUG 2025)
ESUG
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
Supabase Meetup: Build in a weekend, scale to millions
Carlo Gilmar Padilla Santana
 

Operating system 24 mutex locks and semaphores

  • 1. Operating System 24 Mutex Locks and Semaphores Prof Neeraj Bhargava Vaibhav Khanna Department of Computer Science School of Engineering and Systems Sciences Maharshi Dayanand Saraswati University Ajmer
  • 2. Solution to Critical-Section Problem 1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted  Assume that each process executes at a nonzero speed  No assumption concerning relative speed of the n processes
  • 3. Critical-Section Handling in OS Two approaches depending on if kernel is preemptive or non- preemptive – Preemptive– allows preemption of process when running in kernel mode – Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU • Essentially free of race conditions in kernel mode
  • 4. Mutex Locks Previous solutions are complicated and generally inaccessible to application programmers OS designers build software tools to solve critical section problem Simplest is mutex lock Protect a critical section by first acquire() a lock then release() the lock Boolean variable indicating if lock is available or not Calls to acquire() and release() must be atomic Usually implemented via hardware atomic instructions But this solution requires busy waiting This lock therefore called a spinlock
  • 5. acquire() and release() • acquire() { while (!available) ; /* busy wait */ available = false;; } • release() { available = true; } • do { acquire lock critical section release lock remainder section } while (true);
  • 6. Semaphore • Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. • Semaphore S – integer variable • Can only be accessed via two indivisible (atomic) operations – wait() and signal() • Originally called P() and V() • Definition of the wait() operation wait(S) { while (S <= 0) ; // busy wait S--; } • Definition of the signal() operation signal(S) { S++; }
  • 7. Semaphore Usage • Counting semaphore – integer value can range over an unrestricted domain • Binary semaphore – integer value can range only between 0 and 1 – Same as a mutex lock • Can solve various synchronization problems • Consider P1 and P2 that require S1 to happen before S2 Create a semaphore “synch” initialized to 0 P1: S1; signal(synch); P2: wait(synch); S2; • Can implement a counting semaphore S as a binary semaphore
  • 8. Semaphore Implementation • Must guarantee that no two processes can execute the wait() and signal() on the same semaphore at the same time • Thus, the implementation becomes the critical section problem where the wait and signal code are placed in the critical section – Could now have busy waiting in critical section implementation • But implementation code is short • Little busy waiting if critical section rarely occupied • Note that applications may spend lots of time in critical sections and therefore this is not a good solution
  • 9. Semaphore Implementation with no Busy waiting • With each semaphore there is an associated waiting queue • Each entry in a waiting queue has two data items: – value (of type integer) – pointer to next record in the list • Two operations: – block – place the process invoking the operation on the appropriate waiting queue – wakeup – remove one of processes in the waiting queue and place it in the ready queue • typedef struct{ int value; struct process *list; } semaphore;
  • 10. Implementation with no Busy waiting (Cont.) wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } } signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(P); } }
  • 11. Deadlock and Starvation • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes • Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S); ... ... signal(S); signal(Q); signal(Q); signal(S); • Starvation – indefinite blocking – A process may never be removed from the semaphore queue in which it is suspended • Priority Inversion – Scheduling problem when lower-priority process holds a lock needed by higher-priority process – Solved via priority-inheritance protocol
  • 12. Assignment • Explain the concept of Mutex Locks • What is a Semaphore. Explain Semaphore Implementation.