UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 2.6 Free with a free account
CPU Scheduling
Introduction to Operating Systems · Chapter 2: Process Management: Scheduling, Multitasking, and System CallsSection 2.6 · lesson 15 of 838 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Introduction to Operating Systems.
Lesson complete
Earn the 3 college credits for Introduction to Operating Systems
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Preemptive Scheduling →← Job SchedulingNext lesson: Preemptive Scheduling →
A free account plays all of Chapter 1 and 3 more lessons of Introduction to Operating Systems. Create a free account →
In this lesson
Comparing first come, shortest job, priority, and round robin scheduling.
- Calculate turnaround, waiting, and response time from a schedule
- Draw Gantt charts for first come first served, shortest job first, and round robin
- Explain the convoy effect, starvation, and how aging prevents starvation
- Choose a scheduling policy that fits a given workload
Free study notes
Get this lesson's notes by email
The key points and 4 practice questions with answers, in your inbox. One email, free.
One email. Unsubscribe in one click.
Transcript
Everything said in the video, section by section (1,140 words). Read along, or search it.
Opening Question
One Till, Four Customers
Sam, who runs a small online bakery, has one till at the pickup counter, and four people in line. One wants a single croissant. One has a wedding order of sixty cupcakes. Two more have small buys. Serve them in arrival order, and the croissant buyer might wait half an hour behind the wedding order. Always serve the smallest order first, and the wedding customer might wait all day. Neither feels fair. A CPU faces the same puzzle thousands of times a second. The rule it uses is called a scheduling policy.
Measures
Turnaround, Waiting, and Response Time
The short-term scheduler from lesson 2.5 picks the next job from the ready queue. The rule it follows is the scheduling policy. To compare policies, we need numbers. Each job has an arrival time, when it joins the ready queue, and a burst time: how much CPU time it needs. Turnaround time is finish time minus arrival time: how long the job spent in the system, start to end. Waiting time is turnaround minus burst: how long it sat in the ready queue, doing nothing. And response time is the time of its first turn, minus arrival. People at a keyboard feel that one most.
Example Setup
Four Jobs and a Gantt Chart
Every example here uses the same four jobs. A arrives at time zero and needs five units. B arrives at one and needs three. C arrives at two and needs eight. D arrives at three, and needs just two. Each schedule is drawn as a Gantt chart: a strip of time showing who has the CPU at each moment. The numbers come from a small Python program that applies each rule.
First Come, First Served
Arrival Order and the Convoy Effect
The simplest rule is first come, first served. Whoever arrived first goes first, and runs until done. A runs first, until five. Then B, until eight. Then C, the long one, until sixteen. And D last, until eighteen. It's easy to build, and nobody gets skipped. But look at D. It arrived at three and needed only two units, yet it waited thirteen. That's the convoy effect: one long job at the front makes all the short ones behind it wait.
Shortest Job First
Short Jobs Out of the Way
Shortest job first picks, among the jobs that have arrived, the one with the smallest burst, and lets it run to completion. At time zero only A has arrived, so A runs until five. By then B, C and D are all waiting, and D, needing two, is the shortest. So D runs until seven, then B until ten, then C until eighteen. When the jobs are all already waiting, this order gives the lowest possible average wait, because short jobs get out of the way quickly.
Drawbacks
Unknown Bursts and Starvation
It has two catches. First, the operating system can't truly know a burst time in advance. Real systems guess, from a job's past behavior. Second, a steady stream of short jobs can keep a long one waiting forever. That's called starvation. Its preemptive twin, shortest remaining time first, stops the running job whenever a newcomer needs less than it has left. Lesson 2.7 works an example.
Quick Check
Average Wait for X, Y, and Z
Quick check, with three new jobs that all arrive at time zero. X needs six units, Y needs one, and Z needs two. Under shortest job first, what's the average waiting time? Pause, and work it out. One and a third. Y runs first, and waits zero. Z waits one, for Y. And X waits three, for both. Zero plus one plus three is four, over three jobs. Four and a third is what you'd get serving X first, in first come, first served order. X's six units would hold up both short jobs.
Priority Scheduling
Most Important Ready Job Goes Next
In priority scheduling, every job carries a priority number, and the CPU always goes to the most important ready job. The system can set priorities, since a mouse click is more urgent than a backup. Or the user can set them. Shortest job first is really a priority policy, where the priority is the shortest burst. So it shares the danger: low-priority jobs can starve. The usual cure is aging. It slowly raises a job's priority the longer it waits, so even the lowest job eventually runs. Priority scheduling can be preemptive or not. That choice is lesson 2.7.
Round Robin
Two-Unit Turns in a Circle
Round robin is the policy built for sharing. Jobs wait in a circle. Each gets a small slice, the quantum, and if it isn't finished, it goes to the back of the line. With a quantum of two, A runs until two, B until four, C until six, and then A again, until eight. D gets its first turn at eight, until ten. Then B finishes at eleven, C runs until thirteen, A finishes at fourteen, and C ends at eighteen. In this program, newly arrived jobs line up ahead of a job that was just interrupted. Choosing the quantum is a trade-off. Too large, and round robin behaves like first come, first served. Too small, and the CPU spends its time switching.
Comparing Results
Lowest Averages Versus Early Turns
Here are the averages the program printed for the four jobs. Shortest job first wins on both: average turnaround eight and a half, and average waiting four. Round robin has the worst averages here, because it keeps interrupting jobs and stretching their finish times. Yet it gives every job an early first turn. D starts at eight, instead of sixteen under first come, first served. That's the point of round robin. It gives up a little average speed to keep everyone feeling served, which is what a person at a keyboard wants.
Multilevel Queue
Separate Queues for Each Job Type
One more idea. A multilevel queue doesn't force one rule on every job. The ready jobs are split into separate queues by type, each with its own policy. Interactive jobs might use round robin, while batch jobs use first come, first served. And the queues themselves are ranked, so a higher queue goes before a lower one.
Quick Check
A Poster Among One-Page Jobs
Quick check. Ivy, who looks after the computers at a school, runs a print server. One huge poster job arrives, and one-page jobs keep arriving all day. The short jobs mustn't sit behind the poster for hours. But the poster must print eventually. Which rule fits? Pause, and decide. Smaller jobs first, with aging. The one-page jobs skip ahead of the poster, and as the poster waits, its priority keeps rising, until it gets its turn. First come, first served gives the convoy effect: the one-pagers stuck behind the poster for hours. And smaller jobs first without aging can starve the poster, because short jobs never stop arriving.
Key Takeaways
Each Policy and Its Trade-Off
To recap. Turnaround is finish minus arrival, and waiting is turnaround minus burst. First come, first served is simple, but short jobs can get stuck in a convoy. Shortest job first gives the lowest average wait, but it can starve long jobs. Aging fixes starvation. Round robin gives every job an early turn. And a multilevel queue gives each kind of job its own rule.
About Introduction to Operating Systems at UPI Study
Introduction to Operating Systems (Computer Science 354) is a self-paced online course of 83 video lessons in 10 chapters, from what an operating system does and process scheduling to concurrency, memory, devices, file systems, networks, the internet, security and virtual machines. It carries a NCCRS credit recommendation for 3 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: Linux vs Operating Systems
Why take it here
Why students take Introduction to Operating Systems with UPI Study
Where the credit goes
Universities that work with NCCRS
Introduction to Operating Systems carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About CPU Scheduling
How do you calculate turnaround time and waiting time in CPU scheduling?
What is the convoy effect in first come, first served scheduling?
What is starvation in CPU scheduling and how does aging fix it?
Why can round robin have a higher average waiting time but better response time?
About the Introduction to Operating Systems course
How many college credits is Introduction to Operating Systems?
How much does Introduction to Operating Systems cost and what is included?
Which universities accept UPI Study credits?
How long does Introduction to Operating Systems take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 3 credits in Introduction to Operating Systems.
83 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
Updated
3 credits · NCCRSEnroll →