Lecture - 3
Lecture - 3
CPU Scheduling
Out line
CPU Scheduling introduction
✓ Scheduling Criteria
✓ Scheduling Algorithms
✓ Scheduling in Linux OS as an example
Process Synchronization
✓ The critical section problem
✓ Software and Hardware solutions for critical section problem
✓ Classic Problems of Synchronization
Deadlocks
✓ Definition and characteristics of deadlock
✓ Methods for handling deadlocks
Main objective of CPU scheduling
▪ Decide which process runs, when, and for how long.
▪ Enforce Priorities
Round Robin and Shortest First Come First Serve and Shortest Job
Examples
Remaining Time First. First.
CPU “Scheduling“ Criteria
Criteria Definition Goal
CPU utilization The % of time the CPU is executing user level process code. Maximize
Throughput Number of processes that complete their execution per time Maximize
unit.
Turnaround time Amount of time to execute a particular process. Minimize
Waiting time Amount of time a process has been waiting in the ready queue. Minimize
Response Time Amount of time it takes from when a request was submitted Minimize
until the first response is produced.
CPU Scheduling introduction
CPU Scheduling introduction
CPU Scheduling
✓ When a computer is multi-programmed, it frequently has multiple
processes competing for the CPU at the same time.
✓ When more than one process is in the ready state and there is only one
CPU available, the operating system must decide which process to run first.
✓ The part of the operating system that makes the choice is called the
scheduler; the algorithm it uses is called the scheduling algorithm.
CPU Scheduling
S c h e d u l i n g Po l i c y
▪ Based on time-sharing
- time slice
▪ Based on priority ranking
- dynamic
▪ Based on Classification of processes
- interactive processes
- batch processes
- real-time processes
System calls related to CPU scheduling
Scheduling
❑The important thing to notice that some processes,
(a) spend most of their time computing, while others,
(b) spend most of their time waiting for I/O.
The former are called compute-bound; the latter are called I/O-bound.
Scheduling
❑ There are a variety of situations in which scheduling may occur.
❑Turnaround time is the average time from the moment that a batch
job is submitted until the moment it is completed. It measures how long
the average user has to wait for the output. Here the rule is: Small is
Beautiful.
1) Scheduling in Batch Systems
1.1) First-Come First-Served (FCFS)
1.2) Shortest Job First (SJF)
1.3) Shortest Remaining Time Next
1.4) Three-Level Scheduling
▪ The admission scheduler decides which jobs to admit to the system.
▪ Once a job has been admitted to the system, a process can be created for
it and it can contend for the CPU.
▪ We will call this scheduler the memory scheduler, since it determines
which processes are kept in memory and which on the disk.
Picking one of the ready processes in main memory to run next which is
called the CPU scheduler.
First-Come First-Served (FCFS)
Shortest Job First (SJF)
1) Scheduling in Batch Systems..
✓ One of the oldest, simplest, fairest, and most widely used algorithms is
round robin.
✓ If the process is still running at the end of the quantum, the CPU is
preempted and given to another process.
✓ If the process has blocked or finished before the quantum has elapsed,
the CPU switching is done when the process blocks.
2.4) Guaranteed Scheduling: The system must keep track of how much CPU each
process has had since its creation.
2.5) Lottery Scheduling: The basic idea is to give processes lottery tickets for various
system resources, such as CPU time.
2.6) Fair-Share Scheduling: we have assumed that each process is scheduled on its
own, without regard to who its owner is.
3) Scheduling in Real-Time Systems
✓ The events that occurring at regular intervals or aperiodic (occurring
unpredictably).
✓ A system may have to respond to multiple periodic event streams.
✓ Depending on how much time each event requires for processing, it may
not even be possible to handle them all.
✓ For example, if there are m periodic events and event i occurs with
period Pi and requires Ci seconds of CPU time to handle each event,
𝑐𝑖
then the load can only be handled if ≤1
𝑖=1 𝑝𝑖
✓ If these events require 50, 30, and 100 msec of CPU time per event,
respectively, the system is schedulable because 0.5 + 0.15 + 0.2 < 1.
Reading assignment
Deadlocks
A set of two or more processes are deadlocked if they are blocked (i.e.,
in the waiting state) each handling a resource and waiting to acquired a
resource held by another process in the set.
Examples
Characteristics of deadlock
✓Deadlock can a rise if four conditions hold simultaneously
2. Deadlock Avoidance
4. Deadlock Ignorance
▪ Ignore the problem and pretend that deadlocks never occur in the
system: used by most OS, including Linux.