Preemptive Scheduling, Explained

UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 2.7 Free with a free account

Preemptive Scheduling

Introduction to Operating Systems · Chapter 2: Process Management: Scheduling, Multitasking, and System CallsSection 2.7 · lesson 16 of 836 min · free with a free account

← CPU SchedulingNext lesson: Threads →

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

When an operating system may take the CPU from a running job.

  • Distinguish preemptive from non-preemptive scheduling
  • Identify the four moments when a scheduler can act
  • Compare shortest job first with shortest remaining time first on the same jobs
  • Weigh the benefits of preemption against its costs

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 (796 words). Read along, or search it.

Opening Question

A Quiz Behind a Long Report

Ivy, who looks after the computers at a school, is halfway through printing a hundred-page report. Then a teacher walks in with a one-page quiz, due in two minutes. Should the printer finish the report first, or pause it and print the quiz? Both answers are reasonable, and operating systems have to pick one. It decides whether a computer feels snappy, or sticky.

Definitions

May the Running Job Be Interrupted

A scheduling policy answers: who goes next? A second question is: may the running job be interrupted? In non-preemptive scheduling, once a job has the CPU, it keeps it until it finishes, or gives it up by itself, for example by waiting for a disk. In preemptive scheduling, the operating system may take the CPU away from a running job at any moment. That happens when its time slice ends, or when a more important job becomes ready.

Decision Points

Two Moments Become Four

Look back at the process states from lesson 2.2. The scheduler gets a chance to act whenever a job changes state. Without preemption, there are only two such moments: the running job asks for something slow, or it ends. Preemption adds two more. The timer ends the job's slice, so it goes from running back to ready. Or a job finishes waiting, and it's more urgent than the one that's running.

Simulation Setup

Three Jobs, Two Versions of Shortest First

To see the difference, here's a small Python program with three jobs. P arrives at zero and needs eight units. Q arrives at one and needs two. R arrives at two and needs one. Every time unit, it asks who should run. Without preemption, a running job simply carries on. With preemption, it picks the job with the least work left, every single time. So the same shortest-first idea runs twice: as shortest job first, then as its preemptive twin, shortest remaining time first.

Running It

P Holds the CPU or Gives Way

Run it. Without preemption, P is the only job at time zero, so it starts, and holds the CPU until eight. Q and R, which need only two and one, wait behind it. With preemption, Q arrives at one, needing less than P has left, so Q takes over. Then R runs, and P finishes last, at eleven.

Results

Waiting and Response Times Fall

Compare the numbers. Preemption cut the average wait from four point six seven to one point three three. And the average response, the time before a job's first turn, fell to a third of a unit. Short, urgent jobs no longer sit behind a long one. That's why every modern desktop, phone and server is preemptive. A program that hangs can't freeze the machine, and a click is answered promptly.

The Cost

More Switches and Later Finishes

The price is real, though. Every preemption is a context switch, and switches cost time, as lesson 2.4 showed. A preempted job may be halfway through updating data that another job reads. Lessons 3.3 and 3.4 deal with that. And long jobs get slower. P waited three units under preemption, and none without it. It got the same eight units of CPU either way. It just finished at eleven, instead of eight.

When to Use Each

Payroll Runs Versus Airbags

Non-preemptive scheduling is simpler, with fewer switches. It suits batch work where nobody is waiting, such as an overnight payroll run. Preemption is a must where deadlines matter, as in the real-time systems from lesson 1.3. An airbag controller can't wait for a long job to finish. So preemptive isn't better in every way. And shortest remaining time first also needs a guess of each job's remaining time, which is hard.

Quick Check

A Case Where No Preemption Happens

Quick check. Change the jobs. P arrives at zero and needs four. Q arrives at two and needs three. R arrives at three and needs two. Run the program again. What will the SRTF line show? Pause and predict. P runs from zero to four, without a break. Preemption never happens here. When Q arrives at two, P has only two units left, fewer than Q's three. When R arrives at three, P has one left, fewer than R's two. A newcomer takes over only when it needs less than the running job has left, not less than its original burst.

Real Example

Pausing Between Pages

Back to Ivy's printer. A preemptive printer pauses the report between pages, prints the quiz, and then carries on. The report finishes a minute later. And on an old computer with cooperative multitasking, a student's runaway loop once froze the whole machine. On a modern laptop, that's just one sleepy window. The timer pulls the CPU away every slice, so you can still click Close.

Key Takeaways

Preemption, Its Benefits, and Its Price

To recap. Non-preemptive scheduling lets a job keep the CPU until it ends or waits. Preemptive scheduling lets the operating system take it away. Preemption cuts waiting and response time for short jobs, and keeps a hung program from freezing the machine. Its price is more context switches, shared-data problems, and later finishes for long jobs. And real-time systems can't do without it.

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 Preemptive Scheduling

What is the difference between preemptive and non-preemptive scheduling?
In non-preemptive scheduling, a job keeps the CPU until it finishes or gives it up by itself, for example to wait for a disk. In preemptive scheduling, the operating system can take the CPU away at any moment, typically when the job's time slice ends or a more important job becomes ready. Modern desktop, phone, and server systems are preemptive.
What is shortest remaining time first scheduling?
Shortest remaining time first is the preemptive version of shortest job first: whenever a new job arrives needing less time than the running job has left, the newcomer takes over. With job P arriving at 0 needing 8 units, Q at 1 needing 2, and R at 2 needing 1, it cuts the average wait from about 4.67 to 1.33 units.
Why does preemption sometimes not happen under shortest remaining time first?
Preemption happens only when a newcomer needs less time than the running job has left, not less than the running job's original burst. If P needs 4 units from time 0, Q arrives at 2 needing 3, and R arrives at 3 needing 2, P runs straight through to 4. At each arrival, P's remaining time is smaller, 2 and then 1.
What are the disadvantages of preemptive scheduling?
Preemptive scheduling adds context switches, and each one takes time away from real work. A job can also be interrupted halfway through updating data that another job reads, which calls for careful synchronization. Long jobs finish later too: a job needing 8 units finished at 11 instead of 8. Shortest remaining time first also depends on guessing each job's remaining time.

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 →