Semaphores in Process Synchronization
Last Updated :
07 Jan, 2025
Semaphores are a tool used in operating systems to help manage how different processes (or programs) share resources, like memory or data, without causing conflicts. A semaphore is a special kind of synchronization data that can be used only through specific synchronization primitives. Semaphores are used to implement critical sections, which are regions of code that must be executed by only one process at a time. By using semaphores, processes can coordinate access to shared resources, such as shared memory or I/O devices.
What is Semaphores?
A semaphore is a synchronization tool used in concurrent programming to manage access to shared resources. It is a lock-based mechanism designed to achieve process synchronization, built on top of basic locking techniques.
Semaphores use a counter to control access, allowing synchronization for multiple instances of a resource. Processes can attempt to access one instance, and if it is not available, they can try other instances. Unlike basic locks, which allow only one process to access one instance of a resource. Semaphores can handle more complex synchronization scenarios, involving multiple processes or threads. It help prevent problems like race conditions by controlling when and how processes access shared data.
The process of using Semaphores provides two operations:
- wait (P): The wait operation decrements the value of the semaphore
- signal (V): The signal operation increments the value of the semaphore.
When the value of the semaphore is zero, any process that performs a wait operation will be blocked until another process performs a signal operation.
When a process performs a wait operation on a semaphore, the operation checks whether the value of the semaphore is >0. If so, it decrements the value of the semaphore and lets the process continue its execution; otherwise, it blocks the process on the semaphore. A signal operation on a semaphore activates a process blocked on the semaphore if any, or increments the value of the semaphore by 1. Due to these semantics, semaphores are also called counting semaphores. The initial value of a semaphore determines how many processes can get past the wait operation.
Semaphores are required for process synchronization to make sure that multiple processes can safely share resources without interfering with each other. They help control when a process can access a shared resource, preventing issues like race conditions.
Types of Semaphores
Semaphores are of two Types:
- Binary Semaphore: This is also known as a mutex lock, as they are locks that provide mutual exclusion. It can have only two values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section problems with multiple processes and a single resource.
- Counting Semaphore: Counting semaphores can be used to control access to a given resource consisting of a finite number of instances. The semaphore is initialized to the number of resources available. Its value can range over an unrestricted domain.
To learn more refer: Types of Semaphores
Working of Semaphore
A semaphore is a simple yet powerful synchronization tool used to manage access to shared resources in a system with multiple processes. It works by maintaining a counter that controls access to a specific resource, ensuring that no more than the allowed number of processes access the resource at the same time.
There are two primary operations that a semaphore can perform:
- Wait (P operation): This operation checks the semaphore’s value. If the value is greater than 0, the process is allowed to continue, and the semaphore’s value is decremented by 1. If the value is 0, the process is blocked (waits) until the semaphore value becomes greater than 0.
- Signal (V operation): After a process is done using the shared resource, it performs the signal operation. This increments the semaphore’s value by 1, potentially unblocking other waiting processes and allowing them to access the resource.
Now let us see how it does so. First, look at two operations that can be used to access and change the value of the semaphore variable.

A critical section is surrounded by both operations to implement process synchronization. The below image demonstrates the basic mechanism of how semaphores are used to control access to a critical section in a multi-process environment, ensuring that only one process can access the shared resource at a time

Now, let us see how it implements mutual exclusion. Let there be two processes P1 and P2 and a semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of semaphore s becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen when P1 finishes its critical section and calls V operation on semaphore s.
This way mutual exclusion is achieved. Look at the below image for details which is a Binary semaphore.

Implementation – Binary Semaphores
C++
struct semaphore {
enum value(0, 1);
// q contains all Process Control Blocks (PCBs)
// corresponding to processes got blocked
// while performing down operation.
Queue<process> q;
};
P(semaphore s)
{
if (s.value == 1) {
s.value = 0;
}
else {
// add the process to the waiting queue
q.push(P) sleep();
}
}
V(semaphore s)
{
if (s.q is empty) {
s.value = 1;
}
else {
// select a process from waiting queue
Process p = q.front();
// remove the process from waiting as it has been
// sent for CS
q.pop();
wakeup(p);
}
}
C
#include <stdio.h>
#include<stdlib.h>
#include<sys/queue.h>
struct semaphore{
Queue<process> q;
int value;
};
void P(struct semaphore s)
{
if (s.value == 1) {
s.value = 0;
}
else {
s.q.push(P);
sleep();
}
}
void V(semaphore s)
{
if (s.q is empty) {
s.value = 1;
}
else {
// Get a process from the Waiting Queue
Process p = q.front();
// Remove the process from waiting
q.pop();
wakeup(p);
}
}
int main() {
printf("This is Hemish!!");
// This code is Contributed by Himesh Singh Chauhan
return 0;
}
// This code is modified by Susobhan Akhuli
Java
import java.util.*;
class Semaphore {
public enum Value { Zero, One }
public Queue<Process> q = new LinkedList<Process>();
public Value value = Value.One;
public void P(Semaphore s, Process p)
{
if (s.value == Value.One) {
s.value = Value.Zero;
}
else {
// add the process to the waiting queue
q.add(p);
p.Sleep();
}
}
public void V(Semaphore s)
{
if (s.q.size() == 0) {
s.value = Value.One;
}
else {
// select a process from waiting queue
Process p = q.peek();
// remove the process from waiting as it has
// been sent for CS
q.remove();
p.Wakeup();
}
}
}
Python
from enum import Enum
from queue import Queue
class Semaphore:
class Value(Enum):
Zero = 0
One = 1
def __init__(self):
self.q = Queue()
self.value = Semaphore.Value.One
def P(self, s, p):
if s.value == Semaphore.Value.One:
s.value = Semaphore.Value.Zero
else:
# add the process to the waiting queue
s.q.put(p)
p.Sleep()
def V(self, s):
if s.q.qsize() == 0:
s.value = Semaphore.Value.One
else:
# select a process from waiting queue
p = s.q.queue[0]
# remove the process from waiting as it has
# been sent for CS
s.q.get()
p.Wakeup()
C#
using System.Collections.Generic;
class Semaphore {
public enum value { Zero, One }
public Queue<Process> q = new Queue<Process>();
public void P(Semaphore s, Process p) {
if (s.value == value.One) {
s.value = value.Zero;
} else {
// add the process to the waiting queue
q.Enqueue(p);
p.Sleep();
}
}
public void V(Semaphore s) {
if (s.q.Count == 0) {
s.value = value.One;
} else {
// select a process from waiting queue
Process p = q.Peek();
// remove the process from waiting as it has been
// sent for CS
q.Dequeue();
p.Wakeup();
}
}
}
JavaScript
class Semaphore {
constructor() {
this.value = 0;
// q contains all Process Control Blocks (PCBs)
// corresponding to processes got blocked
// while performing down operation.
this.q = [];
}
P() {
if (this.value == 1) {
this.value = 0;
} else {
// add the process to the waiting queue
this.q.push(P);
sleep();
}
}
V() {
if (this.q.length == 0) {
this.value = 1;
} else {
// select a process from waiting queue
let p = this.q.shift();
// remove the process from waiting as it has been
// sent for CS
wakeup(p);
}
}
}
The description above is for binary semaphore which can take only two values 0 and 1 and ensure mutual exclusion. There is one other type of semaphore called counting semaphore which can take values greater than one.
Now suppose there is a resource whose number of instances is 4. Now we initialize S = 4 and the rest is the same as for binary semaphore. Whenever the process wants that resource it calls P or waits for function and when it is done it calls V or signal function. If the value of S becomes zero then a process has to wait until S becomes positive. For example, Suppose there are 4 processes P1, P2, P3, and P4, and they all call wait operation on S(initialized with 4). If another process P5 wants the resource then it should wait until one of the four processes calls the signal function and the value of semaphore becomes positive.
Limitations
- One of the biggest limitations of semaphore is priority inversions.
- Deadlock, suppose a process is trying to wake up another process that is not in a sleep state. Therefore, a deadlock may be blocked indefinitely.
- The operating system has to keep track of all calls to wait and signal the semaphore.
The main problem with semaphores is that they require busy waiting, If a process is in the critical section, then other processes trying to enter the critical section will be waiting until the critical section is not occupied by any process. Whenever any process waits then it continuously checks for semaphore value (look at this line while (s==0); in P operation) and wastes CPU cycle.
There is also a chance of “spinlock” as the processes keep on spinning while waiting for the lock. To avoid this another implementation is provided below.
Implementation – Counting semaphore
CPP
struct Semaphore {
int value;
// q contains all Process Control Blocks(PCBs)
// corresponding to processes got blocked
// while performing down operation.
Queue<process> q;
};
P(Semaphore s)
{
s.value = s.value - 1;
if (s.value < 0) {
// add process to queue
// here p is a process which is currently executing
q.push(p);
block();
}
else
return;
}
V(Semaphore s)
{
s.value = s.value + 1;
if (s.value <= 0) {
// remove process p from queue
Process p = q.pop();
wakeup(p);
}
else
return;
}
Java
import java.util.LinkedList;
import java.util.Queue;
// semaphore class
class Semaphore {
// our value
int value;
Queue<Process> q;
public Semaphore(int value)
{
this.value = value;
q = new LinkedList<>();
}
public void P(Process p)
{
value--;
if (value < 0) {
q.add(p);
p.block();
}
}
public void V()
{
value++;
if (value <= 0) {
Process p = q.remove();
p.wakeup();
}
}
}
Python
import heapq
# Global Variable to track the Processes going into Critical Section
COUNTER=1
class Semaphore:
def __init__(self,value):
# Value of the Semaphore passed to the Constructor
self.value=value
# The Waiting queue which will be using the heapq module of Python
self.q=list()
def getSemaphore(self):
''' Function to print the Value of the Semaphore Variable '''
print(f"Semaphore Value: {self.value}")
def block(process):
print(f"Process {process} Blocked.")
def wakeup(process):
print(f"Process {process} Waked Up and Completed it's work.")
def P(s):
global COUNTER
s.value=s.value-1
if(s.value<0):
heapq.heappush(s.q,COUNTER)
block(COUNTER)
else:
print(f'Process {COUNTER} gone inside the Critical Section.')
COUNTER+=1
return
def V(s):
global COUNTER
s.value=s.value+1
if(s.value<=0):
p=heapq.heappop(s.q)
wakeup(p)
COUNTER-=1
else:
print(f"Process {COUNTER} completed it's work.")
COUNTER-=1
return
# Can Pass the Value of the Counting Semaphore to the Class Constructor
# Example for Counting Semaphore value as 2
s1=Semaphore(2)
s1.getSemaphore()
P(s1)
s1.getSemaphore()
P(s1)
s1.getSemaphore()
P(s1)
s1.getSemaphore()
V(s1)
s1.getSemaphore()
V(s1)
s1.getSemaphore()
V(s1)
s1.getSemaphore()
# This Code is Contributed by Himesh Singh Chauhan
C#
using System.Collections.Generic;
public class Semaphore {
public int value;
// q contains all Process Control Blocks(PCBs)
// corresponding to processes got blocked
// while performing down operation.
Queue<Process> q = new Queue<Process>();
public void P(Process p)
{
value--;
if (value < 0) {
// add process to queue
q.Enqueue(p);
p.block();
}
}
public void V()
{
value++;
if (value <= 0) {
// remove process p from queue
Process p = q.Dequeue();
p.wakeup();
}
}
}
JavaScript
// Define a Semaphore object
function Semaphore() {
this.value = 0;
this.q = []; // Initialize an array to act as a queue
}
// Implement the P operation
Semaphore.prototype.P = function(p) {
this.value = this.value - 1;
if (this.value < 0) {
// Add process to queue
this.q.push(p);
// Assuming block() and wakeup() functions are defined elsewhere
block();
}
};
// Implement the V operation
Semaphore.prototype.V = function() {
this.value = this.value + 1;
if (this.value <= 0) {
// Remove process p from queue
var p = this.q.shift();
// Assuming wakeup() function is defined elsewhere
wakeup(p);
}
};
// This code is contributed by Susobhan Akhuli
In this implementation whenever the process waits it is added to a waiting queue of processes associated with that semaphore. This is done through the system call block() on that process. When a process is completed it calls the signal function and one process in the queue is resumed. It uses the wakeup() system call.
Uses of Semaphores
- Mutual Exclusion : Semaphore ensures that only one process accesses a shared resource at a time.
- Process Synchronization : Semaphore coordinates the execution order of multiple processes.
- Resource Management : Limits access to a finite set of resources, like printers, devices, etc.
- Reader-Writer Problem : Allows multiple readers but restricts the writers until no reader is present.
- Avoiding Deadlocks : Prevents deadlocks by controlling the order of allocation of resources.
Advantages of Semaphores
- Semaphore is a simple and effective mechanism for process synchronization
- Supports coordination between multiple processes. By controlling the access to critical sections, semaphores help in managing multiple processes without them interfering with each other.
- When used correctly, semaphores can help avoid deadlocks by managing access to resources efficiently and ensuring that no process is indefinitely blocked from accessing necessary resources.
- Semaphores help prevent race conditions by ensuring that only one process can access a shared resource at a time.
- Provides a flexible and robust way to manage shared resources.
Disadvantages of Semaphores
- It Can lead to performance degradation due to overhead associated with wait and signal operations.
- If semaphores are not managed carefully, they can lead to deadlock. This often occurs when semaphores are not released properly or when processes acquire semaphores in an inconsistent order.
- It can cause performance issues in a program if not used properly.
- It can be difficult to debug and maintain. Debugging systems that rely heavily on semaphores can be challenging, as it is hard to track the state of each semaphore and ensure that all processes are correctly synchronized
- It can be prone to race conditions and other synchronization problems if not used correctly.
- It can be vulnerable to certain types of attacks, such as denial of service attacks.
Classical Synchronization Problems using Semaphores Concept
These are the classical synchronization problem using the concept of semaphores.
1. Producer-Consumer Problem
The producer-consumer problem involves two types of processes producers that generate data and consumers that process data. The Producer-Consumer Problem is like a restaurant where the chef (producer) makes food and the customer (consumer) eats it. The counter (buffer: A fixed-size queue where producers place items and consumers remove items.) holds food temporarily. A special lock (semaphore) ensures the chef doesn’t overflow the counter and the customer doesn’t take food when it’s not available. In This way, everything runs smoothly and efficiently and gives faster results.
Semaphores Used
- empty: Counts the number of empty slots in the buffer
- full: Counts the number of full slots in the buffer
- mutex: Locks access to the buffer (mutual exclusion)
2. Traffic Light Control
Description: Traffic lights at an intersection can be managed using semaphores to control the flow of traffic and ensure safe crossing.
Example:
Traffic Lights: Represented by semaphores that control the green, yellow, and red lights for different directions.
Semaphores Used: Each light direction is controlled by a semaphore that manages the timing and transitions between light states.
Implementation
Light Controller: Uses semaphores to cycle through green, yellow, and red lights. The controller ensures that only one direction has the green light at a time and manages transitions to avoid conflicts.
3. Bank Transaction Processing
- Multiple Transactions: Processes that need to access shared resources (accounts)
- Account Balance: Shared resource represented by a semaphore
- Semaphore value: 1 (only one transaction can access and modify an account at a time is crucial for maintaining data integrity).
Process
- Acquire Semaphore: A transaction must need to acquire the semaphore first before modifying an account balance.
- The account is : Then operations like debiting or crediting the account are performed by the transaction.
- Release Semaphore: After Completing the transactions the semaphore is released to allow other transactions to proceed.
4. Print Queue Management
- Multiple Print Jobs: Processes that need to access shared resources (printers)
- Printer: Shared resource represented by a semaphore
- Semaphore Value: 1 (only one print job can access the printer at a time)
Process
- Acquire Semaphore: First, acquire a semaphore for the printer to begin the print job.
- Print Job Execution: The printer processes the print job.
- Release Semaphore: Now the semaphore is released after the job is done.
5. Railway Track Management
- Multiple Trains: Processes that need to access shared resources (tracks)
- Track: Shared resource represented by a semaphore
- Semaphore Value: 1 (only one train can access the track at a time)
Process
- Acquire Semaphore: A train acquires the semaphore before entering a track.
- Travel Across Track: train Travels across the track.
- Release Semaphore: The semaphores are released after the train passes the track.
6. Dining Philosopher’s Problem
- Multiple Philosophers: Process that needs to access shared resources(forks).
- Forks: Shared resources represented by semaphores, that need to eat. Each philosopher needs two forks to eat.
- Semaphore Value: 1 (Only one philosopher can access a fork at a time preventing deadlock and starvation).
Process
- Acquire Forks: Philosophers acquire the semaphore for both the left and right forks before eating.
- Eat eat : When both the forks are acquired then the philosopher eats.
- Release Forks: After eating, both the forks are released for others to use them.
Solution – Dining Philosopher Problem Using Semaphores
7. Reader-Writer Problem
In Reader-Writer problem Synchronizing access to shared data where multiple readers can read the data simultaneously, but writers need exclusive access to modify it. In simple terms imagine a library where multiple readers and writers come and all readers want to read a book, and some people(writers) want to update or edit the book. That’s why we need a system to ensure that these actions are done smoothly without errors or conflicts.
- Multiple Readers and Writers: Processes that need to access a shared resource(data).
- Data: Shared resources represented by a Semaphore.
- Semaphore Value for Reader: Process that reads shared data, Can access simultaneously(value>1, more than one reader can access at the same time)
- Semaphore Value for Writers: Processes that modify shared data need exclusive access(value =1, one at a time)
Solution –
Conclusion
Process Synchronization is an important of concurrent computing, ensuring that multiple processes can execute without interfering with each other. Semaphores can be used to tackle classic synchronization challenges in computing. It plays a vital role In managing access to shared resources and coordinating the activities of concurrent processes. It also helps to improve system performance, maintain data integrity, and remove the conflicts between the processes. Effective process synchronization ensures consistency, stability, and efficiency in multi-threaded and multi-process systems.
Semaphores in Process Synchronization – FAQs
Why is Synchronization important?
To maintain Data Consistency, Efficient use of resources, and avoiding problems like race conditions, and deadlock.
What does a semaphore protect?
It protects the simultaneous access of shared resources by multiple processes.
Why it is important to protect Critical Sections?
Protecting critical sections ensures consistency of data and prevents conflicts.
What is the difference between process synchronization and mutual exclusion?
Process synchronization coordinates access to shared resources, while mutual exclusion ensures only one process can access a resource at a time.
Similar Reads
Operating System Tutorial
An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
5 min read
Structure of Operating System
Types of OS
Batch Processing Operating System
In the beginning, computers were very large types of machinery that ran from a console table. In all-purpose, card readers or tape drivers were used for input, and punch cards, tape drives, and line printers were used for output. Operators had no direct interface with the system, and job implementat
6 min read
Multiprogramming in Operating System
As the name suggests, Multiprogramming means more than one program can be active at the same time. Before the operating system concept, only one program was to be loaded at a time and run. These systems were not efficient as the CPU was not used efficiently. For example, in a single-tasking system,
5 min read
Time Sharing Operating System
Multiprogrammed, batched systems provide an environment where various system resources were used effectively, but it did not provide for user interaction with computer systems. Time-sharing is a logical extension of multiprogramming. The CPU performs many tasks by switches that are so frequent that
5 min read
What is a Network Operating System?
The basic definition of an operating system is that the operating system is the interface between the computer hardware and the user. In daily life, we use the operating system on our devices which provides a good GUI, and many more features. Similarly, a network operating system(NOS) is software th
2 min read
Real Time Operating System (RTOS)
Real-time operating systems (RTOS) are used in environments where a large number of events, mostly external to the computer system, must be accepted and processed in a short time or within certain deadlines. such applications are industrial control, telephone switching equipment, flight control, and
6 min read
Process Management
Introduction of Process Management
Process Management for a single tasking or batch processing system is easy as only one process is active at a time. With multiple processes (multiprogramming or multitasking) being active, the process management becomes complex as a CPU needs to be efficiently utilized by multiple processes. Multipl
8 min read
Process Table and Process Control Block (PCB)
While creating a process, the operating system performs several operations. To identify the processes, it assigns a process identification number (PID) to each process. As the operating system supports multi-programming, it needs to keep track of all the processes. For this task, the process control
6 min read
Operations on Processes
Process operations refer to the actions or activities performed on processes in an operating system. These operations include creating, terminating, suspending, resuming, and communicating between processes. Operations on processes are crucial for managing and controlling the execution of programs i
5 min read
Process Schedulers in Operating System
A process is the instance of a computer program in execution. Scheduling is important in operating systems with multiprogramming as multiple processes might be eligible for running at a time.One of the key responsibilities of an Operating System (OS) is to decide which programs will execute on the C
7 min read
Inter Process Communication (IPC)
Processes can coordinate and interact with one another using a method called inter-process communication (IPC) . Through facilitating process collaboration, it significantly contributes to improving the effectiveness, modularity, and ease of software systems. Types of Process Independent processCo-o
15 min read
Context Switching in Operating System
An operating system is a program loaded into a system or computer. and manage all the other program which is running on that OS Program, it manages the all other application programs. or in other words, we can say that the OS is an interface between the user and computer hardware. So in this article
6 min read
Preemptive and Non-Preemptive Scheduling
In operating systems, scheduling is the method by which processes are given access the CPU. Efficient scheduling is essential for optimal system performance and user experience. There are two primary types of CPU scheduling: preemptive and non-preemptive. Understanding the differences between preemp
6 min read
Critical Section Problem Solution
Peterson's Algorithm in Process Synchronization
Prerequisite - Synchronization, Critical Section The producer-consumer problem (or bounded buffer problem) describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue. Producers produce an item and put it into the buffer. If the buffer is already
15+ min read
Semaphores in Process Synchronization
Semaphores are a tool used in computer science to help manage how different processes (or programs) share resources, like memory or data, without causing conflicts. A semaphore is a special kind of synchronization data that can be used only through specific synchronization primitives. Semaphores are
15+ min read
Semaphores and its types
A semaphore is a tool used in computer science to manage how multiple programs or processes access shared resources, like memory or files, without causing conflicts. Semaphores are compound data types with two fields one is a Non-negative integer S.V(Semaphore Value) and the second is a set of proce
6 min read
Producer Consumer Problem using Semaphores | Set 1
The Producer-Consumer problem is a classic synchronization issue in operating systems. It involves two types of processes: producers, which generate data, and consumers, which process that data. Both share a common buffer. The challenge is to ensure that the producer doesn't add data to a full buffe
4 min read
Readers-Writers Problem | Set 1 (Introduction and Readers Preference Solution)
The readers-writer problem in operating systems is about managing access to shared data. It allows multiple readers to read data at the same time without issues but ensures that only one writer can write at a time, and no one can read while writing is happening. This helps prevent data corruption an
8 min read
Dining Philosopher Problem Using Semaphores
The Dining Philosopher Problem states that K philosophers are seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One chopstick may be picked
11 min read
Hardware Synchronization Algorithms : Unlock and Lock, Test and Set, Swap
Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. There are a software as well as
5 min read
Deadlocks & Deadlock Handling Methods
Introduction of Deadlock in Operating System
A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process. In this article, we will discuss deadlock, its necessary conditions, etc. in detail. What is Deadlock?Deadlock is a situation in c
11 min read
Conditions for Deadlock in Operating System
A deadlock is a situation where a set of processes is blocked because each process is holding a resource and waiting for another resource acquired by some other process. In this article, we will discuss what deadlock is and the necessary conditions required for deadlock. What is Deadlock?Deadlock is
8 min read
Banker's Algorithm in Operating System
The Bankerâs Algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for the predetermined maximum possible amounts of all resources, then makes an âs-stateâ check to test for possible activities, before deciding whether allocation shoul
15+ min read
Wait For Graph Deadlock Detection in Distributed System
Deadlocks are a fundamental problem in distributed systems. A process may request resources in any order and a process can request resources while holding others. A Deadlock is a situation where a set of processes are blocked as each process in a Distributed system is holding some resources and that
5 min read
Handling Deadlocks
Deadlock is a situation where a process or a set of processes is blocked, waiting for some other resource that is held by some other waiting process. It is an undesirable state of the system. In other words, Deadlock is a critical situation in computing where a process, or a group of processes, beco
9 min read
Deadlock Prevention And Avoidance
When two or more processes try to access the critical section at the same time and they fail to access simultaneously or stuck while accessing the critical section then this condition is known as Deadlock. Deadlock prevention and avoidance are strategies used in computer systems to ensure that diffe
6 min read
Deadlock Detection And Recovery
Deadlock Detection and Recovery is the mechanism of detecting and resolving deadlocks in an operating system. In operating systems, deadlock recovery is important to keep everything running smoothly. A deadlock occurs when two or more processes are blocked, waiting for each other to release the reso
6 min read
Deadlock Ignorance in Operating System
In this article we will study in brief about what is Deadlock followed by Deadlock Ignorance in Operating System. What is Deadlock? If each process in the set of processes is waiting for an event that only another process in the set can cause it is actually referred as called Deadlock. In other word
5 min read
Recovery from Deadlock in Operating System
In today's world of computer systems and multitasking environments, deadlock is an undesirable situation that can bring operations to a halt. When multiple processes compete for exclusive access to resources and end up in a circular waiting pattern, a deadlock occurs. To maintain the smooth function
8 min read
Memory Management
Memory Management in Operating System
The term memory can be defined as a collection of data in a specific format. It is used to store instructions and process data. The memory comprises a large array or group of words or bytes, each with its own location. The primary purpose of a computer system is to execute programs. These programs,
14 min read
Implementation of Contiguous Memory Management Techniques
Memory Management Techniques are basic techniques that are used in managing the memory in the operating system. In this article, we will deal with the implementation of Continuous Memory Management Techniques. Memory Management Techniques are classified broadly into two categories: ContiguousNon-con
4 min read
Non-Contiguous Allocation in Operating System
Non-contiguous allocation, also known as dynamic or linked allocation, is a memory allocation technique used in operating systems to allocate memory to processes that do not require a contiguous block of memory. In this technique, each process is allocated a series of non-contiguous blocks of memory
6 min read
Compaction in Operating System
Compaction is a technique to collect all the free memory present in the form of fragments into one large chunk of free memory, which can be used to run other processes. It does that by moving all the processes towards one end of the memory and all the available free space towards the other end of th
3 min read
Best-Fit Allocation in Operating System
INTRODUCTION: Best-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to a process. In Best-Fit, the operating system searches through the list of free blocks of memory to find the block that is closest in size to the memory request from the process. Once a
4 min read
Worst-Fit Allocation in Operating Systems
For both fixed and dynamic memory allocation schemes, the operating system must keep a list of each memory location noting which are free and which are busy. Then as new jobs come into the system, the free partitions must be allocated. These partitions may be allocated in 4 ways: 1. First-Fit Memory
2 min read
First-Fit Allocation in Operating Systems
INTRODUCTION: First-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to a process. In First-Fit, the operating system searches through the list of free blocks of memory, starting from the beginning of the list, until it finds a block that is large enough t
3 min read
Fixed (or static) Partitioning in Operating System
Fixed partitioning, also known as static partitioning, is one of the earliest memory management techniques used in operating systems. In this method, the main memory is divided into a fixed number of partitions at system startup, and each partition is allocated to a process. These partitions remain
8 min read
Variable (or Dynamic) Partitioning in Operating System
In operating systems, Memory Management is the function responsible for allocating and managing a computerâs main memory. The memory Management function keeps track of the status of each memory location, either allocated or free to ensure effective and efficient use of Primary Memory. Below are Memo
4 min read
Paging in Operating System
Paging is a memory management scheme that eliminates the need for a contiguous allocation of physical memory. The process of retrieving processes in the form of pages from the secondary storage into the main memory is known as paging. The basic purpose of paging is to separate each procedure into pa
10 min read
Segmentation in Operating System
A process is divided into Segments. The chunks that a program is divided into which are not necessarily all of the exact sizes are called segments. Segmentation gives the user's view of the process which paging does not provide. Here the user's view is mapped to physical memory. Types of Segmentatio
4 min read
Virtual Memory in Operating System
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of the main memory. The addresses a program may use to reference memory are distinguished from the addresses the memory system uses to identify physical storage sites and program-generated
15+ min read
Page Replacement Algorithms
Page Replacement Algorithms in Operating Systems
In an operating system that uses paging for memory management, a page replacement algorithm is needed to decide which page needs to be replaced when a new page comes in. Page replacement becomes necessary when a page fault occurs and no free page frames are in memory. in this article, we will discus
8 min read
Program for Page Replacement Algorithms | Set 2 (FIFO)
Prerequisite : Page Replacement Algorithms In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating S
10 min read
Belady's Anomaly in Page Replacement Algorithms
In the Operating System, process data is loaded in fixed-sized chunks and each chunk is referred to as a page. The processor loads these pages in fixed-sized chunks of memory called frames. Typically the size of each page is always equal to the frame size. A page fault occurs when a page is not foun
10 min read
Optimal Page Replacement Algorithm
Prerequisite: Page Replacement Algorithms In operating systems, whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. Different page replacement algorithms suggest different ways to decide which pa
15+ min read
Program for Least Recently Used (LRU) Page Replacement algorithm
Prerequisite: Page Replacement AlgorithmsIn operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating Sys
14 min read
Techniques to handle Thrashing
Prerequisite - Virtual Memory Thrashing is a condition or a situation when the system is spending a major portion of its time servicing the page faults, but the actual processing done is very negligible. Causes of thrashing: High degree of multiprogramming.Lack of frames.Page replacement policy.Thra
6 min read