Usage of XPC Framework

As the description of XPC says that it can be used for inter process communication mechanism, what exactly the inter process means?

Can it be used to create shared memory between any type of processes (ex. Swift Process (App) and any other language process) or it is for strictly App to App (swift to swift) communication?

Answered by DTS Engineer in 704782022

macOS supports a variety of Unix-y shared memory APIs, including mmap, POSIX shared memory (see the shm_open man page), and System V shared memory (see the shmget man page).

IMPORTANT macOS’s support for System V IPC is intended for compatibility only. If you’re writing new code, avoid that API.

The only thing to watch out for here is that the App Sandbox can restrict all forms of IPC, including shared memory. Whether that’s a concern depends on your specific circumstances. Which brings us to …

is there code reference available for this?

Can your post a quick summary of your architecture? What sort of product are you developing? An app? A daemon? Something more complex?

For shared memory to make sure you must have two different processes. What contexts are those processes running in?

IMPORTANT Execution context is really important on macOS because there are significant differences between it and other Unix-y platforms. See Technote 2083 Daemons and Agents for an in-depth explanation of that.

And any other requirements? Do you have existing shared memory code that you plan to reuse?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

XPC is a general-purpose IPC system with some restrictions.

On the generality side:

  • The language involved doesn’t matter. You can use XPC from both C-based languages and Swift.

  • There’s no requirement that the processes involved be apps.

  • The focus is on IPC messaging but you can use those messages to carry shared memory regions if you want.

On the restrictions side:

  • XPC has two APIs, the low-level C API (<xpc/xpc.h>) and the Objective-C API (NSXPCConnection). The C API is usable from any language that can interoperate with C, including C, C++, Objective-C, Objective-C++, and Swift. The Objective-C API is easily usable from Objective-C and Swift; using it from other languages is not impossible but it’s not easy either.

  • XPC is available, but not generally useful, on iOS. There are a few specific use cases which require XPC on iOS, but most of the use cases are on the Mac.

  • To published a named listener your process must be managed by launchd. That means an XPC Service, a Service Management login item, a launchd daemon, or a launchd agent, but not an app [1]. This means that it’s not possible to use XPC between two independent apps without some sort of intermediary.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Technically apps are managed by launchd on modern systems but the key thing here is that launchd does not control the app’s lifecycle, for example, it can’t launch the app in response to XPC demand.

Accepted Answer

macOS supports a variety of Unix-y shared memory APIs, including mmap, POSIX shared memory (see the shm_open man page), and System V shared memory (see the shmget man page).

IMPORTANT macOS’s support for System V IPC is intended for compatibility only. If you’re writing new code, avoid that API.

The only thing to watch out for here is that the App Sandbox can restrict all forms of IPC, including shared memory. Whether that’s a concern depends on your specific circumstances. Which brings us to …

is there code reference available for this?

Can your post a quick summary of your architecture? What sort of product are you developing? An app? A daemon? Something more complex?

For shared memory to make sure you must have two different processes. What contexts are those processes running in?

IMPORTANT Execution context is really important on macOS because there are significant differences between it and other Unix-y platforms. See Technote 2083 Daemons and Agents for an in-depth explanation of that.

And any other requirements? Do you have existing shared memory code that you plan to reuse?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Usage of XPC Framework
 
 
Q