Author: Didier Kryn Date: To: dng@lists.dyne.org Subject: Re: [DNG] great intro to init systems, by Laurence Bercot,
author of s6
Le 01/12/2025 à 02:14, Steve Litt a écrit : > sbin/init, if that's what it's called,*is* PID1, unless it exec's to
> something else
More detailed explanations:
1 is the pid of the process launched by the kernel after boot. Obviously
it's the first. It is free to create other processes, by the fork()
system-call; but it remains there until shutdown. If it dies, there is a
"kernel panic", followed, after 1mn, by reboot.
There are two system-calls inlvoved: fork(), which creates a new
process, and execve() which changes the program that the process
executes. A process can call execve() as many times as usefull; this may
lead it to successively perform very different tasks while remaining the
same process with the same pid. Most pid1 never call execve(), but
there's nothing against.
The shell command 'exec' does perform execve() only; it does not
create a new process. Instead, if the shell invokes a command without
prefixing it with exec, then it calls fork() to create a new process and
waits (waitpid() system-call) for its termination; while the new process
executes the proper execve() call to run the desired command.
There is also a new function to invoke a command as a new process:
posix_spawn(), which allows to write programs running even on systems
which lack the fork() system-call. posix_spawn() combines the effects of
fork() and execve(). I don't know if Linux has a system-call for it or
if the C library implements it by a combination of fork() and execve().