You are on page 1of 34

CPU Scheduling

Presented by:

Muhammad Ameen Muhammad Furqan

CPU Scheduling ?
In the CPU scheduling the Operating System decide that which process should be executed first on the basis of different algorithms.

Why CPU Scheduling Is Required?

The objective of multiprogramming is to have some process running at all times to maximize CPU utilization.
Almost all computer resources are scheduled before used.

CPU and I/O Burst Cycle


The

execution of a process consists of a cycle of CPU and I/O wait.


A process begins with a CPU burst, followed by

an I/O burst, followed by another CPU burst and so on. The last CPU burst will end will a system request to terminate the execution.
The

CPU burst durations vary from process to process and computer to computer.
An I/O bound program has many very short CPU

bursts. A CPU bound program might have a few very long CPU bursts.

CPU Scheduler
Whenever, the CPU becomes idle, the OS must select one of the processes in the ready-queue to be executed. The selection process is carried out the CPU scheduler. The CPU scheduler select a process from the ready queue and allocates the CPU to that process.

Types of Scheduling
There are four types of scheduling that an OS has to perform:

Long Term scheduling Medium-Term Scheduling Short-Term Scheduling I/O Scheduling

Long Term scheduling

The long term scheduling determines which programs are admitted to the system for processing. It controls the level of multiprogramming. Long term scheduling is performed when a new process is created. The criteria used for long-term scheduling may include first-come-first serve, priority, expected execution time, and I/O requirements.

Medium-Term Scheduling

The medium-term scheduling is a part of swapping function. This is a decision to add a process to those that are at least partially in main memory and therefore available for execution. The swapping- In decision is made on the need to manage the degree of multiprogramming and the memory requirements of the swapped-out process.

Short-Term Scheduling
A decision of which ready process to execute

next is made in short-term scheduling.

I/O Scheduling

The decision as to which processs pending I/O requests shall be handled by the available I/O device is made in I/O scheduling.

Scheduling Criteria
CPU utilization : keep the CPU as busy as possible (from 0% to 100%) Throughput : # of processes that complete their execution per time unit Turnaround time : Amount of time to execute a particular Process Waiting time : Amount of time a process has been waiting in the ready queue Response time: Amount of time it takes from when a request was submitted until the first response is produced

Optimization Criteria
Max CPU utilization Max throughput Min turnaround time Min waiting time Min Response time

Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes in the ready queue is to be allocated the CPU. There are many different CPU scheduling algorithms, which we will discuss now.

Scheduling Algorithms

First-Come First-Served (FCFS) Scheduling Shortest-Job First Scheduling (SJF) Priority Scheduling Round-Robin Scheduling

First-Come First-Served (FCFS) Scheduling


The process that requests the CPU first is allocated the CPU first. It is no preemptive algorithm. Can easily be implemented with a FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue. When CPU is free, it is allocated to the process at the head of the queue.

Advantages Very simple Disadvantages

Long average and worst-case waiting times Poor dynamic behavior (convoy effect short process behind long process)

Example:
Process P1 Burst Time 24

P2
P3

3
3

Suppose that the processes arrive in the order P1, P2,P3.The Gantt Chart for the schedule is:
P1 24 P2 27 P3 30

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17

Cont
Suppose that the processes arrive in the order P2, P3, P1. The Gantt chart for the schedule is:
P2 0 3 P3 6 P1 30

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3

Shortest-Job First Scheduling (SJF)


This algorithm associates with each process the length of its next CPU burst. When the CPU is available, it is assigned the process that has the smallest next CPU burst. It is a non-preemptive policy.

Cont
If a new process arrives with CPU burst length less than remaining time of current executing process, preempt the currently executing process and allocate the CPU to the new process.

Cont
Advantages:

Minimizes
Problems:

average waiting times.

How to determine length of next CPU

burst? Problem: starvation of jobs with long CPU bursts.

Priority Scheduling
In priority scheduling, a priority (an integer) is associated with each process. Priorities can be assigned either externally or internally. The CPU is allocated to the process with the highest priority (smallest integer means highest priority).

Priority scheduling can be:


Preemptive No preemptive

Problem Starvation or indefinite blocking low priority processes may never execute. Solution
Aging as time progresses increase the

priority of the process.

Round-Robin Scheduling
Each process gets a small unit of CPU time (called time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

Ready queue is treated as a circular queue.

CPU scheduler goes around the ready queue, allocating the CPU to each process for a time interval of 1 time quantum.

You might also like