OS - Module 4 - Process Scheduling
OS - Module 4 - Process Scheduling
Module 4
Operating Systems
Module Outline
• Scheduler
o Scheduling algorithm
o Objectives of Scheduling
o Criteria for scheduling
o CPU-bound vs I/O-bound processes
• Types of Scheduling
• Process scheduling queues
o FCFS
o SJF
o Priority
o Round Robin
o Multilevel feedback queues scheduling
• BSD Unix scheduling
• Multiple processor scheduling
Process Scheduling 2
Operating Systems
Scheduling
• Scheduler
o The scheduler is part of the operating system, concerned with which process to run
next if multiple run-able processes are in ready queue
o Scheduler is concerned with deciding on policy, not providing a mechanism
o It is important because it can have a big effect on resource utilization and overall
performance of the system
• Scheduling Algorithm
o In a multiprogramming computer several processes will compete for use of the
processor
o At any time only one process will be running while other will wait for process to be
free
o Scheduling algorithm is logic that determines the order in which processes should
run when multiple processes are there
o The aim of process scheduling is to achieve objectives such as maximum CPU
utilization and to improve its performance
Process Scheduling 3
Operating Systems
Process Scheduling
Process Scheduling 4
Operating Systems
Process Scheduling
• 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
Process Scheduling 5
Operating Systems
Process Scheduling 6
Operating Systems
CPU Scheduler
• Short-term scheduler selects from among the processes in ready queue,
and allocates the CPU to one of them
o Queue may be ordered in various ways
• CPU scheduling decisions may take place when a process:
o Switches from running to waiting state
o Switches from running to ready state
o Switches from waiting to ready
o Terminates
• Scheduling under 1 and 4 is non-preemptive
• All other scheduling is preemptive
o Consider access to shared data
o Consider preemption while in kernel mode
o Consider interrupts occurring during crucial OS activities
Process Scheduling 7
Operating Systems
Dispatcher
• Dispatcher module gives control of the CPU to the process selected by
the short-term scheduler; this involves:
o switching context
o switching to user mode
o jumping to the proper location in the user program to restart that program
• Dispatch latency
o time it takes for the dispatcher to stop one process and start another running
Process Scheduling 8
Operating Systems
Scheduling Performance
• CPU utilization
o keep the CPU as busy as possible
• Throughput
o # of processes that complete their execution per time unit
• Turnaround time
o amount of time to execute a particular process
• Waiting time
o amount of time a process has been waiting in the ready queue
• Response time
o amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment)
Process Scheduling 9
Operating Systems
Process Scheduling 10
Operating Systems
Scheduling Philosophies
• Preemptive
o The system may preempt the CPU from a process at any time;
▪ Prevents any one process from using the CPU for too long
▪ Can lead to race condition (require synchronization techniques)
• Non-preemptive
o Each process voluntarily gives up the CPU
o It is;
▪ Simple
▪ Easy to implement
▪ Not suitable for multi-user systems
o If the running process become blocked next process can be scheduled
Process Scheduling 12
Operating Systems
• Ready queue
o Set of all the processes residing in main memory (ready or waiting)
• Device queue
o Set of processes waiting for an I/O devices
Process Scheduling 13
Operating Systems
Levels of Scheduling
• Scheduling can be exercised at three levels
• High-level scheduling (Long-term scheduler)
o Allow limited number of processes in the ready queue to compete
o It disallows processes beyond a certain limit for batch processes first and in the end
also the interactive processes
o Invoke less frequently
o May be slow
• Medium-level scheduling (Medium-term scheduler)
o It is concerned with decision to temporarily remove a process from the system (to
reduce system load)
o To reintroduce a process or swapping in
o What if there is room in memory and both a new process as well as swapped out
process want to be loaded
▪ In this case, medium level scheduler has to work in close conjunction with high level
scheduler
Process Scheduling 14
Operating Systems
Levels of Scheduling
• Low level-scheduling (Short-term scheduler)
o Handles the decisions of which process is to be assigned to the processor (dispatcher)
o Invoke very frequently
o Must execute fast
o Its scheduling could be preemptive or non-preemptive
o Dispatch latency should be minimum
▪ Dispatch latency is the time it takes for the dispatcher to stop one process and start another running
▪ Example:
• Assume a process runs for 90ms and scheduler run for 10ms
• Overheads = 10/(90+10) = 10%
• Any OS may use one or all of these levels, depending upon the desired
• These levels have to interact amongst themselves quite closely to ensure that the
computing resources are managed optimally
• The exact algorithms for these and interaction between them are quite complex
Process Scheduling 15
Operating Systems
Scheduling Algorithms
Process Scheduling 16
Operating Systems
P1 P2 P3
0 30 36 40
Process Scheduling 17
Operating Systems
Process Scheduling 18
Operating Systems
Process Scheduling 20
Operating Systems
P1 P3 P2 P4
0 3 7 8 12 16
◼ Average waiting time = (0 + 6 + 3 + 7)/4 = 4
Process Scheduling 21
Operating Systems
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
◼ Average waiting time = (9 + 1 + 0 +2)/4 = 3
Process Scheduling 22
Operating Systems
Process Scheduling 23
Operating Systems
Process Scheduling 24
Operating Systems
• Time quantum = 20 ms
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Process Scheduling 25
Operating Systems
Priority Scheduling
• In priority scheduling a priority is associated with each process and
the CPU is allocated to the process with the highest priority
• Equal priority processes are scheduled in FCFS order
• Priorities are generally some fixed range of numbers, such as 0, 1, 5,
10
• There is no general agreement on whether ‘0’ is highest or lower
priority
• Priority can be defined on the bases of following
o Process time limit
o Memory requirements
o Number of open files
o Ratio of average I/O burst to CPU burst
Process Scheduling 26
Operating Systems
Multilevel Queue
• Ready queue is partitioned into separate queues:
foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm
o foreground – RR
o background – FCFS
• Scheduling must be done between the queues
o Fixed priority scheduling; (i.e., serve all from foreground then from
background)
▪ Possibility of starvation
o Time slice – each queue gets a certain amount of CPU time which it can
schedule amongst its processes; i.e., 80% to foreground in RR
o 20% to background in FCFS
Process Scheduling 28
Operating Systems
Process Scheduling 29
Operating Systems
Process Scheduling 30
Operating Systems
• Scheduling
o 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
o 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
Process Scheduling 31
Operating Systems
Process Scheduling 32
Operating Systems
Thread Scheduling
Process Scheduling 33
Operating Systems
Thread Scheduling
• Distinction between user-level and kernel-level threads
• When threads supported, threads scheduled, not processes
• Process-contention scope (PCS)
o Thread library schedules user-level threads to run on LW
o Scheduling competition is within the process
o Typically done via priority set by programmer
o Many-to-one and many-to-many models
Process Scheduling 34
Operating Systems
Pthread Scheduling
• API allows specifying either PCS or SCS during thread creation
o PTHREAD_SCOPE_PROCESS schedules threads using PCS scheduling
o PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling
• Can be limited by OS
o Linux and macOS only allow PTHREAD_SCOPE_SYSTEM
Process Scheduling 35
Operating Systems
Multiple-Processor Scheduling
• CPU scheduling more complex when multiple CPUs are available
• Multiprocessor may be any one of the following architectures:
o Multicore CPUs
o Multithreaded cores
o NUMA systems
o Heterogeneous multiprocessing
Process Scheduling 38
Operating Systems
Multiple-Processor Scheduling
• Symmetric multiprocessing (SMP) is where each processor is self
scheduling
• All threads may be in a common ready queue (a)
• Each processor may have its own private queue of threads (b)
Process Scheduling 39
Operating Systems
Multicore Processors
• Recent trend to place multiple processor cores on same physical chip
• Faster and consumes less power
• Multiple threads per core also growing
o Takes advantage of memory stall to make progress on another thread while
memory retrieve happens
Process Scheduling 40
Operating Systems
Process Scheduling 41
Operating Systems
Process Scheduling 42
Operating Systems
Process Scheduling 43
Operating Systems
• Load balancing
o attempts to keep workload evenly distributed
• Push migration
o periodic task checks load on each processor, and if found pushes task from
overloaded CPU to other CPUs
• Pull migration
o idle processors pulls waiting task from busy processor
Process Scheduling 44
Operating Systems
Process Scheduling 46
Operating Systems
Process Scheduling 47
Operating Systems
Process Scheduling 48
Operating Systems
Process Scheduling 49
Operating Systems
Interrupt Latency
Process Scheduling 50
Operating Systems
Dispatch Latency
• Conflict phase of dispatch
latency
o Preemption of any process
running in kernel mode
o Release by low-priority process
of resources needed by high-
priority processes
Process Scheduling 51
Operating Systems
Priority-based Scheduling
• For real-time scheduling, scheduler must support preemptive,
priority-based scheduling
o But only guarantees soft real-time
• For hard real-time must also provide ability to meet deadlines
• Processes have new characteristics: periodic ones require CPU at
constant intervals
o Has processing time t, deadline d, period p
o0≤t≤d≤p
o Rate of periodic task is 1/p
Process Scheduling 52
Operating Systems
Process Scheduling 53
Operating Systems
Process Scheduling 54
Operating Systems
Process Scheduling 55
Operating Systems
Process Scheduling 56
Operating Systems
Process Scheduling 57
Operating Systems
Process Scheduling 60
Operating Systems
Process Scheduling 61
Operating Systems
Process Scheduling 62
Operating Systems
Process Scheduling 63
Operating Systems
Process Scheduling 64
Operating Systems
• CFS scheduler maintains per task virtual run time in variable vruntime
o Associated with decay factor based on priority of task – lower priority is
higher decay rate
o Normal default priority yields virtual run time = actual run time
• To decide next task to run, scheduler picks task with lowest virtual run
time
Process Scheduling 65
Operating Systems
CFS Performance
Process Scheduling 66
Operating Systems
Linux Scheduling
• Real-time scheduling according to POSIX.1b
o Real-time tasks have static priorities
• Real-time plus normal map into global priority scheme
• Nice value of -20 maps to global priority 100
• Nice value of +19 maps to priority 139
Process Scheduling 67
Operating Systems
Linux Scheduling
• Linux supports load balancing, but is also NUMA-aware
• Scheduling domain is a set of CPU cores that can be balanced against
one another.
• Domains are organized by what they share (i.e., cache memory.) Goal
is to keep threads from migrating between domains
Process Scheduling 68
Operating Systems
Windows Scheduling
• Windows use a priority based, preemptive scheduling algorithm
• Windows scheduler ensures that highest priority thread will always run
o Thread runs until
1. blocks
2. uses time slice
3. preempted by higher-priority thread
• Windows dispatcher uses a 32-level priority scheme
• Priorities are divided into two classes
o Variable class (priorities from 0 to 15)
o Real time class (priorities from 16 to 31)
• Priority 0 is memory-management thread
• Dispatcher uses a queue for each scheduling priority
• If no run-able thread, runs idle thread
Process Scheduling 69
Operating Systems
Process Scheduling 70
Operating Systems
Process Scheduling 71
Operating Systems
Windows Priorities
Process Scheduling 72
Operating Systems
Solaris Scheduling
• Solaris uses priority-based process scheduling
• It has six classes of priority, which are;
o Real time (RT)
o System (SYS)
o Fair Share (FSS)
o Fixed priority (FP)
o Time sharing (TS) (default)
o Interactive (IA)
• Given thread can be in one class at a time
• Each class includes different set of priorities
• Each class has its own scheduling algorithm
• Real time processes has highest priority and interactive and time-sharing
processes have least priorities
• Solaris uses system class to run kernel processes, such as scheduler
o System class is reserved for kernel processes
Process Scheduling 73
Operating Systems
Process Scheduling 74
Operating Systems
Solaris Scheduling
Process Scheduling 75
Operating Systems
Solaris Scheduling
• Scheduler converts class-specific priorities into a per-thread global
priority
o Thread with highest priority runs next
o Runs until (1) blocks, (2) uses time slice, (3) preempted by higher-priority
thread
o Multiple threads at same priority selected via RR
Process Scheduling 76
Operating Systems
Process Scheduling 83
Operating Systems
Implementation
• Even simulations have limited accuracy
• Just implement new scheduler and test in real systems
o High cost, high risk
o Environments vary
Process Scheduling 84