Multitasking
# Multitasking
Multitasking is the ability to execute multiple tasks concurrently. Only a single task can be executed on a CPU core at a time. To create the illusion that the tasks run in parallel, the operating system rapidly switches between active tasks so that each one can make a bit of progress.
# Pre-emptive Multitasking
The operating system controls when to switch tasks. This is made possible by interrupts, which allows the OS to regain control of the CPU on each interrupt. Since tasks are interrupted at arbitrary points in time, they might be in the middle of some calculations. In order to be able to resume them later, the operating system must backup the whole state of the task, including its call stack and the values of all CPU registers via a context switch.
# Advantages
- make it possible to run untrusted userspace programs
# Disadvantages
- each task requires its own stack. Compared to a shared stack, this results in higher memory usage per task and often limits the number of tasks in the system
- the operating system always has to save the complete CPU register state on each task switch, even if the task only used a small subset of the registers.
# Cooperative Multitasking
Instead of forcibly pausing running tasks at arbitrary points in time, cooperative multitasking lets each task run until it voluntarily gives up control of the CPU. This allows tasks to pause themselves at convenient points in time, for example, when they need to wait for an I/O operation anyway.
Cooperative multitasking is often used at the language level, like in the form of coroutinesor async/await. The idea is that either the programmer or the compiler inserts yield operations into the program, which give up control of the CPU and allow other tasks to run. For example, a yield could be inserted after each iteration of a complex loop.
# Advantages
- Since tasks define their pause points themselves, they don’t need the operating system to save their state. Instead, they can save exactly the state they need for continuation before they pause themselves, which often results in better performance.
# Disadvantages
- an uncooperative task can potentially run for an unlimited amount of time. Thus, a malicious or buggy task can prevent other tasks from running and slow down or even block the whole system