ArticleslgStudy

computer science

Launchd

Launchd is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Launchd rather than just read about it. In short: launchd is an init and operating system service management daemon created by Apple Inc. as part of macOS, iOS, iPadOS, watchOS, tvOS, and visionOS to replace its BSD-style init and SystemStarter. There have been efforts to port launchd to FreeBSD and derived systems.

Key takeaways

  • Launchd belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Launchd to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Launchd from memory before moving on to harder problems.

Reference excerpt

launchd is an init and operating system service management daemon created by Apple Inc. as part of macOS, iOS, iPadOS, watchOS, tvOS, and visionOS to replace its BSD-style init and SystemStarter. There have been efforts to port launchd to FreeBSD and derived systems.

Components There are two main programs in the launchd system: launchd and launchctl. launchd manages the daemons at both a system and user level. Similar to xinetd, launchd can start daemons on demand. Similar to watchdogd, launchd can monitor daemons to make sure that they keep running. launchd also has replaced init as PID 1 on macOS and as a result it is responsible for starting the system at boot time. Configuration files define the parameters of services run by launchd. Stored in the LaunchAgents and LaunchDaemons subdirectories of the Library folders, the property list-based files have approximately thirty different keys that can be set. launchd itself has no knowledge of these configuration files or any ability to read them - that is the responsibility of "launchctl". launchctl is a command line application which talks to launchd using IPC and knows how to parse the property list files used to describe launchd jobs, serializing them using a specialized dictionary protocol that launchd understands. launchctl can be used to load and unload daemons, start and stop launchd controlled jobs, get system utilization statistics for launchd and its child processes, and set environment settings.

launchd

launchd has two main tasks. The first is to boot the system, and the second is to load and maintain services. Here is a simplified view of the Mac OS X Tiger system startup on a PowerPC Mac (on an Intel Mac, EFI replaces Open Firmware and boot.efi replaces BootX):

Open Firmware activates, initializes the hardware, and then loads BootX. BootX loads the kernel, spins the pinwheel cursor, and loads any needed kernel extensions (kexts). The kernel loads launchd. launchd runs /etc/rc, various scripts which scan through /System/Library/LaunchDaemons and /Library/LaunchDaemons, calling launchctl on the plists as needed, then launchd starts the login window. In step 4, the startup scripts scan through a few different directories for jobs to run. There are two different directories that are scanned:

The LaunchDaemons directories contain items that will run as root, generally background processes. The LaunchAgents directories contain jobs, called agent applications, that will run as a user or in the context of userland. These may be scripts or other foreground items, and they can even include a user interface. These directories are all kept in the typical Library directories of Mac OS X. launchd is very different from SystemStarter in that it may not actually launch all the daemons at boot time. Key to launchd, and similar to xinetd, is the idea of launch-on-demand daemons. When launchctl scans through the job plists at boot time, it asks launchd to reserve and listen on all of the ports requested by those jobs. If so indicated in the plist by the "OnDemand" key, the daemon is not actually loaded at the time. Rather, launchd will listen on the port, start the daemon when needed, and shut it down when it is no longer needed. After a daemon is loaded, launchd will keep track of it and make sure it is running if needed. In this way it is like watchdogd, and shares watchdogd's requirement that processes do not attempt to fork or daemonize on their own. If a process goes into the background, launchd will lose track of it and attempt to relaunch it. Mac OS X Tiger, consequently, boots much faster than previous releases. The system only has to register the daemons that are to run and does not actually launch them until they are needed. In fact, the progress bar that appears during boot time is just a placebo application (named WaitingForLoginWindow) that does not really show anything other than the passage of time. The hardest part to manage during a launchd boot is dependencies. SystemStarter had a very simple system of dependencies that used the "Uses", "Requires", and "Provides" keys in the plist of a startup item. There are two main strategies when creating launchd dependencies on Tiger: IPC allows daemons to talk amongst themselves to work out dependencies, or daemons can watch files or paths for changes. Using IPC is much more subtle than the SystemStarter's keys and requires more work from the developer, but it may lead to cleaner and quicker startups. SystemStarter was still supported up to OS X Mountain Lion, but was removed in OS X Yosemite.

launchctl In launchd, control of services is centralized in the launchctl application. Originally, launchctl could take commands from the command line, from the standard input, or operate in interactive mode. With superuser privileges, it allowed the user to make system-wide changes, and commands could be made permanent when stored in /etc/launchd.conf. (A per-user ~/.launchd.conf file appears to have been considered, but was never implemented.) In later versions of macOS, interactive mode was removed, as was the ability to read commands from standard input, and the /etc/launchd.conf file is no longer supported. The reason given for these changes is security considerations. Today, launchctl works exclusively through command-line invocations and persistence is achieved through launchd .plist files in the appropriate locations (see below). launchctl communicates with launchd via a Mach-specific IPC mechanism.

Property list A property list (plist) is a type of file that launchd uses for program configuration. When launchd scans a folder, or a job is submitted with launchctl, it reads a plist file that describes how the program is to be run. A list of often used keys follows below. All keys are optional unless otherwise noted. For a full list, see Apple's manual page for launchd.plist.

Socket activation protocol The name of each key under Sockets will be placed into the environment of the job when it is run, and the file descriptor of that socket will be available in that environment variable. This differs from systemd's socket activation in that the name of a socket definition inside of the job configuration is hardcoded into the application. This protocol is less flexible, although it does not, as systemd does, require the daemon to hardcode a starting file descriptor (as of 2014, it is 3).

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Launchd

Start with the simplest possible case. Write down what Launchd claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Launchd before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Launchd ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Launchd

In research
Launchd appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Launchd in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Launchd is common in secondary-school and first-year university syllabi. It links to neighbouring topics MacOS, Software using the Apache license, Unix process- and task-management-related software, so understanding it makes those chapters shorter.
In everyday life
Look for Launchd outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Launchd” →

How to study Launchd in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Launchd means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Launchd out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Launchd in simple terms?

launchd is an init and operating system service management daemon created by Apple Inc. as part of macOS, iOS, iPadOS, watchOS, tvOS, and visionOS to replace its BSD-style init and SystemStarter. There have been efforts to port launchd to FreeBSD and derived systems.

Why does Launchd matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Launchd?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Launchd.

Tags

  • MacOS
  • Software using the Apache license
  • Unix process- and task-management-related software

Keep exploring