Why does UNIX use the init process to start other processes?
In the Unix system, when the system starts, the kernel will start the first user space process after initialization.init
process.init
The process is responsible for starting and managing other user space processes, and the kernel itself does not handle these tasks directly. Why does Unix use this design instead of letting the kernel directly responsible for starting all processes? This article will analyze the logic and advantages behind this design from multiple angles.
0. Does UNIX really use the init process?
RTFS, by observing the Linux kernel source code (here), You can find this code:
if (!try_to_run_init_process("/sbin/init") ||
!try_to_run_init_process("/etc/init") ||
!try_to_run_init_process("/bin/init") ||
!try_to_run_init_process("/bin/sh"))
return 0;
panic("No working init found. Try passing init= option to kernel. "
"See Linux Documentation/admin-guide/ for guidance.");
This shows that Linux will really try to start an init process, and even "strike" if the startup fails.
Observe simultaneouslypstree
Output:
VM: ~ > pstree
systemd─┬─ModemManager───2*[{ModemManager}]
├─acpid
├─2*[agetty]
├─dbus-daemon
├─mysqld───48*[{mysqld}]
├─nginx───2*[nginx]
├─sshd───sshd───sshd───bash───pstree
├─systemd-journal
├─systemd-logind
...
It can be found that all the processes are from Systemd's growth differentiation.
1. Why doesn’t the kernel start all processes directly?
Although the kernel has the ability to start the process, letting the kernel directly start all processes does not conform to UNIX's design philosophy.
-
Separation
The life cycle management of the process (such as creation and scheduling)KernelResponsible for deciding which processes need to be started and their configuration is left to user spaceinit processmanage. This can keep the kernel simple and reduce complexity. -
flexibility
passinit
Or other alternative plans, users can dynamically adjust the logic of the system, such as defining different operating levels or the starting sequence of custom services (systemctl
To. And handling these logic to the kernel will limit this flexibility. -
Avoid kernel code bloat
If the kernel is directly responsible for starting all user processes, it will have to deal with a large number of complex logic, such as service dependence, configuration file analysis, etc. This can lead to a huge and difficult to maintain the kernel code.
2. Responsibility separation and modular design
The UNIX operating system follows the design philosophy of separation of responsibilities and modularize different functions:
-
init
Responsible:High-level logic, such as system initialization and service management, determines which services or processes need to be started and in what order. -
The kernel is responsible for:Provide underlying mechanisms (e.g.
fork
、exec
) To achieve process creation and scheduling.
By handing the high -level logic of process management to the user spaceinit
The process is responsible, and the kernel can maintain the management of the underlying resource management, which simplifies its own design and implementation.
3. Flexibility and configurability
init
The process is the program of the user space. It can be configured or replaced by the system administrator to other initialization systems (such assystemd
orupstart
To. This flexibility allows different UNIX systems to customize its initial logic according to demand without changing the kernel.
> ls /usr/sbin/init -l
lrwxrwxrwx 1 root root 20 Dec 17 04:23 /usr/sbin/init -> /lib/systemd/systemd
-
Kernel tasks:Start the first user space process (PID is 1
init
)。 -
init
Task:Configuration system, start service, management operation level.
This design avoids the logic of the system initialized into the kernel, making it easier for the system to maintain and expand.
4. Summary
UNIX selectinit
The process starts and manages other processes, rather than directly completed by the kernel. This is to achieve separation of responsibilities, enhance flexibility, simplify the kernel design and improve the maintenance of the system.
This design not only follows the modular philosophy of UNIX, but also provides a foundation for system scalability and flexibility, reflecting the simplicity and elegance of UNIX design.