You are on page 1of 33

Operating System

BY QURAT UL AIN
CPU Scheduling

 CPU Scheduling is a process of determining which process will own


CPU for execution while another process is on hold. The main task of
CPU scheduling is to make sure that whenever the CPU remains idle,
the OS at least select one of the processes available in the ready queue
for execution
 In Multiprogramming systems, the Operating system schedules the
processes on the CPU to have the maximum utilization of it and this
procedure is called CPU scheduling. The Operating System uses
various scheduling algorithm to schedule the processes
CPU Scheduling
CPU Scheduling
CPU Scheduling

Preemptive Scheduling
 In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
Sometimes it is important to run a task with a higher priority before another lower
priority task, even if the lower priority task is still running. The lower priority task holds
for some time and resumes when the higher priority task finishes its execution.
Non-Preemptive Scheduling
 In this type of scheduling method, the CPU has been allocated to a specific process. The
process that keeps the CPU busy will release the CPU either by switching context or
terminating. It is the only method that can be used for various hardware platforms.
That’s because it doesn’t need special hardware (for example, a timer) like preemptive
scheduling.
CPU Scheduling

 Context switch
 A context switch is a procedure that a computer's CPU (central processing unit)
follows to change from one task (or process) to another while ensuring that the
tasks do not conflict. Effective context switching is critical if a computer is to
provide user-friendly multitasking
What is saved in the Process Control Block?

 The Operating system maintains a process control block during the


lifetime of the process. The Process control block is deleted when
the process is terminated or killed. There is the following
information which is saved in the process control block and is
changing with the state of the process.
Why do we need Scheduling?

In Multiprogramming, if the long term scheduler picks more I/O bound processes
then most of the time, the CPU remains idol. The task of Operating system is to
optimize the utilization of resources. If most of the running processes change
their state from running to waiting then there may always be a possibility of
deadlock in the system. Hence to reduce this overhead, the OS needs to schedule
the jobs to get the optimal utilization of CPU and to avoid the possibility to
deadlock.
The Purpose of a Scheduling algorithm

 Maximum CPU utilization


 Fare allocation of CPU
 Maximum throughput
 Minimum turnaround time
 Minimum waiting time
 Minimum response time
There are the following algorithms which can be used to
schedule the jobs
Algorithms

 First Come First Serve


 Round Robin
 Shortest Job First
 Shortest remaining time first
 Priority based scheduling
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart P1
0 2
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart P1 P2
0 2 4
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart P1 P2
0 2 4 5
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart P1 P2 P3
0 2 4 5 8
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2
P2 1 2
P3 5 3
P4 6 4

Gantt
Chart P1 P2 P3 P4
0 2 4 5 8 12
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2 2
P2 1 2 4
P3 5 3 8
P4 6 4 12

Gantt
Chart P1 P2 P3 P4
0 2 4 5 8 12
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2 2 2
P2 1 2 4 3
P3 5 3 8 3
P4 6 4 12 6

Gantt
Chart P1 P2 P3 P4
TAT= CT-AT
0 2 4 5 8 12
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2 2 2 0
P2 1 2 4 3 1
P3 5 3 8 3 0
P4 6 4 12 6 2

Gantt
Chart P1 P2 P3 P4
WT = TAT-ET
0 2 4 5 8 12
First Come First Serve

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 0 2 2 2 0 0
P2 1 2 4 3 1 1
P3 5 3 8 3 0 0
P4 6 4 12 6 2 2

Gantt
Chart P1 P2 P3 P4
WT = TAT-ET
0 2 4 5 8 12
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart
0
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart P3
0 1 3
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart P3 P1
0 1 3 6
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart P3 P1
0 1 3 6
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart P3 P1 P2
0 1 3 6 10
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3
P2 2 4
P3 1 2
P4 4 4

Gantt
Chart P3 P1 P2 P4
0 1 3 6 10 14
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3 6
P2 2 4 10
P3 1 2 3
P4 4 4 14

Gantt
Chart P3 P1 P2 P4
0 1 3 6 10 14
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3 6 5
P2 2 4 10 8
P3 1 2 3 2
P4 4 4 14 10

Gantt
Chart P3 P1 P2 P4
TAT= CT-AT
0 1 3 6 10 14
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3 6 5 2
P2 2 4 10 8 4
P3 1 2 3 2 0
P4 4 4 14 10 6

Gantt
Chart P3 P1 P2 P4
WT = TAT-ET
0 1 3 6 10 14
Shortest Job Fist (SJF)

Process Arrival Time Execution Completion Turn Waiting Time Response


No Time Time Around Time
Time
P1 1 3 6 5 2 2
P2 2 4 10 8 4 4
P3 1 2 3 2 0 0
P4 4 4 14 10 6 6

Gantt
Chart P3 P1 P2 P4
0 1 3 6 10 14
SJF Example

Process Arrival Time Execution


No Time
P1 1 5
P2 3 7
P3 2 7
P4 4 7

You might also like