Skip to content
BSD syscall#266

svc · unix #266

shm_open

Opens (or creates) a named POSIX shared-memory object and returns a file descriptor.

Prototype

int shm_open(const char *name, int oflag, int mode);

Returns: int

Arguments

NameTypeDirDescription
nameconst char-
oflagint-
modeint-

Version history

XNU tagmacOS#
xnu-1456.1.26macOS 10.6 Snow Leopard266
xnu-1699.24.8macOS 10.7 Lion266
xnu-2050.18.24macOS 10.8 Mountain Lion266
xnu-1486.2.11266
xnu-1504.15.3266
xnu-1504.3.12266
xnu-1504.7.4266
xnu-1504.9.17266
xnu-1504.9.26266
xnu-1504.9.37266
xnu-1699.22.73266
xnu-1699.22.81266
xnu-1699.24.23266
xnu-1699.26.8266
xnu-1699.32.7266
xnu-2050.22.13266
xnu-2050.24.15266
xnu-2050.48.11266
xnu-2050.7.9266
xnu-2050.9.2266

Examples

C — create and map

int fd = shm_open("/sample.shm", O_CREAT | O_RDWR, 0600);
ftruncate(fd, 0x4000);
void *p = mmap(NULL, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Notes

On macOS shm_open(2) is backed by a tmpfs-like in-kernel object — there is no visible file on disk despite the leading slash convention in the name. The resulting fd is sized with ftruncate(2) and then mapped with mmap(2, MAP_SHARED). The sandbox profile must grant the (ipc-posix-shm-read*/ipc-posix-shm-write*) operation for cross-process sharing; XPC daemons and the WebContent process are typical examples.

Related APIs

shm_unlinkmmapftruncatemach_make_memory_entryxpc_shmem_create