
New Video from @Computerphile Explains Multitasking in Operating Systems
In this video, the host explores how a simple operating system can load and run two programs in parallel, using components and concepts discussed in previous episodes. The goal is to show how these elements come together to make a computer with a single CPU appear to run multiple programs simultaneously.
The host begins by recalling that each program must have an independent view of memory. To achieve this, the operating system must remap or present a specific view of memory to each program while managing the details in the background. He uses pieces of paper to represent the memory pages of two processes, called process zero and process one. Each process has its own page table, which maps virtual addresses to physical memory addresses.
The operating system initially sets up the page table for process zero, loads the Fibonacci program into memory, and adjusts privileges to switch from supervisor mode to user mode before starting the program. The Fibonacci program begins to execute, but to allow another program to run as well, the host introduces the concept of cooperative and preemptive multitasking.
Cooperative multitasking, used in older operating systems, relies on programs voluntarily yielding control of the CPU. However, this method is prone to errors if a program does not yield control, making the system unresponsive. To avoid this, modern systems use preemptive multitasking, where the operating system forces programs to share CPU time.
To implement preemptive multitasking, the host introduces the idea of a timer, similar to a kitchen timer, which interrupts the CPU at regular intervals. When the timer expires, it triggers an interrupt, forcing the CPU to save the current program's state and switch to another program. This process is called context switching.
The host then explains how the operating system handles interrupts to switch between processes. When an interrupt occurs, the state of the current program (registers, stack pointer, etc.) is saved in the process table, and the operating system switches to another process. The timer is reset, and the process repeats, giving the impression that both programs are running simultaneously.
Finally, the host discusses the practical implications of context switching and interrupt handling. He mentions that most programs spend the majority of their time waiting for external events, such as keyboard inputs or file reads, allowing the operating system to efficiently manage CPU resources. He also emphasizes that modern operating systems optimize this process to minimize the time spent switching between processes.
In conclusion, the video provides a comprehensive overview of how an operating system can manage the execution of multiple programs on a single CPU, using concepts such as memory management, interrupts, and context switching. These concepts are fundamental to understanding how modern computers can efficiently run multiple tasks in parallel.