The Linux Process Journey — PID 0 (swapper)

Shlomi Boutnaru, Ph.D.
3 min readAug 16, 2022

--

When starting to learn OS internals we must understand the default processes executing (roles, tasks, etc). Because of that I am going to start a series of posts named “Process ID Card” (aimed at providing the OS vocabulary). Based on a poll I have conducted I am going to start with Linux (Windows will follow).

In order to create the list of processes I want to explain, I have installed a clean Ubuntu 22.04 VM (Desktop version) and executed ps. Probably the best way to do it is to go over the processes by the order of their PID value.
The first one I want to talk about is the one we can’t see on the list, that is PID 0 (we can see it is the PPID for PID 1 and PID 2 — on them in the next posts). So let us dive into the details about PID 0, aka swapper (but also has the nicknames of sched and idle, based on the different roles it had over the years). First, it is not a normal user mode process, it is rather part of the kernel (we are going to talk about kernel threads in the following posts).

Historically, old Unix systems used swapping and not demand paging. So, swapper was responsible for the “Swap Process” — moving all pages of a specific process from/to memory/backing store (including related process’ kernel data structures). In the case of Linux PID 0 was used as the “idle process”, simply does not do anything (like nops). It was there so Linux will always have something that a CPU can execute (for cases that a CPU can’t be stopped to save power). By the way, the idle syscall is not supported since kernel 2.3.13 (for more info check out “man 2 idle”). So what is the current purpose of swapper today? helping with pageout ? cache flushes? idling? buffer zeroning? I promise we will answer it in more detail while going through the other processes and explaining the relationship between them.

But how can you believe that swapper (PID 0) even exists? if you can’t see it using ps. I am going to use “bpftrace” for demonstrating that (if you don’t know about bpftrace, I strongly encourage you to read about it). In the demo I am going to trace the kernel function “hrtimer_wakeup” which is responsible for waking up a process and move it to the set of runnable processes. During the trace I am going to print the pid of the calling process (BTW, in the kernel everything is called a task — more on that in future posts) and the executable name (the comm field of the task_struct [/include/linux/sched.h]). Here is the command:

sudo bpftrace -e ‘kfunc:hrtimer_wakeup { printf(“%s:%d\n”,curtask->comm,curtask->pid); }’

From the output we can see we have 3 instances of swapper: swapper/0, swapper/1 and swapper/2 all of them with PID 0. The reason we have three is because my VM has 3 virtual CPUs and there is a swapper process for each one of them — see the output of the command in the image below.

bpftrace output (showing swapper instance on each CPU)

You can also follow me on twitter — Shlomi Boutnaru (@boutnaru) / Twitter

See you all at PID 1 ;-)

--

--