You can listen to music, chat with a friend and download a game all at once. But one CPU core can only do one thing at a time. So how does it work? It's a trick called multitasking, and today you'll see it happen. 🤹
The juggling chef
Picture a chef with three pots. The chef gives each pot a quick stir, then moves to the next one, again and again. Each pot gets looked after, so it feels like all three cook at once.
The CPU does this with processes. It gives each one a tiny time slice, only a few thousandths of a second, then switches. The part of the OS that decides who goes next is the scheduler.
🧪 Lab 1: how many chefs do you have?
nproc
This is the number of cores: each is one chef who can truly cook at the same time. Remember your number, called N.
🧪 Lab 2: keep one chef busy
The command yes prints the letter y forever, which keeps a core 100% busy. We'll throw the output into /dev/null (the black hole) so it doesn't flood your screen.
yes > /dev/null &
top
In top, look for the process called yes. It uses almost 100% CPU: one whole chef is busy with it. Press q to leave top.
Now stop it:
kill %1
🧪 Lab 3: more jobs than chefs!
Now we start one more busy process than you have cores. If nproc printed 4, start 5. This one command does it for you:
for i in $(seq 1 $(( $(nproc) + 1 ))); do yes > /dev/null & done
Then watch for 10 seconds:
top
You'll see every yes process sharing the CPU: each gets less than 100%. That's the scheduler giving everyone a fair turn!
⚠️ Stop them all right away (this is important): press q to leave top, then:
pkill yes
jobs
Everything is quiet again. (If jobs still lists things, run pkill yes once more.)
🌡️ Don't leave busy processes running. They make your computer warm and noisy. Always clean up your experiments!
🧪 Lab 4: priorities with nice
Some jobs are more important. Linux uses a number called nice, from -20 (very important) to 19 (very polite and happy to wait). Normal programs have 0.
nice -n 19 yes > /dev/null &
ps -o pid,ni,cmd -C yes
The NI column shows 19. This process is so polite that it only uses the CPU when nobody else needs it. Stop it:
pkill yes
🧪 Lab 5: how long did that take?
time sleep 2
time measures how long a command took. You'll see real close to 2 seconds, but user and sys close to 0, because sleep was waiting, not working. Compare with a quick calculation:
time ls /usr/bin > /dev/null
What did you learn?
| What you saw | What it means |
|---|---|
yes used ~100% of one core |
One process can keep a core busy |
Many yes shared the CPU |
The scheduler gave everyone a turn |
nice 19 waited politely |
The scheduler can use priorities |
🧑🍳 More cores = more chefs. A 4-core computer can really do four things at the same moment, and juggle many more.
Words to know
| Word | What it means |
|---|---|
| Multitasking | Running many programs by taking quick turns |
| Scheduler | The OS part that decides which process runs next |
| Time slice | The tiny turn a process gets on the CPU |
| Nice value | A priority number: lower is more important |
