seegongsik
Operating systems

What is a context switch

Every time it switches jobs, the computer writes down how far it got, then reads that note back when it returns. Behind all that turn-taking is this little note.

01

Leave a note when you stop

Imagine you're working at your desk and suddenly have to do something else. If you just get up and leave, you won't remember where you were when you come back.
So you jot down a note saying "got this far" before you leave, and when you come back, you read it and pick up right where you stopped.
A computer does exactly the same. When it pauses a job, it saves the current state in full, like a note, and when it comes back, it loads that state just as it was.
When you pause a game and start it again later and it continues from the same spot, that's also because the state at the moment of pausing was saved.

Task A in progressgot this farSaveRestoreBack, continuegot this far

Write down the state before leaving, then come back and carry on exactly where you were.

When a job stops, its state is saved; when it resumes, that state is restored.

02

Slowing down the moment of switching

Let's slow down that brief moment of switching jobs.
First, job A is running. When it's time to switch, A's state is saved, and then job B's saved state is loaded. Only then does B start running.
The important point is that during the save and load, neither A nor B can do any work. That time is spent purely on switching places.
Restoring can only begin after saving finishes, so this empty stretch can't be skipped.

No task runs hereTask ASaveA stateRestoreB stateTask B

Between saving and restoring, no job makes any progress.

A switch happens in two steps, saving and restoring, and nothing gets done in between.

03

The more often you switch, the more you lose

Switching always has a small cost: the time spent writing the note and reading it back.
Use the slider below to set how many times the job switches within the same 1 second. As the number of switches rises, the time spent switching grows, and the time spent actually working shrinks.
Switch too rarely, and one job hogs the seat for too long; switch too often, and you're too busy switching to get any work done. That's why a balance is needed.
When a computer slows down with too many programs open, one of the reasons is that switching time piles up.

Switches per second6
Real work time 82%Time changing seats 18%
A sensible balance. Switch too rarely and one task hogs the seat; switch too often and you spend your time switching instead of working.

Move the slider to see how much the switching cost eats into working time.

Switching isn't free, so switching too often eats into working time.

04

Same time, different results

Let's look at two approaches side by side over exactly the same time.
The left one switches only now and then. Few cells go to switching, so many cells go to work. The right one switches very often. A switching cost squeezes into every cell, so few cells actually get work done.
The same amount of time passed for both, but when it's over, they've handled different amounts. What made the difference wasn't the amount of work but the time thrown away on switching.
It's just like a person who keeps jumping between tasks: busy all the time, yet finishing very little.

Switch now and then
Work 10 · Switch 2
Switch very often
Work 6 · Switch 6
work slot switch slot

Both rows are the same length, but the one that switches often has fewer working cells.

When switches are frequent, you get less done even in the same amount of time.

05

Summing it up on one page

There are just three things to remember.
First, the note is the state: saving all the progress so far when a job stops is what leaving a note means.
Second, switching is two actions, saving and loading back, and no job moves forward in between. Third, that's why switching too often is costly: as switching time piles up, the time left for real work shrinks.
In the end, switching is the secret to doing many jobs with a single worker, and the knack is to switch just the right amount.

One
The note is the state. When you stop, you save all the progress so far.
Two
Switching is two actions, saving and loading again. Nothing runs in between.
Three
So switching too often costs you. As switching time piles up, work time shrinks.

The note is the state, a switch is saving and restoring, and switching too often costs you.

In one lineSwitching means saving and restoring state, and since it isn't free, it has to be done in moderation.
← Operating systems
Was this helpful? Support seegongsik