Which process has PID 0?
I’m looking for the process started in Linux which has process ID 0. I know init has PID 1 , which is the first process in Linux, is there any process with PID 0?
3 Answers 3
From the wikipedia page titled: Process identifier:
There are two tasks with specially distinguished process IDs: swapper or sched has process ID 0 and is responsible for paging, and is actually part of the kernel rather than a normal user-mode process. Process ID 1 is usually the init process primarily responsible for starting and shutting down the system. Originally, process ID 1 was not specifically reserved for init by any technical measures: it simply had this ID as a natural consequence of being the first process invoked by the kernel. More recent Unix systems typically have additional kernel components visible as ‘processes’, in which case PID 1 is actively reserved for the init process to maintain consistency with older systems.
You can see the evidence of this if you look at the parent PIDs (PPID) of init and kthreadd :
$ ps -eaf UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jun24 ? 00:00:02 /sbin/init root 2 0 0 Jun24 ? 00:00:00 [kthreadd]
kthreadd is the kernel thread daemon. All kthreads are forked from this thread. You can see evidence of this if you look at other processes using ps and seeing who their PPID is:
$ ps -eaf root 3 2 0 Jun24 ? 00:00:57 [ksoftirqd/0] root 4 2 0 Jun24 ? 00:01:19 [migration/0] root 5 2 0 Jun24 ? 00:00:00 [watchdog/0] root 15 2 0 Jun24 ? 00:01:28 [events/0] root 19 2 0 Jun24 ? 00:00:00 [cpuset] root 20 2 0 Jun24 ? 00:00:00 [khelper]
If computers start counting at 0, why does the init process have a pid of 1?
pid 0 has a special meaning for the kill(2) system call where it means myself and for waitpid(2) where it means any process in my process group at least. Not to mention that fork() returning 0 means that we’re in the child.
Zero causes flags to be automatically set on most CPU’s status registers when it is loaded in a register, enabling a branch to taken on it without specifically comparing/testing for it. Therefore it’s very much used as a «sentinel» value, i.e. a value that means «invalid», «end of data», or «special case» here. So even though computers start counting at 0, there will be many cases where 1 is the first valid value for the application or data structure in question.
A side note: Computers don’t start counting at zero. Many programming languages and I believe all machine languages use offsets where others (and most humans) use indexes but, no matter what, counting is counting. An array with two elements has two elements (count them), no matter whether your language refers to them by offset or by index.
this is likely a case of opposing underlying paradigms of programming: the programmer’s gravitation towards special flag values (zero, negative one) trumps the lesser pull of a proclivity for zero-based numbering systems.
3 Answers 3
Processes need to have a parent (PPID). The kernel, despite not being a real process, is nevertheless handcrafting some real processes like at least init, and is giving itself the process ID 0. Depending on the OS it might or might not be displayed as a process in ps output but is always displayed as a PPID:
$ ps -ef|head UID PID PPID C STIME TTY TIME CMD root 1 0 0 09:09 ? 00:00:00 /sbin/init root 2 0 0 09:09 ? 00:00:00 [kthreadd] root 3 2 0 09:09 ? 00:00:00 [ksoftirqd/0] .
$ ps -ef|head UID PID PPID C STIME TTY TIME CMD root 0 0 0 Oct 19 ? 0:01 sched root 5 0 0 Oct 19 ? 11:20 zpool-rpool1 root 1 0 0 Oct 19 ? 0:13 /sbin/init root 2 0 0 Oct 19 ? 0:07 pageout root 3 0 1 Oct 19 ? 117:10 fsflush root 341 1 0 Oct 19 ? 0:15 /usr/lib/hal/hald --daemon=yes root 9 1 0 Oct 19 ? 0:59 /lib/svc/bin/svc.startd .
Note also that pid 0 (and -1 and other negative values for that matter) have different meanings depending on what function use them like kill , fork and waitpid .
Finally, while the init process is traditionally given pid #1 , this is no more the case when OS level virtualization is used like Solaris zones, as there can be more than one init running:
$ ps -ef|head UID PID PPID C STIME TTY TIME CMD root 4733 3949 0 11:07:25 ? 0:26 /lib/svc/bin/svc.configd root 4731 3949 0 11:07:24 ? 0:06 /lib/svc/bin/svc.startd root 3949 3949 0 11:07:14 ? 0:00 zsched daemon 4856 3949 0 11:07:46 ? 0:00 /lib/crypto/kcfd root 4573 3949 0 11:07:23 ? 0:00 /usr/sbin/init netcfg 4790 3949 0 11:07:34 ? 0:00 /lib/inet/netcfgd root 4868 3949 0 11:07:48 ? 0:00 /usr/lib/pfexecd root 4897 3949 0 11:07:51 ? 0:00 /usr/lib/utmpd netadm 4980 3949 0 11:07:54 ? 0:01 /lib/inet/nwamd
In the linux kernel, where is the first process initialized?
I’m looking for the code in the linux kernel (2.4.x) that initializes the first process, pid=0. Many searches provided many clues, but I still cannot find it. Any pointers, anyone?
3 Answers 3
The initial task struct is set up by the macro INIT_TASK() , defined in include/linux/init_task.h . All other task structs are created by do_fork .
This should be the accepted answer. I would add that init/init_task.c also sets up the thread_info and kernel stack for PID 0 (on the boot CPU only) using INIT_THREAD_INFO() . Then, for the x86 architecture, startup_32 (in arch/x86/kernel/head_32.S ) loads a pointer to the PID 0 kernel stack into ESP. From that point on, Linux is in «process context» for PID 0.
check out rest_init() at the end
// idle process, pid = 0 cpu_idle(); // never return
Thanks, I was already looking at that page, so now I know I was on the right path. Where in there is the first task_struct created and populated?
The problem I was asked to solve was to add a field to task_struct. I’d like to initialize it, and have each fork() copy it, but can’t find where the initial value is set.
The first process that the kernel initializes is the swapper process or the idle thread. This thread runs forever. When no other process is active in the system, then this thread [which is cpu_idle() function found in arch/arm/kernel/process.c for the ARM architecture] calls the architecture dependent pm_idle function, which power collapses the CPU until a timer interrupt or some other interrupt wakes it up.
The swapper process [pid=0] is initialized in arch/arm/kernel/init_task.c by the macro INIT_TASK.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Linux init process pid
We are private in protest of the API changes. https://www.theverge.com/2023/6/8/23754780/reddit-api-updates-changes-news-announcements Don’t message us for access, everyone is blocked out site-wide. See http://redd.it/1476ioa for more info.
After explaining about PID 0, now we are going to talk about PID 1. Mostly known as “init”. init is the first Linux user-mode process created, which runs until the system shuts down. init manages the services (called demons under Linux, more on them in a future post). Also, if we check the process tree of a Linux machine we will find that the root of the tree is init.
There are multiple implementations for init, each of them provide different advantages among them are: SysVinit, launched, systemd, runit, upstart, busybox-init and OpenRC (those are examples only and not a full list). Thus, based on the implementation specific configuration files are read (such as /etc/inittab — SysVinit), different command/tools to manage demons (such as service — SysVinit and systemctl — systemd), and different scripts/profiles might be executed during the boot process (runlevels of SysVinit vs targets in systemd).
The creation of init is done by the kernel function “rest_init” ( https://elixir.bootlin.com/linux/latest/source/init/main.c#L680 — shows the source code). In the code we can see the call to “user_mode_thread” which spawns init, later in the function there is a call to “kernel_thread” which creates PID 2 (but that is for our next post ;-).
Now we will go over a couple of fun facts about init. First, in case a parent process exits before all of its children process, init adopts those child processes. Second, only the signals which have been explicitly installed with a handler can be sent to init. Thus, sending “kill -9 1” won’t do anything in most distributions (try it and see nothing happens). Remember that different init implementations handle signals in different ways.
Because they are multiple init implementations (as we stated before) we can determine the one installed in the following manner. We can perform “ls -l /sbin/init”. If it is not a symlink it is probably SysVinit, else if it points to “/lib/systemd/systmed” than systemd is in use (and of course they are other symlinks to the other implementation — you can read about it in the documentation of each init implementation).
As you can see in the attached screenshot Ubuntu 22.04 uses systemd:
reading the link “/sbin/init” to check which init implementation is in use. In this case it is systemd