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
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: Threads →← 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 →
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.
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 Preemptive Scheduling
What is the difference between preemptive and non-preemptive scheduling?
What is shortest remaining time first scheduling?
Why does preemption sometimes not happen under shortest remaining time first?
What are the disadvantages of preemptive scheduling?
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 →