XNU内核案例(二)第一个用户进程Launchd
- 苹果Mac OS X系统上,第一个进程是kernel_task ------ 内核进程(这个完全不同于Linux系统),PID为0;第二个进程是launchd(相当于linux的init进程),PID为1。
- 可以把kernel_task当作第0个进程,launchd当作第1个进程。
launchd:
- The preferred method is on-demand launching,
- but
launchd
can launch daemons that run continuously, - and can replace
inetd
for launchinginetd
-style daemons. - In addition,
launchd
can start jobs at timed intervals.
launchd
supports non-launch-on-demand daemons, this use is not recommended. The launchd
daemon was designed to remove the need for dependency ordering among daemons. If you do not make your daemon be launched on demand, you will have to handle these dependencies in another way, such as by using the legacy startup item mechanism.launchd
in OS X v10.4, an effort was made to improve the steps needed to launch and maintain daemons. What launchd
provides is a harness for launching your daemon as needed. To client programs, the port representing your daemon’s service is always available and ready to handle requests. In reality, the daemon may or may not be running. When a client sends a request to the port, launchd
may have to launch the daemon so that it can handle the request. Once launched, the daemon can continue running or shut itself down to free up the memory and resources it holds. If a daemon shuts itself down, launchd
once again relaunches it as needed to process requests.In addition to the launch-on-demand feature, launchd
provides the following benefits to daemon developers:
-
Simplifies the process of making a daemon by handling many of the standard housekeeping chores normally associated with launching a daemon.
-
Provides system administrators with a central place to manage daemons on the system.
-
Supports
inetd
-style daemons. -
Eliminates the primary reason for running daemons as root. Because
launchd
runs as root, it can create low-numbered TCP/IP listen sockets and hand them off to the daemon. -
Simplifies error handling and dependency management for inter-daemon communication. Because daemons launch on demand, communication requests do not fail if the daemon is not launched. They are simply delayed until the daemon can launch and process them.
Launchd的配置文件
/Library/LaunchDaemons
, and those describing agents are installed in /Library/LaunchAgents
or in the LaunchAgents
subdirectory of an individual user’s Library
directory. (The appropriate location for executables that you launch from your job is /usr/local/libexec
.)
Key |
Description |
---|---|
|
Contains a unique string that identifies your daemon to |
|
Contains the arguments used to launch your daemon. (required) |
|
Indicates that your daemon requires a separate instance per incoming connection. This causes |
|
This key specifies whether your daemon launches on-demand or must always be running. It is recommended that you design your daemon to be launched on-demand. |
<key>StandardErrorPath</key>
<string>/var/log/myjob.log</string>
<key>Debug</key>
<true/>
<key>Keepalive</key> <true/> <key>StartInterval</key>
<integer>300</integer>
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>45</integer>
<key>Hour</key>
<integer>13</integer>
<key>Day</key>
<integer>7</integer>
</dict>
<key>SoftResourceLimits</key>
<dict>
<key>Core</key>
<integer>9223372036854775807</integer>
</dict>
<dict>
<key>Wait</key>
<false/>
</dict>
launchd
takes care of dependencies between daemons, in some cases, your daemon may depend on other system functionality that cannot be addressed in this manner. This section describes many of these special cases and how to handle them.launchd
daemon, you must take additional care to ensure that your daemon works correctly if that non-launchd
daemon has not started when your daemon is started. The best way to do this is to include a loop at start time that checks to see if the non-launchd
daemon is running, and if not, sleeps for several seconds before checking again.SIGTERM
prior to this loop to ensure that you are able to properly shut down if the daemon you rely on never becomes available.郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。