Follow

While we're dumping knowledge, what does it mean to be the active task?

A CPU core is only able to execute one instruction at a time*. In order to support multitasking, providing the illusion that several tasks are running at once, it has to use task switching. This can be cooperative or preemptive, the former meaning that the task decides when it gets switched out with another, and the latter meaning that the task is pre-empted, or control is taken away from it. Cooperative multitasking, also called green threads or coroutines, is popular in userspace with things like asyncio.

Preemptive multitasking usually works by firing an interrupt on a clock. An interrupt is an external signal to the CPU to suspend execution of the current task and jump to the kernel's interrupt handler. The kernel can choose where to jump back to when this happens - including a different task. So on every clock interrupt it's cycling between tasks, tens of thousands of times per second.

Multi-core or multi-CPU systems can do more than one thing at a time, but if the output of ps aux | wc -l is greater than the number of cores in your machine, then it's using preemptive multitasking to make it work.

* Kind of, there's pipelining and branch prediction and all sorts of other stuff. But definitely only one process or thread at a time, probably.

@sir
I have a great idea! How about we use some spare CPU cycles to speculate what might come next in the pipeline. If we're right then it's a great speed up. If we're wrong then no harm no foul. What could go wrong?

/sarcasm

Sign in to participate in the conversation
Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!