Skip to content

Threads#

Now that the process has been loaded into memory we can see what's happening across the bottom of the diagram: the threads are being scheduled for execution on the CPU.

The process keeps a table of the threads it has created (processes have at least one thread but they can create multiple threads) and these threads are ordered in terms of priority (defined by the process.) The operating system - the kernel - then reads this table and executes the thread on the CPU.

The act of running a thread on the CPU is called scheduling. The kernel takes care of this and it has to balance your process' threads with the threads of every other process on the system. This is called "context switching" and you can see it in the diagram above. What this means is a thread is only given so much time on the CPU, then it's stopped, put to the side (in RAM), and another thread is then executed.

Now if you've been following along and you're connecting all the dots, you mgiht have realised that a CPU has multiple cores... what executes on a core? A thread. So if a CPU has multiple cores that means it must be able to run multiple threads at the same time? That's correct, it can.

The thread contains a sub-set of the instructions that we had inside our binary on disk. Those instructions are executed and can do virtually anything a computer is capable of: connect to a network, render someting on the screen, create a file, and so much more.

So in summary a process is a program that was loaded from your disk into RAM. The process then creates at least one thread which is then executed on the CPU. This thread runs the instructions from inside the program. The thread can access memory to store and use information.

A process can create multiple threads which can run across multiple CPU cores.

When a process runs and creates threads, the process is known as the "parent" and the threads as "children".