shm_open Subroutine

Purpose

Opens a shared memory object.

Library

Standard C Library (libc.a)

Syntax

#include <sys/mman.h>

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

Description

The shm_open subroutine establishes a connection between a shared memory object and a file descriptor. It creates an open file description that refers to the shared memory object and a file descriptor that refers to that open file description. This file descriptor is used by other subroutines to refer to that shared memory object.

The name parameter points to a string naming a shared memory object. The name parameter does not appear in the file system and is not visible to other subroutines that take pathnames as arguments.The name parameter must conform to the construction rules for a pathname.

If successful, the shm_open subroutine returns a file descriptor for the shared memory object that is the lowest numbered file descriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not share it with any other processes. The FD_CLOEXEC file descriptor flag associated with the new file descriptor is set.

The file status flags and file access modes of the open file description are according to the value of the oflag parameter. The oflag parameter is the bitwise-inclusive OR of the following flags defined in the fcntl.h header file.

Parameters

Item Description
name Points to a string naming a shared memory object.
oflag Specifies the flags to be used by the shm_open subroutine.
mode Sets the value of the permission bits of the shared memory object.

Read-Write Flags

Applications specify exactly one of the first two values (access modes) below in the value of the oflag parameter:
Item Description
O_RDONLY Open for read access only.
O_RDWR Open for read or write access.

Other Flags

Any combination of the remaining flags may be specified in the value of the oflag parameter:

Item Description
O_CREAT If the shared memory object exists, this flag has no effect, except as noted under the O_EXCL flag below. Otherwise, the shared memory object is created, the user ID of the shared memory object is set to the effective user ID of the process, and the group ID of the shared memory object is set to the effective group ID of the process. The permission bits of the shared memory object are set to the value of the mode parameter except those set in the file mode creation mask of the process. Only the low-order 9 bits of the mode parameter are taken into account.The shared memory object has a size of zero.
O_EXCL If the O_EXCL and O_CREAT flags are set, the shm_open subroutine fails if the shared memory object exists. The O_EXCL flag is ignored if the O_CREAT flag is not set.
O_TRUNC If the shared memory object exists and it is successfully opened, the O_RDWR flag, the object is truncated to zero length, and the mode and owner is unchanged by the shm_open call.

Return Values

Upon successful completion, the shm_open subroutine returns a non-negative integer representing the lowest numbered unused file descriptor. If unsuccessful, it returns -1 and sets errno to indicate the error.

Error Codes

Item Description
EACCES The shared memory object exists and the permissions specified by the oflag parameter are denied, or the shared memory object does not exist and permission to create the shared memory object is denied, or the O_TRUNC flag is specified and write permission is denied.
EEXIST The O_CREAT and O_EXCL flags are set and the named shared memory object already exists.
EINVAL The shm_open subroutine is not supported for an empty name string, or the name parameter is missing, or the oflag parameter contains an invalid value.
EFAULT The name parameter points outside of the allocated address space of the process.
EMFILE Too many file descriptors are currently in use by this process.
ENAMETOOLONG The length of the name parameter exceeds PATH_MAX or a pathname component is longer than NAME_MAX.
ENFILE Too many shared memory objects are currently open in the system.
ENOENT The O_CREAT flag is not set and the named shared memory object does not exist.
ENOMEM The system is unable to allocate resources.
ENOTSUP This function is not supported with processes that have been checkpoint-restart'ed.
ENOSPC There is insufficient space for the creation of the new shared memory object