Multitasking, Explained | Introduction to Operating Systems

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

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

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

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.

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 Multitasking

What is the difference between multitasking and multiprocessing?
Multitasking lets several programs make progress on one CPU by giving each a short turn in rotation, while multiprocessing uses several CPUs to run programs at the very same instant. Multitasking works even with a single CPU. Modern computers do both: they have several cores, and each core multitasks among many programs.
What is a context switch?
A context switch is the operating system stopping one program and starting another on the CPU. It saves the leaving program's context, meaning its program counter, other registers, and a pointer to its memory, into that program's process control block. Then it loads the next program's context from its own block and resumes it exactly where it stopped.
How does the time slice length affect performance?
Shorter time slices cause more context switches, while longer slices make other programs wait longer for a turn. With three jobs needing 4, 3, and 5 milliseconds and a 1-millisecond switch cost, 1-millisecond slices cause 10 switches and finish at 22 milliseconds, while 4-millisecond slices cause 2 switches and finish at 14. Real systems pick a middle value.
What is cooperative multitasking?
Cooperative multitasking is a style in which each program must hand the CPU back by itself, instead of the operating system taking it on a schedule. Sixteen-bit Windows programs and the classic Mac OS worked this way. Its weakness is that one program that never gives up the CPU can freeze the whole machine, a problem the timer interrupt solved.

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 →