01Why Does It Look Like "Running Simultaneously"?
You might listen to music, browse web pages, and have chat software and download tasks running at the same time. These programs all appear to be "running simultaneously."
But "all running" doesn't mean "every instruction of every program is executing at the same moment." The operating system arranges for many tasks to take turns getting CPU time, switching so quickly that we feel they are all working together.
OS Scheduling
Constantly deciding who runs next
CPU
Executes scheduled threads
02What's the Relationship Between Programs, Processes, and Threads?
Program files on disk are just code and data. When a program is launched, the operating system creates a process.
What actually gets scheduled and executed by the CPU are typically threads within a process. A process can have just one thread or many threads.
Program File
Statically saved on disk
Process
Has its own address space and running resources
CPU Schedules Threads
Threads execute after getting processor time
03How Does the Operating System Decide Who Uses the CPU First?
The operating system kernel has scheduling mechanisms. It decides which thread gets CPU time next based on factors like whether threads are ready, priority levels, and system policies.
Ready Queue
Scheduler
Selects the next thread to run
Running
Selected thread gets CPU
04How Does the CPU Quickly Switch Tasks?
Suppose the CPU is executing a browser thread, and now the operating system decides to execute the music thread instead. It can't simply "forget where the browser left off."
The system needs to save the important running state of the current thread, then restore the previously saved state of another thread. This type of switching is called context switching.
Browser Running
Registers hold current execution state
Save State
Save necessary registers and other context
Restore Another Thread
Restore music thread's previous running state
Continue Execution
Music thread continues from appropriate position
05What Does the CPU Do When Programs Are Waiting?
Many programs don't always need the CPU. They often wait for network, disk, keyboard input, or other devices.
Browser
Waiting for website data to return
Enters Waiting State
Temporarily doesn't need CPU
CPU Does Other Tasks
Executes other ready threads
When network data, disk data, or other events are ready, waiting threads can become "ready" again and later get CPU time again.
06Do Multi-Core CPUs Make Tasks Truly Execute Simultaneously?
Yes. If the CPU has multiple cores, different cores can truly execute different threads at the same moment.
Single Core
Can only execute one instruction stream at a time, but can switch quickly
Multi-Core
Multiple cores can truly execute multiple threads in parallel
07Why Do Computers Still Slow Down With Too Many Programs?
Multitasking is not without cost. The more programs, the more intense the competition for CPU, memory, storage, and other resources may become.
CPU Too Busy
Too many ready threads competing for limited processor time
Insufficient Memory
System may need to reclaim, compress, or swap out memory pages more frequently
Storage Busy
Many programs reading/writing disk simultaneously interfere with each other
Switching Has Costs
Context switching itself takes time
So the operating system's goal is not "make all programs infinitely fast," but to make the system responsive, fair with resources, and overall efficient with limited hardware resources.
What You've Learned
- Computers can keep many programs in a running state, which is called multitasking or concurrency.
- What actually gets scheduled and executed by the CPU are typically threads, not the program files themselves on disk.
- The operating system scheduler constantly decides which ready threads get CPU time.
- When switching threads, the system saves and restores necessary running states, called context switching.
- When programs wait for network, disk, or input, they can usually temporarily give up the CPU.
- Single-core CPUs can achieve concurrency through fast switching, while multi-core CPUs can also truly execute multiple threads in parallel.
- Concurrency is not parallelism; "many programs are running" doesn't mean "all programs are executing instructions at this moment."
- Too many programs still slow down the computer because CPU, memory, storage, and switching all have costs.
Next Question: If the operating system already schedules programs, why do computers sometimes suddenly become very laggy? Next article: Why Do Computers Sometimes Slow Down?