SlideShare a Scribd company logo
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Operating System
KCS – 401
CPU Scheduling
Dr. Pankaj Kumar
Associate Professor – CSE
SRMGPC Lucknow
Outline of the Lecture
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
Algorithm Evaluation
Basic Concept
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Maximum CPU utilization obtained with multiprogramming
CPU–I/O Burst Cycle – Process execution consists of a cycle of
CPU execution and I/O wait
CPU burst followed by I/O burst
CPU burst distribution is of main concern
Burst Time/Execution Time: It is a time required by the process to
complete execution. It is also called running time.
Arrival Time: when a process enters in a ready state.
CPU Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
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.
The selection process will be carried out by the CPU scheduler. It selects one of the processes in
memory that are ready for execution.
CPU Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Types of 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 Scheduler
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Short-term scheduler selects from among the processes in ready queue, and allocates the
CPU to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
Scheduling under 1 and 4 is nonpreemptive
All other scheduling is preemptive
Consider access to shared data
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
Dispatcher
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Dispatcher module gives control of the CPU to the process selected by the short-term scheduler;
this involves:
switching context
switching to user mode
jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
CPU utilization: CPU utilization is the main task in which the
operating system needs to make sure that CPU remains as busy as
possible. It can range from 0 to 100 percent.
Throughput: The number of processes that finish their execution
per unit time is known Throughput. So, when the CPU is busy
executing the process, at that time, work is being done, and the
work completed per unit time is called Throughput.
Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue.
Response time: It is an amount to time in which the request was submitted until the first
response is produced.
Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the
calculation of the total time spent waiting to get into the memory, waiting in the queue and,
executing on the CPU. The period between the time of process submission to the completion time
is the turnaround time.
Scheduling Criteria
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Completion Time: Time at which process completes its
execution.
Turn Around Time: Time Difference between
completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn
around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Waiting time = Start time - Arrival time
Scheduling Type
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Types of CPU scheduling Algorithm
There are mainly six types of process scheduling algorithms
1. First Come First Serve (FCFS)
2. Shortest-Job-First (SJF) Scheduling
3. Shortest Remaining Time
4. Priority Scheduling
5. Round Robin Scheduling
6. Multilevel Queue Scheduling
First Come First Serve (FCFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
It is the easiest and most simple CPU scheduling algorithm. In this type of algorithm, the process
which requests the CPU gets the CPU allocation first. This scheduling method can be managed
with a FIFO queue.
As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of
the queue. So, when CPU becomes free, it should be assigned to the process at the beginning of
the queue.
Characteristics of FCFS method:
• It offers non-preemptive and pre-emptive scheduling algorithm.
• Jobs are always executed on a first-come, first-serve basis
• It is easy to implement and use.
• However, this method is poor in performance, and the general wait time is quite high.
First Come First Serve (FCFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
P P P
1 2 3
0 24 30
27
First Come First Serve (FCFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Suppose that the processes arrive in the order:
P2 , P3 , P1
The Gantt chart for the schedule is:
Waiting time for P1 = 6; P2 = 0; P3 = 3
Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
P1
0 3 6 30
P2
P3
Convoy effect - short process behind long process
Consider one CPU-bound and many I/O-bound processes
First Come First Serve (FCFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Burst time Arrival time
P1 6 2
P2 3 5
P3 8 1
P4 3 0
P5 4 4
Waiting time for P1 = 11-2 = 9
Waiting time for P2 = 21-5 = 16
Waiting time for P3 = 3-1 = 2
Waiting time for P4 = 0
Waiting time for P5 = 17-4 = 13
Average waiting time: (9 + 16 + 2+0+13)/5 = 40/5 =8
Turn Around Time = P1 = 17-2 = 15
Turn Around Time = P2 = 23-5 = 18
Turn Around Time = P3 = 11-1 = 10
Turn Around Time = P4 = 3-0 = 3
Turn Around Time = P5 = 21-4 = 17
Average Turn Around Time : (15 + 18 + 10+3+17)/5 = 63/5 =12.6
First Come First Serve (FCFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Id Arrival time Burst time
P1 3 4
P2 5 3
P3 0 2
P4 5 1
P5 4 3
Turn Around Time = P1 = 7-3 = 4
Turn Around Time = P2 = 13-5 = 8
Turn Around Time = P3 = 2-0 = 2
Turn Around Time = P4 = 14-5 = 9
Turn Around Time = P5 = 10-4 = 6
Average Turn Around time = (4 + 8 + 2 + 9 + 6) / 5 = 29 / 5 = 5.8
Waiting time for P1 = 3 – 3 = 0
Waiting time for P2 = 10-5 = 5
Waiting time for P3 = 0
Waiting time for P4 = 13-5 = 8
Waiting time for P5 = 7-4 = 3
Average waiting time: (0 + 5 + 0 + 8 + 3)/5 = 16/5 =3.2
Shortest Job First Serve (SJFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
1. It is a Greedy Algorithm.
2. Sort all the process according to the arrival time.
3. Then select that process which has minimum arrival time and minimum Burst time.
4. After completion of process make a pool of process which after till the completion of
previous process and select that process among the pool which is having minimum Burst
time.
• It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of ageing.
• It is practically infeasible as Operating System may not know burst time and therefore may
not sort them. While it is not possible to predict execution time, several methods can be used
to estimate the execution time for a job, such as a weighted average of previous execution
times. SJF can be used in specialized environments where accurate estimates of running time
are available.
Shortest Job First Serve (SJFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Burst Time
P1 6
P2 8
P3 7
P4 3
SJF scheduling chart
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
P3
0 3 24
P4
P1
16
9
P2
Shortest Job First Serve (SJFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Now we add the concepts of varying arrival times and preemption to the
analysis
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec
Preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
Shortest Job First Serve (SJFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
set of 5 processes whose arrival time and burst
time are given below-
•Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
•Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit
Shortest Job First Serve (SJFS)
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Advantages-
• SJFS is optimal and guarantees the minimum average waiting time.
• It provides a standard for other algorithms since no other algorithm performs better than it.
Disadvantages-
• It can not be implemented practically since burst time of the processes can not be known in
advance.
• It leads to starvation for processes with larger burst time.
• Priorities can not be set for the processes.
• Processes with larger burst time have poor response time.
Priority Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Priority Scheduling is a method of scheduling processes that is based on priority. In this
algorithm, the scheduler selects the tasks to work as per the priority.
The processes with higher priority should be carried out first, whereas jobs with equal priorities
are carried out on a round-robin or FCFS basis. Priority depends upon memory requirements,
time requirements, etc.
Characteristics of Priority Scheduling
• It used in Operating systems for performing batch processes.
• If two jobs having the same priority are READY, it works on a FIRST COME, FIRST
SERVED basis.
• In priority scheduling, a number is assigned to each process that indicates its priority level.
• Lower the number, higher is the priority.
• In this type of scheduling algorithm, if a newer process arrives, that is having a higher priority
than the currently running process, then the currently running process is preempted.
Priority Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Average waiting time = (6+0+16+18+1)/5 = 8.2 msec
Priority Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Priority Burst time Arrival time
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Average Waiting time = (0+11+0+5+2)/5 = 18/5= 3.6
Priority Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Advantages
Processes are executed on the basis of priority so high priority does not need to wait for long
which saves time
Suitable for applications with fluctuating time and resource requirements.
Disadvantages of priority scheduling
Starvation – low priority processes may never execute
Solution  Aging – as time progresses increase the priority of the process
Round-Robin Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
• Round robin is a pre-emptive algorithm
• The CPU is shifted to the next process after fixed interval time, which is called time
quantum/time slice.
• The process that is preempted is added to the end of the queue.
• Round robin is a hybrid model which is clock-driven
• Time slice should be minimum, which is assigned for a specific task that needs to be
processed. However, it may differ OS to OS.
• It is a real time algorithm which responds to the event within a specific time limit.
• Round robin is one of the oldest, fairest, and easiest algorithm.
• Widely used scheduling method in traditional OS.
Round-Robin Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process Queue Burst time
P1 4
P2 3
P3 5
quantum/time slice = 2
Waiting Time
P1= 8-4= 4
P2= 9-3= 6
P3= 12-5= 7
Turnaround Time
P1= 8
P2= 9
P3= 12
Average Waiting time = (4+6+7)/3 = 5.66
Round-Robin Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Process ID Arrival Time Burst Time
1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
Process
ID
Completion
Time
Turn
Around
Time
Waiting
Time
1 17 17 12
2 23 22 16
3 11 9 6
4 12 9 8
5 24 20 15
6 21 15 11
Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units
P1-P2-P3-P4-P5-P1-P6-P2-P5
Round-Robin Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Disadvantages
• If slicing time of OS is low, the processor output will be reduced.
• This method spends more time on context switching
• Its performance heavily depends on time quantum.
• Priorities cannot be set for the processes.
• Round-robin scheduling doesn't give special priority to more important tasks.
• Lower time quantum results in higher the context switching overhead in the system.
• Finding a correct time quantum is a quite difficult task in this system.
Multilevel Queue Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Multilevel queue scheduling algorithm partitions the ready
queue into several separate queues.
A common division is made between foreground (or
interactive) processes and background (or batch) processes.
These two types of processes have different response-time
requirements, and so might have different scheduling needs. In
addition, foreground processes may have priority over
background processes.
Multilevel queue scheduling has the following characteristics:
• Each queue has its own scheduling algorithm
• Processes are divided into different queues based on their
process type, memory size and process priority.
Multilevel Queue Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
• System Process The Operating system itself has its own process to
run and is termed as System Process.
• Interactive Process The Interactive Process is a process in which
there should be the same kind of interaction (e.g. online game ).
• Batch Processes Batch processing is basically a technique in the
Operating system that collects the programs and data together in
the form of the batch before the processing starts.
• Student Process The system process always gets the highest
priority while the student processes always get the lowest priority.
For System Processes: First Come First Serve(FCFS) Scheduling.
For Interactive Processes: Shortest Job First (SJF) Scheduling.
For Batch Processes: Round Robin(RR) Scheduling
For Student Processes: Priority Scheduling
Multilevel Feedback Queue Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
This Scheduling is like Multilevel Queue (MLQ) Scheduling but in this
process can move between the queues. Multilevel Feedback Queue
Scheduling (MLFQ) keep analyzing the behavior (time of execution) of
processes and according to which it changes its priority.
Three queues:
Q1 – RR with time quantum 8 milliseconds
Q2 – RR time quantum 16 milliseconds
Q3 – FCFS
Scheduling
A new job enters queue Q0 which is served FCFS
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to queue Q1
At Q1 job is again served FCFS and receives 16 additional
milliseconds
If it still does not complete, it is preempted and moved to
queue Q2
Multiprocessor Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Multiple processor scheduling or multiprocessor scheduling focuses on designing the scheduling
function for the system which is consist of ‘more than one processor’. With multiple processors
in the system, the load sharing becomes feasible but it makes scheduling more complex.
CPU scheduling more complex when multiple CPUs are available
Homogeneous processors within a multiprocessor
Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating
the need for data sharing
Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common
ready queue, or each has its own private queue of ready processes
Multiprocessor Scheduling
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Processor affinity – process has affinity for processor on which it is currently running
1. Soft Affinity – When an operating system has a policy of attempting to keep a process
running on the same processor but not guaranteeing it will do so, this situation is called soft
affinity.
2. Hard Affinity – Hard Affinity allows a process to specify a subset of processors on which it
may run.
If SMP, need to keep all CPUs loaded for efficiency
Load balancing attempts to keep workload evenly distributed
Push migration – periodic task checks load on each processor, and if found pushes task from
overloaded CPU to other CPUs
Pull migration – idle processors pulls waiting task from busy processor

More Related Content

DOC
Operating Systems Third Unit - Fourth Semester - Engineering
PPTX
Operating Systems CPU Scheduling and its Algorithms
PPT
Process management in os
PPTX
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
PPTX
Preemptive process example.pptx
PPT
Operating System 5
PPTX
OS ASSIGNMENT.pptx
PPT
Process Scheduling in Ope Spptystems rating
Operating Systems Third Unit - Fourth Semester - Engineering
Operating Systems CPU Scheduling and its Algorithms
Process management in os
CPU SCHEDULING ALGORITHMS-FCFS,SJF,RR.pptx
Preemptive process example.pptx
Operating System 5
OS ASSIGNMENT.pptx
Process Scheduling in Ope Spptystems rating

Similar to Operating System Sheduling (20)

PPT
ch6- CPU scheduling https://www.slideshare.net/slideshow/operating-system-18-...
PDF
Process Scheduling Algorithms.pdf
PPT
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
PDF
CPU Scheduling Part-II.pdf
PPT
Operating System Scheduling
PPTX
Scheduling algo(by HJ)
PDF
Do2644844490
PPT
Lecture 5, 6 and 7 cpu scheduling
PPT
Ch6
 
PPTX
2_CPU Scheduling (2)beautifulgameyt.pptx
PPT
Cpu Scheduling Galvin
PPTX
CPU Scheduling
PPTX
CPU scheduling
PPTX
DISEÑO DE MEMORIA EN SISTEMAS DE INFORMACION DIGITALES
PPT
chapter 5 CPU scheduling.ppt
PPT
PPT
Ch05 cpu-scheduling
PPT
Cpu scheduling(suresh)
ch6- CPU scheduling https://www.slideshare.net/slideshow/operating-system-18-...
Process Scheduling Algorithms.pdf
CPU SCHEDULING IN OPERATING SYSTEMS IN DETAILED
CPU Scheduling Part-II.pdf
Operating System Scheduling
Scheduling algo(by HJ)
Do2644844490
Lecture 5, 6 and 7 cpu scheduling
Ch6
 
2_CPU Scheduling (2)beautifulgameyt.pptx
Cpu Scheduling Galvin
CPU Scheduling
CPU scheduling
DISEÑO DE MEMORIA EN SISTEMAS DE INFORMACION DIGITALES
chapter 5 CPU scheduling.ppt
Ch05 cpu-scheduling
Cpu scheduling(suresh)
Ad

More from Shri Ram Swaroop Memorial College of Engineering & Management (20)

PDF
Operating System: Process and synchronization
PDF
Operating System: interprocess Communication
Operating System: Process and synchronization
Operating System: interprocess Communication
Ad

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Lesson notes of climatology university.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
Complications of Minimal Access Surgery at WLH
Lesson notes of climatology university.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pre independence Education in Inndia.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Sports Quiz easy sports quiz sports quiz
102 student loan defaulters named and shamed – Is someone you know on the list?
Renaissance Architecture: A Journey from Faith to Humanism
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose

Operating System Sheduling

  • 1. B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Operating System KCS – 401 CPU Scheduling Dr. Pankaj Kumar Associate Professor – CSE SRMGPC Lucknow
  • 2. Outline of the Lecture B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Real-Time CPU Scheduling Operating Systems Examples Algorithm Evaluation
  • 3. Basic Concept B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Maximum CPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait CPU burst followed by I/O burst CPU burst distribution is of main concern Burst Time/Execution Time: It is a time required by the process to complete execution. It is also called running time. Arrival Time: when a process enters in a ready state.
  • 4. CPU Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar 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. The selection process will be carried out by the CPU scheduler. It selects one of the processes in memory that are ready for execution.
  • 5. CPU Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Types of 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.
  • 6. CPU Scheduler B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them Queue may be ordered in various ways CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state 2. Switches from running to ready state 3. Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive Consider access to shared data Consider preemption while in kernel mode Consider interrupts occurring during crucial OS activities
  • 7. Dispatcher B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch latency – time it takes for the dispatcher to stop one process and start another running
  • 8. Scheduling Criteria B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar CPU utilization: CPU utilization is the main task in which the operating system needs to make sure that CPU remains as busy as possible. It can range from 0 to 100 percent. Throughput: The number of processes that finish their execution per unit time is known Throughput. So, when the CPU is busy executing the process, at that time, work is being done, and the work completed per unit time is called Throughput. Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue. Response time: It is an amount to time in which the request was submitted until the first response is produced. Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the calculation of the total time spent waiting to get into the memory, waiting in the queue and, executing on the CPU. The period between the time of process submission to the completion time is the turnaround time.
  • 9. Scheduling Criteria B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Completion Time: Time at which process completes its execution. Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time Waiting Time(W.T): Time Difference between turn around time and burst time. Waiting Time = Turn Around Time – Burst Time Waiting time = Start time - Arrival time
  • 10. Scheduling Type B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Types of CPU scheduling Algorithm There are mainly six types of process scheduling algorithms 1. First Come First Serve (FCFS) 2. Shortest-Job-First (SJF) Scheduling 3. Shortest Remaining Time 4. Priority Scheduling 5. Round Robin Scheduling 6. Multilevel Queue Scheduling
  • 11. First Come First Serve (FCFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar It is the easiest and most simple CPU scheduling algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation first. This scheduling method can be managed with a FIFO queue. As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of the queue. So, when CPU becomes free, it should be assigned to the process at the beginning of the queue. Characteristics of FCFS method: • It offers non-preemptive and pre-emptive scheduling algorithm. • Jobs are always executed on a first-come, first-serve basis • It is easy to implement and use. • However, this method is poor in performance, and the general wait time is quite high.
  • 12. First Come First Serve (FCFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 P P P 1 2 3 0 24 30 27
  • 13. First Come First Serve (FCFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt chart for the schedule is: Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case P1 0 3 6 30 P2 P3 Convoy effect - short process behind long process Consider one CPU-bound and many I/O-bound processes
  • 14. First Come First Serve (FCFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Burst time Arrival time P1 6 2 P2 3 5 P3 8 1 P4 3 0 P5 4 4 Waiting time for P1 = 11-2 = 9 Waiting time for P2 = 21-5 = 16 Waiting time for P3 = 3-1 = 2 Waiting time for P4 = 0 Waiting time for P5 = 17-4 = 13 Average waiting time: (9 + 16 + 2+0+13)/5 = 40/5 =8 Turn Around Time = P1 = 17-2 = 15 Turn Around Time = P2 = 23-5 = 18 Turn Around Time = P3 = 11-1 = 10 Turn Around Time = P4 = 3-0 = 3 Turn Around Time = P5 = 21-4 = 17 Average Turn Around Time : (15 + 18 + 10+3+17)/5 = 63/5 =12.6
  • 15. First Come First Serve (FCFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Id Arrival time Burst time P1 3 4 P2 5 3 P3 0 2 P4 5 1 P5 4 3 Turn Around Time = P1 = 7-3 = 4 Turn Around Time = P2 = 13-5 = 8 Turn Around Time = P3 = 2-0 = 2 Turn Around Time = P4 = 14-5 = 9 Turn Around Time = P5 = 10-4 = 6 Average Turn Around time = (4 + 8 + 2 + 9 + 6) / 5 = 29 / 5 = 5.8 Waiting time for P1 = 3 – 3 = 0 Waiting time for P2 = 10-5 = 5 Waiting time for P3 = 0 Waiting time for P4 = 13-5 = 8 Waiting time for P5 = 7-4 = 3 Average waiting time: (0 + 5 + 0 + 8 + 3)/5 = 16/5 =3.2
  • 16. Shortest Job First Serve (SJFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar 1. It is a Greedy Algorithm. 2. Sort all the process according to the arrival time. 3. Then select that process which has minimum arrival time and minimum Burst time. 4. After completion of process make a pool of process which after till the completion of previous process and select that process among the pool which is having minimum Burst time. • It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of ageing. • It is practically infeasible as Operating System may not know burst time and therefore may not sort them. While it is not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of running time are available.
  • 17. Shortest Job First Serve (SJFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Burst Time P1 6 P2 8 P3 7 P4 3 SJF scheduling chart Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 P3 0 3 24 P4 P1 16 9 P2
  • 18. Shortest Job First Serve (SJFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Now we add the concepts of varying arrival times and preemption to the analysis Process Arrival Time Burst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec Preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF).
  • 19. Shortest Job First Serve (SJFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Id Arrival time Burst time P1 3 1 P2 1 4 P3 4 2 P4 0 6 P5 2 3 set of 5 processes whose arrival time and burst time are given below- •Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit •Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit
  • 20. Shortest Job First Serve (SJFS) B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Advantages- • SJFS is optimal and guarantees the minimum average waiting time. • It provides a standard for other algorithms since no other algorithm performs better than it. Disadvantages- • It can not be implemented practically since burst time of the processes can not be known in advance. • It leads to starvation for processes with larger burst time. • Priorities can not be set for the processes. • Processes with larger burst time have poor response time.
  • 21. Priority Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Priority Scheduling is a method of scheduling processes that is based on priority. In this algorithm, the scheduler selects the tasks to work as per the priority. The processes with higher priority should be carried out first, whereas jobs with equal priorities are carried out on a round-robin or FCFS basis. Priority depends upon memory requirements, time requirements, etc. Characteristics of Priority Scheduling • It used in Operating systems for performing batch processes. • If two jobs having the same priority are READY, it works on a FIRST COME, FIRST SERVED basis. • In priority scheduling, a number is assigned to each process that indicates its priority level. • Lower the number, higher is the priority. • In this type of scheduling algorithm, if a newer process arrives, that is having a higher priority than the currently running process, then the currently running process is preempted.
  • 22. Priority Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Burst Time Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = (6+0+16+18+1)/5 = 8.2 msec
  • 23. Priority Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Priority Burst time Arrival time P1 1 4 0 P2 2 3 0 P3 1 7 6 P4 3 4 11 P5 2 2 12 Average Waiting time = (0+11+0+5+2)/5 = 18/5= 3.6
  • 24. Priority Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Advantages Processes are executed on the basis of priority so high priority does not need to wait for long which saves time Suitable for applications with fluctuating time and resource requirements. Disadvantages of priority scheduling Starvation – low priority processes may never execute Solution  Aging – as time progresses increase the priority of the process
  • 25. Round-Robin Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar • Round robin is a pre-emptive algorithm • The CPU is shifted to the next process after fixed interval time, which is called time quantum/time slice. • The process that is preempted is added to the end of the queue. • Round robin is a hybrid model which is clock-driven • Time slice should be minimum, which is assigned for a specific task that needs to be processed. However, it may differ OS to OS. • It is a real time algorithm which responds to the event within a specific time limit. • Round robin is one of the oldest, fairest, and easiest algorithm. • Widely used scheduling method in traditional OS.
  • 26. Round-Robin Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process Queue Burst time P1 4 P2 3 P3 5 quantum/time slice = 2 Waiting Time P1= 8-4= 4 P2= 9-3= 6 P3= 12-5= 7 Turnaround Time P1= 8 P2= 9 P3= 12 Average Waiting time = (4+6+7)/3 = 5.66
  • 27. Round-Robin Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Process ID Arrival Time Burst Time 1 0 5 2 1 6 3 2 3 4 3 1 5 4 5 6 6 4 Process ID Completion Time Turn Around Time Waiting Time 1 17 17 12 2 23 22 16 3 11 9 6 4 12 9 8 5 24 20 15 6 21 15 11 Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units P1-P2-P3-P4-P5-P1-P6-P2-P5
  • 28. Round-Robin Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Disadvantages • If slicing time of OS is low, the processor output will be reduced. • This method spends more time on context switching • Its performance heavily depends on time quantum. • Priorities cannot be set for the processes. • Round-robin scheduling doesn't give special priority to more important tasks. • Lower time quantum results in higher the context switching overhead in the system. • Finding a correct time quantum is a quite difficult task in this system.
  • 29. Multilevel Queue Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Multilevel queue scheduling algorithm partitions the ready queue into several separate queues. A common division is made between foreground (or interactive) processes and background (or batch) processes. These two types of processes have different response-time requirements, and so might have different scheduling needs. In addition, foreground processes may have priority over background processes. Multilevel queue scheduling has the following characteristics: • Each queue has its own scheduling algorithm • Processes are divided into different queues based on their process type, memory size and process priority.
  • 30. Multilevel Queue Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar • System Process The Operating system itself has its own process to run and is termed as System Process. • Interactive Process The Interactive Process is a process in which there should be the same kind of interaction (e.g. online game ). • Batch Processes Batch processing is basically a technique in the Operating system that collects the programs and data together in the form of the batch before the processing starts. • Student Process The system process always gets the highest priority while the student processes always get the lowest priority. For System Processes: First Come First Serve(FCFS) Scheduling. For Interactive Processes: Shortest Job First (SJF) Scheduling. For Batch Processes: Round Robin(RR) Scheduling For Student Processes: Priority Scheduling
  • 31. Multilevel Feedback Queue Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar This Scheduling is like Multilevel Queue (MLQ) Scheduling but in this process can move between the queues. Multilevel Feedback Queue Scheduling (MLFQ) keep analyzing the behavior (time of execution) of processes and according to which it changes its priority. Three queues: Q1 – RR with time quantum 8 milliseconds Q2 – RR time quantum 16 milliseconds Q3 – FCFS Scheduling A new job enters queue Q0 which is served FCFS When it gains CPU, job receives 8 milliseconds If it does not finish in 8 milliseconds, job is moved to queue Q1 At Q1 job is again served FCFS and receives 16 additional milliseconds If it still does not complete, it is preempted and moved to queue Q2
  • 32. Multiprocessor Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Multiple processor scheduling or multiprocessor scheduling focuses on designing the scheduling function for the system which is consist of ‘more than one processor’. With multiple processors in the system, the load sharing becomes feasible but it makes scheduling more complex. CPU scheduling more complex when multiple CPUs are available Homogeneous processors within a multiprocessor Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes in common ready queue, or each has its own private queue of ready processes
  • 33. Multiprocessor Scheduling B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Processor affinity – process has affinity for processor on which it is currently running 1. Soft Affinity – When an operating system has a policy of attempting to keep a process running on the same processor but not guaranteeing it will do so, this situation is called soft affinity. 2. Hard Affinity – Hard Affinity allows a process to specify a subset of processors on which it may run. If SMP, need to keep all CPUs loaded for efficiency Load balancing attempts to keep workload evenly distributed Push migration – periodic task checks load on each processor, and if found pushes task from overloaded CPU to other CPUs Pull migration – idle processors pulls waiting task from busy processor