UPI Study › Courses › Introduction to Operating Systems › Free lessons › Lesson 2.4 Free with a free account
Multitasking
Introduction to Operating Systems · Chapter 2: Process Management: Scheduling, Multitasking, and System CallsSection 2.4 · lesson 13 of 837 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: Job Scheduling →← System CallsNext lesson: Job 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
How time slices, timer interrupts, and context switches share one CPU.
- Explain how time slices let one CPU run several programs in turn
- Describe how a timer interrupt returns control to the operating system
- Explain what a context switch saves and loads and why it costs time
- Calculate switches and finish time for different slice lengths
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 (908 words). Read along, or search it.
Opening Question
Three Programs on One Core
Sam, who runs a small online bakery, is playing music, checking orders in a browser, and typing a price list, all at once. The laptop has one working core, and nothing stutters. Yet that core can only do one thing at a time. So who's doing the magic trick, and where's the catch? The trick belongs to the operating system. The catch is a small cost, paid every time it switches.
Analogy
A Juggler and Time Slices
A juggler with three balls only ever holds one at a time, yet all three seem to float. Multitasking works the same way. The operating system keeps several programs in progress, by giving each a short turn on the CPU, then moving on. Each turn is a time slice, also called a quantum. Slices are measured in milliseconds, so you can't tell one after another, very quickly, from all at once.
Comparison
Taking Turns Versus Several CPUs
Compare that with lesson 2.1. Multiprocessing needs several CPUs, to run programs at the very same instant. Multitasking works even with one CPU, by taking turns. Modern computers do both: there are several cores, and each core multitasks.
Timer Interrupt
Taking the CPU Back on Schedule
While a program is running, how does the operating system get the CPU back? The program certainly won't hand it over politely. The trick is hardware. A small clock, the timer, rings at a steady rhythm. Each ring is an interrupt. An interrupt makes the CPU drop what it's doing, switch to kernel mode, from lesson 2.3, and run a piece of operating system code. If the running program has used up its slice, the operating system picks another one. So the system gets control back on a schedule, whether the program likes it or not. Lesson 3.3 looks closely at interrupts.
Context Switch
Saving and Loading a Program's Context
Stopping one program and starting another is called a context switch. The context is everything the CPU needs to carry on exactly where it left off: the program counter, the other CPU registers, and a pointer to the process's memory. First, the operating system saves the leaving program's context into its process control block. Then it loads the next program's context from that program's own block, and jumps back into it, exactly where it stopped.
Common Mistake
Switches Are Never Free
A common mistake is thinking a context switch is free. While the operating system copies values in and out, no program does any of its own work. And the CPU's fast caches, from lesson 4.9, are now warmed up for the wrong program. So switching is useful, but every extra switch is time taken away from real work.
Simulation Setup
Three Programs, One Millisecond per Switch
So how long should a slice be? This small Python program simulates three programs sharing one CPU. Browser needs four milliseconds of CPU time, Music needs three, and Editor needs five. That's twelve milliseconds of useful work. Every switch costs one millisecond. That's a made-up number to keep the arithmetic clear. Real switches are far cheaper, but never free. And the program tries slices of one, two, and four milliseconds.
How It Works
Taking Turns in a Fixed Order
Inside run, the programs take turns in a fixed order, skipping any that have finished. Whenever the CPU moves to a different program, the clock gains the switch cost, and the switch is counted. Then the program runs for one slice, or less if it needs less, and its time left goes down.
Results
Ten Switches Versus Two
Run it. With one-millisecond slices, the CPU switched ten times, and the jobs didn't finish until twenty-two milliseconds, though the useful work is only twelve. With four-millisecond slices, there were only two switches, and everything finished at fourteen. The rows underneath show the turns: B one, M one, E one, and so on. Count the changes, and you count the switches.
Trade-Off
Tiny Slices Versus Huge Slices
So bigger slices waste less time. Why not make them huge? Because a huge slice makes every other program wait longer for its turn. If Music had to wait through a very long Editor slice, the sound would stutter. Tiny slices waste the CPU on switching. Huge slices make the computer feel sluggish. So real systems pick a middle value.
Quick Check
Three-Millisecond Slices
Quick check. Change the slice list to just three milliseconds, and keep everything else the same. Browser needs four, Music three, Editor five. How many switches, and when does everything finish? Pause and predict. Four switches, done at sixteen milliseconds. The turns go B three, M three, E three. Then Browser has one left, and Editor has two. That's four changes of program: Browser to Music, Music to Editor, Editor back to Browser, and Browser to Editor. Twelve plus four is sixteen. Two switches would be right only if every program finished in one turn. Browser and Editor need a second turn, and each turn change costs one.
History
Cooperative Multitasking and Frozen Machines
Back to Sam's laptop. Music, browser and price list each get turns of a few milliseconds, and the timer makes sure nobody keeps the CPU. Early personal computer systems often used cooperative multitasking instead: each program had to hand the CPU back by itself. Sixteen-bit Windows programs and the classic Mac OS worked that way. One program that forgot could freeze the whole machine. The timer interrupt ended that. Lesson 2.7 compares the two styles.
Key Takeaways
Slices, Timers, and Switch Costs
To recap. Multitasking gives each program a short time slice on the CPU, so many seem to run at once. A timer interrupt hands control back to the operating system on a schedule. A context switch saves one program's context and loads another's, and it always costs some time. And slice length is a trade-off: tiny slices waste time on switching, and huge ones feel sluggish.
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 Multitasking
What is the difference between multitasking and multiprocessing?
What is a context switch?
How does the time slice length affect performance?
What is cooperative multitasking?
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 →