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.11—266
xnu-1504.15.3—266
xnu-1504.3.12—266
xnu-1504.7.4—266
xnu-1504.9.17—266
xnu-1504.9.26—266
xnu-1504.9.37—266
xnu-1699.22.73—266
xnu-1699.22.81—266
xnu-1699.24.23—266
xnu-1699.26.8—266
xnu-1699.32.7—266
xnu-2050.22.13—266
xnu-2050.24.15—266
xnu-2050.48.11—266
xnu-2050.7.9—266
xnu-2050.9.2—266

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