CPU Scheduling, Explained | Introduction to Operating Systems

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

← 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 →

Earn 3 college credits for Introduction to Operating SystemsRecommended for credit by NCCRS · Computer Science 354 · self-paced · one payment, lifetime access

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.

Compare: Linux vs Operating Systems

Why take it here

Why students take Introduction to Operating Systems with UPI Study

What happens under every app you runIntroduction to Operating Systems explains the software that runs a computer: processes and scheduling, threads and synchronization, deadlock, memory and paging, devices and storage, file systems, networking, security and virtualization. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

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.

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?
Turnaround time is a job's finish time minus its arrival time, and waiting time is turnaround time minus its burst time. For example, a job arriving at time 3 that needs 2 units and finishes at 18 has a turnaround of 15 and a waiting time of 13. Response time, the time of the first turn minus arrival, matters most to people at a keyboard.
What is the convoy effect in first come, first served scheduling?
The convoy effect happens when one long job near the front of the queue makes every shorter job behind it wait. Under first come, first served, jobs needing 5, 3, 8, and 2 units run in arrival order, so the 2-unit job arriving at time 3 cannot start until 16. Shortest job first would start it at time 5 instead.
What is starvation in CPU scheduling and how does aging fix it?
Starvation is when a job waits indefinitely because others keep getting chosen ahead of it, such as a long job under shortest job first while short jobs keep arriving. Aging fixes it by slowly raising a waiting job's priority the longer it waits, so even the lowest-priority job eventually runs. A print server can use aging so a huge poster still prints.
Why can round robin have a higher average waiting time but better response time?
Round robin keeps interrupting jobs to give each one a short turn, which stretches finish times but gives every job an early first turn. With a quantum of 2 on four sample jobs, it had the worst averages, yet the 2-unit job started at time 8 instead of 16 under first come, first served. Too large a quantum makes it behave like first come, first served.

About the Introduction to Operating Systems course

How many college credits is Introduction to Operating Systems?
Introduction to Operating Systems (Computer Science 354) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Introduction to Operating Systems cost and what is included?
$250, paid once, with lifetime access: all 83 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Introduction to Operating Systems take?
It is self-paced with no deadlines. The 83 lessons total about 10 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

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

Introduction to Operating Systems
3 credits · NCCRS
Enroll →