Multitasking is Not as Magical as You Might Think
- Modern computers stream music, download files, and browse the internet simultaneously with a single CPU on the board. This seemingly magical ability is called multitasking, a cornerstone of modern operating systems. It may make computers seem as if they are magical beings, but it is not so different from how we humans multitask.
The Magic Behind the Scenes: Time-Sharing and Context Switching
- With a single CPU, true simultaneous execution of multiple tasks is not possible. Instead, the operating system employs a clever technique called time-sharing, which lets the CPU switch between tasks rapidly. This happens so instantly that tasks are running concurrently.
- When switching between tasks, the operating system saves the current progress and state of the active task and moves on to the next task by loading the saved progress and state of the next task. This is called context switching. This allows computers to pick up exactly where they left off on each task, ensuring a smooth transition and continuous progress.
The Foundation of Multitasking: Processes
- To define what these 'tasks' are, we must discuss processes and threads. When a program runs, it is a process. When an application launches, the OS creates a process and allocates the necessary resources.
- A key characteristic of a process is its isolation. Each process is granted its own independent memory space, a dedicated share of CPU time, and other system resources. This isolation is crucial for stability because it won't shut down the entire system in case of a process crash. The OS acts as a protector that keeps processes from accessing others' memory, creating a secure and reliable multitasking environment.
More and More: Threads
- To further enhance efficiency, a process can be broken down into smaller units of execution called threads. If a process is a program in action, a thread is a distinct execution path within that program. A single process can contain multiple threads, all working in parallel to accomplish different parts of the main task.
- Unlike processes, which are isolated from one another, threads within the same process share the resources allocated to that process. This shared environment allows for efficient communication and data exchange between threads. However, each thread maintains its unique stack, registers, and program counter to prevent collision.
Top comments (0)