Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 04

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Operating System Important Notes

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.

4. Short-term Scheduling (CPU Scheduling)


Definition: Short-term scheduling focuses on selecting one of the processes in the ready queue
to run next. This decision is made frequently to ensure efficient CPU use.

Jamia Polytechnic Akkalkuwa 1 Prepared by: Sayyed Waliullah


Operating System Important Notes

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.

Types of scheduling algorithms

1. First Come First Serve (FCFS)


- Definition: Processes are executed in the order they arrive. It is non-preemptive, meaning once
a process starts, it runs until completion.
- Example: Like standing in a queue; the first person in line gets served first.

2. Shortest Job First (SJF)


- Definition: Executes the process with the shortest burst time first. It can be non-preemptive or
preemptive (Shortest Remaining Time First).
- Example: Tasks with the shortest duration are given priority.

3. Round Robin (RR)


- Definition: Each process is assigned a fixed time slice (quantum). If it doesn't finish within the
time, it is preempted and moved to the end of the queue.
- Example: Like giving everyone equal turns in a game.

4. Priority Scheduling

Jamia Polytechnic Akkalkuwa 2 Prepared by: Sayyed Waliullah


Operating System Important Notes

- 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.

5. Multilevel Queue Scheduling


- Definition: Processes are classified into different queues based on their priority or type, with
each queue having its own scheduling algorithm (e.g., foreground queue may use RR,
background queue may use FCFS).
- Example: Separating tasks into high-priority (urgent) and low-priority (background) categories.

6. Multilevel Feedback Queue


- Definition: Similar to Multilevel Queue but allows processes to move between queues based on
their execution history, giving a flexible and adaptive scheduling approach.
- Example: A task might move to a lower priority queue if it consumes too much CPU time.

7. Shortest Remaining Time First (SRTF)


- Definition: A preemptive version of SJF, where the process with the shortest remaining
execution time is chosen next.
- Example: If a new process arrives with a shorter burst time than the current running process,
the current one is preempted.

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.

System Models for Deadlock

In operating systems, resources can be categorized into two types:


- Preempt able Resources: Can be taken away from a process without causing any issues
(e.g., CPU).
- Non-preempt able Resources: Cannot be taken away without potentially causing failure
(e.g., files, printers).

Necessary Conditions Leading to Deadlock


For deadlock to occur, the following four necessary conditions must hold simultaneously:

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.

Jamia Polytechnic Akkalkuwa 3 Prepared by: Sayyed Waliullah


Operating System Important Notes

- 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.

Deadlock Example Using System Models


Consider a system with two processes (P_1) and (P_2), and two resources (R_1) (printer) and
(R_2) (file).

1. (P_1) holds (R_1) (printer) and waits for (R_2) (file).


2. (P_2) holds (R_2) (file) and waits for (R_1) (printer).

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.

Deadlock Prevention and Avoidance:

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

Approach: Ensure that resources are sharable wherever possible.


- Example: Read-only files can be accessed by multiple processes simultaneously.

- Limitation: Some resources, like printers or other hardware devices, inherently require mutual
exclusion, so this condition cannot always be violated.

b) Hold and Wait

- Approach: Require processes to request all necessary resources at the start (before execution) or force
processes to release all resources before requesting new ones.

Jamia Polytechnic Akkalkuwa 4 Prepared by: Sayyed Waliullah


Operating System Important Notes

- 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:

- The system keeps track of:

- The maximum resources each process may need.

- The currently allocated resources to each process.

- The available resources.

- 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.

Jamia Polytechnic Akkalkuwa 5 Prepared by: Sayyed Waliullah

You might also like