Unit 04
Unit 04
Unit 04
Unit -04
Scheduling Definition
Scheduling in an operating system is the process of deciding which tasks or processes should be
executed by the CPU at any given time. It ensures the efficient allocation of CPU time and
resources, allowing multiple processes to run smoothly without conflict. Scheduling determines
the order in which processes access the CPU and how long each process gets to run, balancing
system performance, fairness, and response time.
Types of Scheduling
Scheduling can be classified into different types based on how the CPU is allocated to processes.
These types ensure that the CPU is shared efficiently among multiple tasks, balancing system
performance and user needs.
1. Preemptive Scheduling
Definition: Preemptive scheduling allows the operating system to interrupt a running process
and allocate the CPU to another process with higher priority or an urgent need. The interrupted
process is then placed back into the ready queue.
Purpose: To ensure that important or time-sensitive tasks are given immediate attention by the
CPU.
Example: The Round Robin (RR) algorithm, which assigns a fixed time slice to each process,
and if the process isn’t finished, it’s preempted and put back into the queue for another turn.
2. Non-preemptive Scheduling
Definition: In non-preemptive scheduling, once a process starts executing, it runs to completion
or until it voluntarily releases the CPU (such as waiting for I/O). The CPU cannot be taken away
by another process during execution.
Purpose: Simplifies process execution by avoiding interruptions, making it easier to implement
but potentially leading to longer wait times for other processes.
Example: The First Come First Serve (FCFS) algorithm, where processes are executed in the
order they arrive, and no process is interrupted until it finishes.
3. Long-term Scheduling
Definition: Long-term scheduling determines which processes are admitted into the system from
the job pool and placed in the ready queue. This type of scheduling controls the degree of
multiprogramming (how many processes can be in memory at the same time).
Purpose: To balance the system's workload by deciding which mix of CPU-bound and I/O-
bound processes should run, ensuring efficient CPU and resource utilization.
Purpose: To optimize the immediate use of the CPU by determining which process should
execute next based on factors like priority, time left, or fairness.
5. Medium-term Scheduling
Definition: Medium-term scheduling manages the swapping of processes between the main
memory and disk (swapping). It temporarily removes processes from the main memory to reduce
the overall load and later brings them back for continued execution.
Purpose: To improve memory management and system performance, especially when system
resources are overcommitted.
These types of scheduling are essential for ensuring that the system remains responsive, efficient,
and fair in allocating CPU resources to various processes.
Scheduling Objectives
1. Maximizing CPU Utilization: Ensure the CPU is kept busy as much as possible by
minimizing idle time.
2. Maximizing Throughput: Increase the number of processes that complete their execution per
unit of time.
3. Minimizing Turnaround Time: Reduce the total time from the submission of a process to its
completion.
4. Minimizing Waiting Time: Decrease the amount of time a process spends waiting in the
ready queue before getting CPU time.
5. Minimizing Response Time: Ensure that the system quickly responds to user commands,
particularly in interactive systems.
6. Fairness: Make sure every process gets an appropriate share of CPU time, avoiding
favoritism.
7. Avoiding Starvation: Ensure that no process waits indefinitely for CPU resources.
4. Priority Scheduling
- Definition: Processes are scheduled based on priority. Higher-priority processes are executed
first. It can be preemptive or non-preemptive.
- Example: Urgent tasks in a to-do list are tackled first.
Deadlock
A deadlock is a situation in operating systems where two or more processes are unable to
proceed because each is waiting for the other to release resources. This creates a cycle of
dependency, causing the processes to be stuck indefinitely.
1. Mutual Exclusion
- Definition: At least one resource must be held in a non-sharable mode. Only one process can
use the resource at a time, and if another process requests it, the requesting process must wait.
- Example: A printer that can only print one document at a time.
2. Hold and Wait
- Definition: A process holding at least one resource is waiting to acquire additional resources
that are currently being held by other processes.
- Example: A process holds a file and waits for a printer, while another process holds the printer
and waits for the file.
3. No Preemption
- Definition: Resources cannot be forcibly taken away from a process once they have been
allocated. They must be released voluntarily by the process holding them.
- Example: A process holds a printer and won't release it until it finishes printing, even if
another process needs it.
4. Circular Wait
- Definition: A set of processes are waiting on each other in a circular chain. Each process is
waiting for a resource held by the next process in the chain.
- Example: Process A waits for Process B's resource, Process B waits for Process C's resource,
and Process C waits for Process A's resource.
This creates a circular wait, and all four conditions (Mutual Exclusion, Hold and Wait, No
Preemption, Circular Wait) are satisfied, leading to a deadlock. Neither process can proceed
unless the other releases its resource, which won't happen.
To deal with deadlocks, two common strategies are used: Deadlock Prevention and Deadlock
Avoidance. Both aim to ensure that deadlocks do not occur in a system, but they differ in how they
approach the problem.
Deadlock Prevention: Deadlock prevention focuses on ensuring that at least one of the four necessary
conditions for deadlock (Mutual Exclusion, Hold and Wait, No Preemption, Circular Wait) does not hold.
By breaking one or more of these conditions, deadlocks can be prevented.
a) Mutual Exclusion
- Limitation: Some resources, like printers or other hardware devices, inherently require mutual
exclusion, so this condition cannot always be violated.
- Approach: Require processes to request all necessary resources at the start (before execution) or force
processes to release all resources before requesting new ones.
- Example: A process cannot hold a printer while waiting for a file; it must release the printer and re-
request both resources.
- Limitation: This can lead to low resource utilization and possible process starvation as processes may
hold resources longer than necessary.
c) No Preemption
- Approach: Allow preemption of resources. If a process holding a resource requests another resource
that is not available, it must release the currently held resources and re-request them later.
- Example: If a process is waiting for a printer but already holds a file, the system can force the process
to release the file until both resources are available.
- Limitation: Preemption is not always possible, especially with non-preempt able resources like
hardware devices.
d) Circular Wait
- Approach: Impose a strict ordering on resource acquisition so that processes can only request
resources in a specific order, preventing circular wait.
- Example: Processes must request resources in increasing order of their numbering (e.g., a process
holding resource \(R_1\) can only request \(R_2\), but not the other way around).
- Limitation: This method can be difficult to implement efficiently if the resource requests are
unpredictable.
2. Deadlock Avoidance
Deadlock avoidance takes a more dynamic approach by carefully allocating resources to ensure that the
system never enters an unsafe state where deadlock can occur. One common technique used for
deadlock avoidance is Banker's Algorithm.
Banker's Algorithm
- Definition: Named after a banking analogy, this algorithm ensures that a system will not enter an
unsafe state by checking every resource request against the system’s current state.
- Working:
- When a process requests a resource, the algorithm checks if granting the resource will leave the
system in a safe state. If so, the resource is allocated; if not, the process must wait.
- Safe State: A state is considered safe if there is a sequence of processes that can complete without
leading to deadlock. In other words, even if all processes request their maximum resources, the system
can allocate resources in such a way that all processes can finish.