Brendan Ang

Search

Search IconIcon to open search

Multitasking

Last updated Jul 21, 2023 Edit Source

# 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

# Disadvantages

# 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

# Disadvantages