Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views33 pages

OS Notes Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views33 pages

OS Notes Final

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Process Scheduling

Definition of Process Scheduling:

Process scheduling is the activity performed by the operating system to decide the order in which
processes in the ready queue are given access to the CPU. Since multiple processes may be ready
to run at the same time, the OS must schedule them efficiently to improve performance and
responsiveness.

Explanation:

In a multitasking or multiprogramming environment, multiple processes compete for CPU time.


The operating system uses a scheduler to determine:

• Which process runs next


• How long it runs
• When it is paused or resumed

This ensures efficient use of the CPU and fair treatment of processes.
Main Goals of Process Scheduling:

• Maximize CPU utilization (keep the CPU busy as much as possible)


• Minimize response time (especially for interactive users)
• Minimize waiting and turnaround times
• Ensure fairness among all processes

Types of Process Schedulers:


1. Long-Term Scheduler (Job Scheduler)

The Long-Term Scheduler, also known as the Job Scheduler, is a critical component of an
operating system responsible for controlling the admission of jobs into the system. It determines
which programs are admitted into the system for processing and when. The long-term scheduler
manages the job queue and selects processes from the pool of submitted jobs and loads them into
the ready queue in main memory. Its main goal is to maintain a balanced mix of I/O-bound and
CPU-bound processes to ensure efficient use of system resources. Unlike the short-term scheduler
that operates frequently, the long-term scheduler executes less often, typically when a job
completes or when system load changes. In systems with heavy load or high multitasking needs, a
well-optimized long-term scheduler is essential to avoid resource starvation or overloading the
CPU.

+-----------------+
| Job Pool (Disk)|
+-----------------+
|
| [Long-Term Scheduler]
v
+-----------------+
| Ready Queue | <--+
+-----------------+ |
| |
| [Short-Term Scheduler]
v |
+-----------------+ |
| CPU |----+
+-----------------+
|
| [Process Completion]
v
+-----------------+
| Terminated Jobs |
+-----------------+

2. Short-Term Scheduler (CPU Scheduler)

The Short-Term Scheduler, also known as the CPU scheduler, is a key component of an operating
system's process management. It is responsible for selecting one of the ready-to-execute processes
and allocating the CPU to it. This scheduler operates frequently and makes decisions every time
the CPU becomes idle or a process completes. It must be fast and efficient since its decisions
directly affect system responsiveness and CPU utilization. The short-term scheduler works with
the ready queue, which holds all the processes that are ready and waiting to run on the CPU. Unlike
long-term and medium-term schedulers, which run less frequently, the short-term scheduler runs
multiple times per second.

• Example: Deciding which process in the ready queue will execute on the CPU next.

3. Medium-Term Scheduler (Swapper)

The Medium-Term Scheduler is an operating system component that handles process swapping
— moving processes between main memory (RAM) and secondary storage (disk). It is primarily
used to improve multiprogramming and manage system load. When the memory is full or the
system is overloaded, the medium-term scheduler suspends some processes (moves them from
RAM to disk), freeing up memory for others. Later, it may resume those suspended processes
when resources are available. This form of scheduling is essential in time-sharing systems where
maintaining a balance between active and suspended processes improves performance and
responsiveness.

• Example: Moving a paused process to disk and bringing it back later.

Categories of Scheduling:

Feature Preemptive Scheduling Non-Preemptive Scheduling


Definition The CPU can be taken away from Once a process starts executing, it cannot be
a process before it finishes. interrupted until it finishes.
Control The OS can forcibly switch to The process keeps the CPU until it
another process. voluntarily releases it (e.g., via I/O or
completion).
Response Better response time for Higher response time for short or urgent
Time interactive systems. tasks.
Overhead Higher (due to frequent context Lower (since switching happens less often).
switching).
Fairness More fair – every process gets a Can lead to starvation of shorter or low-
chance. priority processes.
Examples Round Robin, Preemptive FCFS, SJF (non-preemptive), Priority (non-
Priority, SRTF preemptive)

Process Scheduling Algorithms


First Come First Serve (FCFS):

Definition:
FCFS is the simplest CPU scheduling algorithm. In FCFS, the process that arrives first in the
ready queue is scheduled first for execution, just like standing in a line at a shop: whoever comes
first gets served first.

Characteristics:

• Type: Non-preemptive
• Scheduling Order: Based on arrival time
• Fairness: All processes are treated equally, but it may not always be efficient.

How It Works:

Processes are handled in the exact order they arrive, without interruption. Once a process starts
execution, it runs until completion.

Example:

Let’s take 3 processes:

Process Arrival Time Burst Time


P1 0 4
P2 1 3
P3 2 5

Gantt Chart:
Time → 0 4 7 12
| P1 | P2 | P3 |

Calculation Table:
Process Arrival Burst Start Completion Turnaround Time Waiting Time
Time Time Time Time (CT - AT) (TAT - BT)

P1 0 4 0 4 4 0

P2 1 3 4 7 6 3

P3 2 5 7 12 10 5

Advantages of FCFS:

• Simple to understand and implement.


• Works well when all processes have similar burst times.

Disadvantages:

• Convoy Effect: Short processes wait behind long ones.


• High waiting time for short or later-arriving processes.
• Non-preemptive: Not suitable for time-sharing systems.

Shortest Job First (SJF) Scheduling


Definition:
Shortest Job First (SJF) is a CPU scheduling algorithm where the process with the shortest burst
time (i.e., the least amount of CPU time needed) is executed first.

Types of SJF:

1. Non-Preemptive SJF
o Once a process starts executing, it runs till completion.
o Suitable for batch systems.
2. Preemptive SJF (also called Shortest Remaining Time First - SRTF)
o If a new process arrives with shorter burst time than the current one, it preempts
(interrupts) the current process.
o Better for interactive systems.

Example: (Non-Preemptive SJF)

Process Arrival Time Burst Time


P1 0 8
P2 1 4
P3 2 2
P4 3 1

Execution Order:

• At time 0 → only P1 is available → start P1


• P1 finishes at 8
• At time 8 → all others (P2, P3, P4) have arrived → pick P4 (1 unit)
• Then P3 (2 units)
• Then P2 (4 units)

Gantt Chart:
Time → 0 8 9 11 15
| P1 | P4 | P3 | P2 |

Final Table:

Process Arrival Burst Completion Turnaround Time (CT Waiting Time


Time Time Time - AT) (TAT - BT)
P1 0 8 8 8 0
P2 1 4 15 14 10
P3 2 2 11 9 7
P4 3 1 9 6 5

Advantages of SJF:

• Minimizes average waiting time — best among all algorithms in this regard.
• Efficient for batch processing.

Disadvantages:

• Difficult to know burst time in advance.


• May cause starvation — longer processes might wait indefinitely.
• Not suitable for real-time systems without estimation.

Priority Scheduling
Definition:
Priority Scheduling is a CPU scheduling algorithm where each process is assigned a priority,
and the CPU is allocated to the process with the highest priority (usually, lower number = higher
priority).

If two processes have the same priority, they are scheduled according to FCFS (First-Come, First-
Served).
Types of Priority Scheduling:

1. Preemptive:
o A newly arrived process with higher priority can interrupt the currently running
process.
2. Non-Preemptive:
o The CPU remains with the current process until it finishes, even if a higher
priority process arrives.

Example (Non-Preemptive):

Process Arrival Time Burst Time Priority


P1 0 4 2
P2 1 3 1
P3 2 2 3

👉 (Lower priority number = higher priority)

Execution Order:

• At time 0 → P1 starts (no other process has arrived)


• At time 1 → P2 arrives with higher priority, but P1 is running (non-preemptive), so it
waits
• After P1 finishes at time 4 → P2 runs
• Then P3

Gantt Chart:
Time → 0 4 7 9
| P1 | P2 | P3 |

Final Table:

Process Arrival Burst Priority Completion Turnaround Time Waiting Time


Time Time Time (CT - AT) (TAT - BT)
P1 0 4 2 4 4 0

P2 1 3 1 7 6 3
P3 2 2 3 9 7 5
Advantages:

• Allows important tasks to be executed earlier.


• More flexible for system control and resource management.

Disadvantages:

• Starvation: Low-priority processes may never get executed.


• Needs a solution like aging (increasing priority of waiting processes over time).

Round Robin (RR) Scheduling


Definition:
Round Robin scheduling is a preemptive CPU scheduling algorithm where each process gets a
fixed time slot (called a time quantum) to execute in a circular order. If a process doesn’t finish
during its time quantum, it goes to the back of the queue.

Key Features:

• Type: Preemptive
• Time Quantum: Small fixed CPU time slice (e.g., 2ms, 4ms, etc.)
• Goal: Fairness and responsiveness
• Used in: Time-sharing and interactive systems

How It Works:

1. All processes are placed in a ready queue.


2. CPU is assigned to the first process in the queue.
3. If the process finishes within the time quantum, it exits.
4. If not, it's paused and placed at the back of the queue.
5. The CPU is then given to the next process, and the cycle continues.

Example:

Let’s take:

Process Arrival Time Burst Time


P1 0 5
P2 1 3
P3 2 1

🕒 Time Quantum = 2 units

Gantt Chart:

Time → 0 2 4 5 7 8
|P1 |P2 |P3 |P1 |P1|

Explanation:

• P1 starts at time 0, runs for 2 units → 3 units left


• P2 runs for 2 units → 1 unit left
• P3 runs for 1 unit (completes)
• P1 runs again for 2 units → 1 unit left
• P2 runs for 1 unit (completes)
• P1 runs last 1 unit (completes)

Final Table:

Process Arrival Burst Completion Turnaround (CT - AT) Waiting (TAT - BT)
P1 0 5 8 8 3
P2 1 3 7 6 3

P3 2 1 5 3 2

Advantages:
• Fair to all processes — no starvation
• Good for time-sharing systems
• Responsive to interactive users

Disadvantages:

• Performance depends on time quantum size:


o Too small → too many context switches (slow system)
o Too large → behaves like FCFS

Multilevel Queue Scheduling


Definition:
Multilevel Queue Scheduling is a CPU scheduling method in which processes are divided into
multiple groups or queues based on their type or priority, and each queue has its own
scheduling algorithm.
Key Concept:

• The ready queue is split into several separate queues.


• Each queue may represent a different category of process, such as:
o System processes (highest priority)
o Interactive processes
o Background jobs (lowest priority)

How It Works:

1. Each process is permanently assigned to one queue based on its characteristics (e.g.,
priority, memory usage, etc.).
2. Each queue may have its own scheduling policy:
o Example:
▪ Interactive queue → Round Robin
▪ Batch queue → FCFS
3. CPU scheduling happens between queues, usually using priority (higher-priority queue
always gets CPU first).
4. No movement of processes between queues (unlike multilevel feedback queue).

Example:

Queue Setup:

Queue Process Type Scheduling Algorithm Priority Level

Q1 System / Real-Time FCFS Highest

Q2 Interactive Round Robin (Time Quantum = 2) Medium

Q3 Batch / Background FCFS Lowest

Processes:

Process Arrival Time Burst Time Queue (Type)


P1 0 4 Q1 (System)
P2 1 6 Q2 (Interactive)
P3 2 3 Q3 (Batch)
P4 3 5 Q2 (Interactive)
P5 4 4 Q3 (Batch)

Scheduling Order:
CPU always checks Q1 first, then Q2, then Q3.

Step-by-step Execution (Gantt Chart):

1. P1 from Q1 starts at 0 → runs till 4


2. Now Q1 is empty → CPU goes to Q2:
o P2 (Q2) runs for 2 (quantum) → remaining = 4
o P4 (Q2) runs for 2 → remaining = 3
o P2 runs for 2 → remaining = 2
o P4 runs for 2 → remaining = 1
o P2 runs for 2 → finishes
o P4 runs for 1 → finishes
3. Now Q2 is empty → CPU checks Q3:
o P3 runs for 3
o P5 runs for 4

Gantt Chart (Time Line):


Time → 0 4 6 8 10 12 14 15 18 22
|P1|P2|P4|P2|P4|P2|P4|P3|P5|

Explanation:

• Q1 (Highest) gets CPU first, so P1 runs from 0–4.


• After that, Q2 processes P2 and P4 are handled with Round Robin.
• Q3 runs only after Q1 and Q2 are both empty.

Final Table:

Process Arrival Burst Queue Type Completion Turnaround Time Waiting Time
Time Time Time (CT - AT) (TAT - BT)
P1 0 4 Q1 (System) 4 4 0
P2 1 6 Q2 14 13 7
(Interactive)
P4 3 5 Q2 15 12 7
(Interactive)
P3 2 3 Q3 (Batch) 18 16 13
P5 4 4 Q3 (Batch) 22 18 14

Advantages:

• Clear separation of process types.


• Efficient for systems with very different kinds of jobs (e.g., interactive + batch).
• Each queue can be optimized independently.

Disadvantages:

• Rigid: Processes are stuck in their assigned queue.


• Starvation: Lower-priority queues may wait too long if higher-priority queues are always
full.
• No flexibility — can’t shift jobs to better-performing queues.

Multilevel Feedback Queue Scheduling


Definition:
Multilevel Feedback Queue Scheduling (MLFQ) is a dynamic scheduling algorithm that allows
processes to move between different queues based on their behavior and aging. The key feature of
MLFQ is that processes can move between queues based on how they are executing, making it
more flexible and adaptive to changing workloads.

Key Characteristics:

• Multiple Queues: There are several queues, each with its own priority and scheduling
algorithm (e.g., Round Robin or FCFS).
• Feedback Mechanism: Processes that use too much CPU time are moved to lower-
priority queues. Processes that use less CPU time or are interactive may be moved to
higher-priority queues.
• Aging: Processes that wait too long in lower-priority queues may be moved to higher-
priority queues to prevent starvation.

How It Works:

1. Multiple Queues:
o Each queue has a different priority and scheduling algorithm. For example:
▪ Queue 1: Highest priority (uses Round Robin or FCFS)
▪ Queue 2: Medium priority (uses Round Robin)
▪ Queue 3: Lowest priority (uses FCFS)
2. Process Movement:
o When a process first enters the system, it starts in the highest priority queue.
o If a process exceeds its time quantum in a given queue, it is moved to a lower-
priority queue (i.e., gets punished for being CPU-intensive).
o If a process waits too long in a low-priority queue, it can be promoted to a higher-
priority queue to avoid starvation (this is aging).
3. Scheduling Behavior:
o Higher-priority queues are served first, and processes in these queues are given
shorter time quanta.
o Lower-priority queues are served only when higher-priority queues are empty,
and processes in these queues receive longer time quanta.

Example:

Let’s assume we have 3 queues with the following parameters:

Queue Priority Level Scheduling Algorithm Time Quantum


Q1 Highest Round Robin 2 ms
Q2 Medium Round Robin 4 ms
Q3 Lowest FCFS Infinite

Process Information:

Process Arrival Time Burst Time


P1 0 5
P2 1 3
P3 2 6
P4 3 8

Scheduling Behavior:

1. Initial State:
o All processes are placed in Q1 (highest priority).
2. Execution of Processes:
o P1 starts at time 0 in Q1 → runs for 2 ms (remaining 3 ms) → moves to Q2 (since
it exceeded time quantum).
o P2 starts at time 2 in Q1 → runs for 2 ms (remaining 1 ms) → moves to Q2.
o P3 starts at time 4 in Q1 → runs for 2 ms (remaining 4 ms) → moves to Q2.
o P4 starts at time 6 in Q1 → runs for 2 ms (remaining 6 ms) → moves to Q2.
3. Move to Q2 (4 ms time quantum):
o P1 runs for 4 ms in Q2 and finishes (total 6 ms).
o P2 runs for 1 ms in Q2 and finishes (total 3 ms).
o P3 runs for 4 ms in Q2 → remaining 2 ms → moves to Q3.
o P4 runs for 4 ms in Q2 → remaining 2 ms → moves to Q3.
4. Execution in Q3 (FCFS, no preemption):
o P3 finishes its remaining 2 ms.
o P4 finishes its remaining 2 ms.

Final Process Information Table:

Process Arrival Burst Queue Completion Turnaround Waiting Time


Time Time Assigned Time Time (CT - AT) (TAT - BT)

P1 0 5 Q1 → Q2 6 6 1
P2 1 3 Q1 → Q2 8 7 4
P3 2 6 Q1 → Q2 16 14 8
→ Q3
P4 3 8 Q1 → Q2 18 15 7
→ Q3

Advantages of MLFQ:
1. Adaptability: MLFQ dynamically adjusts based on the behavior of processes. CPU-bound
processes are penalized, and interactive processes are favored.
2. Prevents Starvation: Thanks to the aging mechanism, processes that are waiting too long
in low-priority queues are promoted, preventing starvation.
3. Fairness: Balances CPU time distribution among different types of processes.

Disadvantages of MLFQ:

1. Complexity: MLFQ is more complex than simpler algorithms like FCFS or Round Robin.
Managing multiple queues and deciding when to promote/demote processes requires
additional logic.
2. Difficulty in Setting Parameters: The parameters (like time quanta and aging) need to be
carefully tuned. If the quantum is too small, too many context switches happen; if too large,
it behaves like FCFS.
3. Overhead: The system needs to monitor process behavior and move processes between
queues, which introduces additional overhead.

Critical Section
Definition:
A Critical Section refers to a part of a program (typically a piece of code) where shared
resources (such as variables, memory, or hardware) are accessed and modified. Because multiple
processes or threads can access these resources simultaneously, there’s a risk of conflicts or data
corruption.

The critical section problem arises when multiple processes or threads try to access and
manipulate shared resources concurrently, and the solution is ensuring that only one process or
thread is allowed to access the critical section at a time.

Critical Section Problem


The use of critical sections in a program can cause a number of issues, including:

• Deadlock: When two or more threads or processes wait for each other to release a critical
section, it can result in a deadlock situation in which none of the threads or processes can
move. Deadlocks can be difficult to detect and resolve, and they can have a significant impact
on a program’s performance and reliability.
• Starvation: When a thread or process is repeatedly prevented from entering a critical
section, it can result in starvation, in which the thread or process is unable to progress. This
can happen if the critical section is held for an unusually long period of time, or if a high-
priority thread or process is always given priority when entering the critical section.
• Overhead: When using critical sections, threads or processes must acquire and release
locks or semaphores, which can take time and resources. This may reduce the program’s
overall performance.

Key Requirements for a Solution:


1. Mutual Exclusion:
Only one process can be in the critical section at any time. This prevents
simultaneous access to shared resources, ensuring consistency.
2. Progress:
If no process is in the critical section and there are processes waiting, then one of the
waiting processes must be allowed to enter the critical section. It should not be stuck
waiting indefinitely.
3. Bounded Waiting:
A process should not have to wait indefinitely to enter the critical section. There must
be a bound on the number of times other processes can enter the critical section
before a waiting process is granted access.

Solutions to the Critical Section Problem:


The solution consists of two categories:

• Hardware based solution


• Software based solution

Hardware-Based Solutions
These solutions rely on special CPU instructions that are executed atomically (meaning they
cannot be interrupted). These instructions help prevent multiple processes from entering the critical
section at the same time.

• The CPU provides support to ensure that only one process can modify a shared variable or
resource at a time.
• These solutions are fast and efficient at the hardware level.
• However, they often involve busy waiting, where a process continuously checks if it can
enter the critical section, which wastes CPU time.
• They are also hardware-dependent, meaning their availability and behavior vary with
different systems.

Types of Hardware-Based Solutions


Hardware-based solutions use atomic operations provided by the CPU to manage access to the
critical section. These operations ensure that a process can check and modify a variable in a single,
uninterruptible step.

1. Test-and-Set

This type of solution checks a value and changes it in one atomic step. It is often used to create
simple locks that control access to the critical section.

• Prevents multiple processes from entering the critical section at once.


• If a process finds the lock already in use, it waits (usually in a loop).
• Causes busy waiting.

2. Compare-and-Swap

This solution compares the current value of a memory location with an expected value. If they
match, it replaces the value with a new one, all in one atomic operation.

• Ensures that only one process can succeed in accessing or modifying a resource.
• Helps in building more advanced synchronization tools like lock-free data structures.

3. Exchange Instruction

This operation swaps the contents of a register with a memory location atomically. It is used to
enforce mutual exclusion by controlling ownership of a resource.

• Guarantees that only one process holds the lock at a time.


• Often used to build basic locking mechanisms.

Software-Based Solutions
These solutions are implemented using code and logical techniques without needing special
hardware instructions.

• They use flags, variables, and structured logic to coordinate which process can enter the
critical section.
• These solutions are more portable and flexible since they can work across different
hardware platforms.
• Many of them avoid busy waiting and are better suited for more complex or multi-process
environments.
• However, they can be harder to design correctly and may require additional OS support.

Types of Software-Based Solutions


Software-based solutions are designed using programming logic to ensure mutual exclusion,
progress, and bounded waiting—the key requirements for handling the critical section problem.

1. Mutex Locks

• A mutex (mutual exclusion) is a lock that only allows one process or thread to access the
critical section at a time.
• Before entering, a process must acquire the lock, and after exiting, it releases the lock.
• If another process has the lock, the requesting process must wait.
2. Semaphores

• A semaphore is a synchronization tool that uses an integer variable to control access to


shared resources.
• There are two types:
o Binary Semaphore: Acts like a mutex (only 0 or 1).
o Counting Semaphore: Allows a fixed number of processes to access the critical
section simultaneously.
• Semaphores use specific operations to manage entry and exit.

3. Peterson’s Algorithm

• A classic algorithm for synchronizing two processes.


• It uses flags and a shared variable to coordinate access to the critical section.
• Ensures fairness and prevents both processes from entering at the same time.

4. Bakery Algorithm

• Designed for multiple processes.


• Works like taking a number in a queue or bakery shop.
• Each process waits for its turn based on its number, ensuring first-come, first-served
access to the critical section.

5. Monitors

• A high-level abstraction provided by some programming languages (like Java).


• Monitors automatically handle the mutual exclusion internally.
• They allow multiple processes to safely wait and notify each other for access to the critical
section.

Deadlock
Deadlock is a state in a computer system where two or more processes are unable to proceed
because each is waiting for a resource that is being held by another process, and none of them
can release their currently held resources.

In simple terms, it’s a situation where processes are stuck waiting on each other forever, and
no process can complete its task.

Example in Concept:

• Process A holds Resource 1 and wants Resource 2.


• Process B holds Resource 2 and wants Resource 1.
• Neither can proceed because each is waiting for the other to release the needed resource.

Four Necessary Conditions for Deadlock (Coffman Conditions)


A deadlock can only happen if all four of the following conditions occur simultaneously in a
system. Let’s go through each one in detail:

1. Mutual Exclusion

• This means resources cannot be shared; only one process can use a resource at any given
time.
• For example, a printer or a file cannot be accessed by two processes at the same time —
one must wait for the other to finish.
• If mutual exclusion is not required, then multiple processes could use the resource
together, and deadlock would not happen.
2. Hold and Wait

• A process is holding at least one resource and waiting for another that is currently held
by a different process.
• This creates a situation where each process is holding something and waiting for
something else — a key step in the buildup to deadlock.
• If processes were required to request all needed resources at once, or release all resources
before requesting new ones, this condition would not occur.

3. No Preemption

• Resources cannot be forcibly removed from a process once they have been allocated.
• The process must release the resource voluntarily after completing its task.
• Without preemption, a process holding a resource might never give it up, preventing others
from using it — contributing to a deadlock.
• If preemption were allowed, the system could interrupt and take back a resource to resolve
the deadlock.
4. Circular Wait

• A circular chain of processes exists, where each process is waiting for a resource that is
held by the next process in the chain.
• For example:
o Process A is waiting for a resource held by Process B
o Process B is waiting for a resource held by Process C
o Process C is waiting for a resource held by Process A
→ This forms a loop where none of the processes can proceed.
• If the system prevents circular waiting by enforcing a strict resource request order, then
this condition can be avoided.

Deadlock Detection
Deadlock detection is a process in operating systems that checks whether a deadlock has
occurred. Unlike prevention or avoidance methods, which attempt to avoid deadlocks from
happening, detection assumes deadlocks will occur occasionally and focuses on identifying and
resolving them when they do.
How Deadlock Detection Works

The goal of deadlock detection is to recognize when a set of processes is in a deadlock state and
then take action to recover from it. The operating system uses algorithms to track the state of the
system, the resources being used, and the resources being requested by each process. If a deadlock
is detected, the OS can terminate processes or force resource preemption to resolve the
situation.

🔄 Steps Involved in Deadlock Detection

1. Resource Allocation Graph (RAG)


o The operating system maintains a Resource Allocation Graph (RAG), where
nodes represent processes and resources, and edges represent relationships between
processes and resources (e.g., "holding" or "waiting").
o Process Nodes: Represent active processes.
o Resource Nodes: Represent resources (e.g., CPU, memory, I/O devices).
o Edges:
▪ A directed edge from a process to a resource indicates that the process is
waiting for that resource.
▪ A directed edge from a resource to a process indicates that the process is
holding that resource.
2. Cycle Detection
o Deadlock is detected by looking for cycles in the Resource Allocation Graph.
▪ Cycle: If there is a circular wait, meaning processes are waiting on each
other in a circular fashion, it indicates a deadlock.
o If a cycle is detected, then the processes involved in the cycle are in a deadlock
state.

Deadlock Recovery Techniques


Once a deadlock has been detected, the system must take action to recover from it. The recovery
process involves breaking the deadlock by either terminating processes, preempting resources,
or applying other strategies. Here are the common techniques used for deadlock recovery:

1. Process Termination

In this technique, deadlock is resolved by killing one or more processes involved in the deadlock.
Once a process is terminated, the resources it was holding are released, allowing other processes
to proceed.

Types of Process Termination:

• Abort All Processes: All processes involved in the deadlock are terminated.
o This is simple but may lead to the loss of a lot of work and data.
• Abort Processes One at a Time: Processes are terminated one by one until the deadlock
is resolved.
o This method tries to minimize the loss of resources or data by selectively
terminating processes.

2. Resource Preemption

In this method, the system forcefully takes resources away from processes to break the deadlock.
The resources are then allocated to the processes that need them to proceed.

How Preemption Works:

• A resource held by a process is preempted (taken away) and reassigned to a waiting


process.
• The preempted process will then be forced to wait for the resource to become available
again.

3. Rollback

This method involves rolling back processes to a safe state and then restarting them, usually from
a checkpoint where they were not part of the deadlock.

How Rollback Works:

• The system saves the state of processes periodically in the form of checkpoints.
• If a deadlock is detected, processes involved in the deadlock are rolled back to the last
checkpoint before the deadlock occurred.
• Once rolled back, these processes can restart and attempt to execute again, possibly in a
different order that avoids the deadlock.

4. Killing or Suspending Processes

Another method for recovering from a deadlock is to suspend processes that are part of the
deadlock cycle. The processes can then be resumed once the deadlock is cleared.

How Killing or Suspending Works:

• Processes involved in the deadlock are suspended, and their resources are released.
• After some time, when it’s safe to do so, the processes can be resumed.

Techniques for Deadlock Prevention

1. Eliminate Mutual Exclusion

• Mutual Exclusion means that a resource can only be held by one process at a time.
• In many systems, mutual exclusion is necessary for certain resources (e.g., printers, files),
but not all resources.
How to Prevent Deadlock by Eliminating Mutual Exclusion:

• Non-shared resources (e.g., CPU cycles, memory) can be accessed simultaneously by


multiple processes.
• For shareable resources, this condition may not be able to be eliminated because some
resources naturally require mutual exclusion.

2. Eliminate Hold and Wait

• Hold and Wait means a process is holding at least one resource while waiting to acquire
additional resources that are being held by other processes.

How to Prevent Deadlock by Eliminating Hold and Wait:

• Require processes to request all resources at once before execution. This is called the
All or Nothing Rule.
o If a process requests a resource, it must request all of the resources it will need
throughout its execution.
• Alternatively, processes can release resources they are holding before requesting
additional resources.

3. Eliminate No Preemption

• No Preemption means that once a resource is allocated to a process, it cannot be forcibly


taken from that process.

How to Prevent Deadlock by Eliminating No Preemption:

• If a process is holding some resources and needs additional resources that are being held
by other processes, the system can forcefully take (or preempt) resources from processes.
o The resources can then be reassigned to other processes that need them.

4. Eliminate Circular Wait

• Circular Wait occurs when a set of processes are waiting for resources in a circular chain.

How to Prevent Deadlock by Eliminating Circular Wait:

• Enforce an Ordering of Resources: This technique ensures that every process requests
resources in a predefined order. If all processes follow the same order when requesting
resources, then a circular wait cannot occur.
Difference between deadlock and starvation:
Aspect Deadlock Starvation
Definition A situation where two or more A situation where a process waits
processes are waiting indefinitely for indefinitely to acquire a resource
resources held by each other. because other higher-priority processes
keep getting it.
Cause Happens when all four conditions of Caused by unfair resource allocation or
deadlock are met: mutual exclusion, scheduling priorities.
hold and wait, no preemption, and
circular wait.
Processes Usually involves multiple processes in Often affects one or a few lower-
Involved a circular wait. priority processes.
System No process involved in the deadlock Some processes may continue, but one
Behavior can proceed. or more are permanently delayed.
Solution Requires deadlock detection, Resolved by ensuring fairness, aging
prevention, or recovery techniques. (gradually increasing priority), or round-
robin scheduling.
Resource Each process holds some resources and The starving process may not hold any
Held waits for others. resource; it just never gets a chance.

Difference between starvation and convoy effect:


Aspect Convoy Effect Starvation
1. Definition A slow process holds up faster A process waits indefinitely due to
processes, creating a “convoy” being continuously bypassed by others.
behind it.
2. Cause Occurs due to poor scheduling, Caused by strict priority scheduling or
especially FCFS. unfair resource allocation.
3. Fairness Technically fair (first-come-first- Unfair – low-priority processes are
served), but inefficient. ignored.
4. Process All processes wait behind one slow Some processes never get to execute.
Behavior process.
5. Resource One process monopolizes the Some processes are denied access to the
Use resource. resource.
6. System Reduces overall system throughput Causes incomplete tasks and
Impact and slows performance. inefficiency in resource use.
7. Example Long CPU-bound job blocks shorter Low-priority job never executes due to
Scenario I/O-bound jobs. constant arrival of higher-priority jobs.

8. Queue A visible queue forms behind the No obvious queue; process just keeps
Behavior slow process. getting skipped.
9. Resolution Use smarter schedulers like Round Use aging to gradually increase the
Robin, SJF, or multilevel queues. priority of long-waiting processes.
10. Detection Easier to observe due to noticeable Harder to detect without monitoring or
performance lag. logging tools.
Memory Management
What is Memory Management?
Memory management is the process by which an operating system (OS) controls and
coordinates computer memory, assigning blocks to various running programs to optimize overall
system performance.

In simpler terms, it's how a computer decides:

• Who gets memory?


• How much memory is given?
• When should memory be released or reused?

Main Functions of Memory Management:


1. Allocation – Giving memory space to programs or processes.
2. Deallocation – Releasing memory when it’s no longer needed.
3. Protection – Ensuring one process doesn’t access another’s memory.
4. Organization – Managing memory in a structured and efficient way.
5. Tracking – Keeping a record of which parts of memory are in use.

Why is Memory Management Important?

• To prevent system crashes or memory leaks.


• To support multitasking (running multiple programs at once).
• To improve speed and efficiency of programs.
• To allow running programs that need more memory than physically available (via
virtual memory).

Example Analogy:
Think of memory as a hotel, and memory management as the reception desk:

• It keeps track of who checks in (allocation),


• Who checks out (deallocation),
• Makes sure no guest enters another's room (protection),
• And decides which rooms are available (tracking).

Memory Management Techniques:


1. Contiguous Memory Allocation
• Fixed Partitioning
• Variable Partitioning
• Single Partitioning
• Multi Partitioning
2. Non-contiguous Memory Allocation
• Paging
• Segmentation
• Virtual Memory
• Swapping
• Buddy System
• Garbage Collection

Contiguous Memory Allocation


Memory is assigned in one single continuous block to each process.

How It Works:

• The memory is divided into fixed or variable-sized partitions.


• Each process gets a partition large enough to fit its entire data.
• Every part of a process is stored together (contiguously).

Types:

• Fixed Partitioning:
o Memory is divided into equal-size parts.
• Variable Partitioning:
o Partitions are sized based on the process.
• Single Partition Allocation:
o Whole memory is allocated to one process.
o Simple but inefficient for multiprogramming.

• Multiple Partition Allocation:


o Memory is divided into several fixed or variable-sized partitions.
o Multiple processes can reside in memory simultaneously.

Pros:

• Simple to implement.
• Fast access to memory.

Cons:

• External Fragmentation: Small unusable gaps between memory blocks.


• Hard to resize processes dynamically.
Non-Contiguous Memory Allocation:

Non-contiguous memory allocation is a memory management technique in which a program's


memory is divided and stored in different parts of the physical memory, not in one single
continuous block.

This means the memory assigned to a process can be scattered across various locations in RAM,
instead of being placed together in one block.

Paging
Memory is divided into small fixed-size blocks called pages (for processes) and frames (for
physical memory).
🔹How It Works:

• Each process is divided into pages.


• Pages can be placed in any available frame in memory.
• A page table keeps track of which page is in which frame.

🔹 Pros:

• Eliminates external fragmentation.


• Allows efficient and flexible memory use.

🔹 Cons:

• Can lead to internal fragmentation (unused space within frames).


• Page table management adds overhead.

Segmentation
Memory is divided into variable-size segments based on logical divisions like code, data, stack,
etc.
🔹 How It Works:

• Each segment has a name, length, and base address.


• The segment table keeps track of all segments.

🔹 Pros:

• Matches how programmers logically divide programs.


• Easy to apply protection to individual segments.

🔹 Cons:

• Can suffer from external fragmentation.


• More complex than paging.

Virtual Memory
A memory management technique that uses a portion of the hard disk as an extension of RAM.

🔹 How It Works:

• Only parts of a program are loaded into RAM.


• When more space is needed, inactive pages are moved to the swap space on disk.
• Managed using paging or segmentation.

🔹 Pros:

• Allows programs larger than physical RAM.


• Supports multitasking better.

🔹 Cons:

• Accessing disk is slower than RAM.


• Can cause thrashing (frequent swapping) if overloaded.
Swapping
A technique to move processes in and out of RAM to manage memory more efficiently.

🔹 How It Works:

• When RAM is full, the OS swaps a process to disk.


• Later, it can be brought back when needed.

🔹 Pros:

• Increases CPU utilization and multiprogramming.

🔹 Cons:

• Slow performance due to disk I/O.


• Causes delays if done frequently.

Buddy System
A method of dividing memory into blocks of size 2ⁿ and splitting or combining them as needed.
🔹 How It Works:

• When a block is free, it's split into two "buddies".


• If both buddies are free, they are merged again.

🔹 Pros:

• Easy to allocate and free memory.


• Reduces external fragmentation.

🔹 Cons:

• Can cause internal fragmentation.

Memory Pooling
Pre-allocating a "pool" of memory blocks and reusing them for repetitive tasks.

🔹 How It Works:

• A fixed number of memory blocks are allocated at once.


• Blocks are reused instead of allocating new ones every time.

🔹 Pros:

• Very fast allocation.


• Reduces fragmentation and overhead.

🔹 Used In:

Real-time systems, embedded systems, and games.


Garbage Collection (in programming languages like Java, Python)
Automatic process that detects and frees unused memory.

🔹 How It Works:

• Finds memory objects that are no longer in use.


• Reclaims and makes the space available again.

🔹 Techniques:

• Reference Counting
• Mark and Sweep
• Generational Garbage Collection

🔹 Pros:

• Reduces memory leaks.


• Simplifies programming.

🔹 Cons:

• Adds runtime overhead.


• Can affect performance if not optimized.

You might also like