The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

shm_open - open a shared memory object (REALTIME)

 SYNOPSIS



#include <sys/mman.h>

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

 DESCRIPTION

The shm_open() function 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. The file descriptor is used by other functions to refer to that shared memory object. The name argument points to a string naming a shared memory object. It is unspecified whether the name appears in the file system and is visible to other functions that take pathnames as arguments. The name argument conforms to the construction rules for a pathname. If name begins with the slash character, then processes calling shm_open() with the same value of name refer to the same shared memory object, as long as that name has not been removed. If name does not begin with the slash character, the effect is implementation-dependent. The interpretation of slash characters other than the leading slash character in name is implementation-dependent.

If successful, shm_open() 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. It is unspecified whether the file offset is set. 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 oflag. The oflag argument is the bitwise inclusive OR of the following flags defined in the header <fcntl.h>. Applications specify exactly one of the first two values (access modes) below in the value of oflag:

O_RDONLY
Open for read access only.
O_RDWR
Open for read or write access.

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

O_CREAT
If the shared memory object exists, this flag has no effect, except as noted under O_EXCL below. Otherwise the shared memory object is created; the user ID of the shared memory object will be set to the effective user ID of the process; the group ID of the shared memory object will be set to a system default group ID or to the effective group ID of the process. The permission bits of the shared memory object will be set to the value of the mode argument except those set in the file mode creation mask of the process. When bits in mode other than the file permission bits are set, the effect is unspecified. The mode argument does not affect whether the shared memory object is opened for reading, for writing, or for both. The shared memory object has a size of zero.
O_EXCL
If O_EXCL and O_CREAT are set, shm_open() fails if the shared memory object exists. The check for the existence of the shared memory object and the creation of the object if it does not exist is atomic with respect to other processes executing shm_open() naming the same shared memory object with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the result is undefined.
O_TRUNC
If the shared memory object exists, and it is successfully opened O_RDWR, the object will be truncated to zero length and the mode and owner will be unchanged by this function call. The result of using O_TRUNC with O_RDONLY is undefined.

When a shared memory object is created, the state of the shared memory object, including all data associated with the shared memory object, persists until the shared memory object is unlinked and all other references are gone. It is unspecified whether the name and shared memory object state remain valid after a system reboot.

 RETURN VALUE

Upon successful completion, the shm_open() function returns a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, it returns -1 and sets errno to indicate the error.

 ERRORS

The shm_open() function will fail if:
[EACCES]
The shared memory object exists and the permissions specified by oflag are denied, or the shared memory object does not exist and permission to create the shared memory object is denied, or O_TRUNC is specified and write permission is denied.
[EEXIST]
O_CREAT and O_EXCL are set and the named shared memory object already exists.
[EINTR]
The shm_open() operation was interrupted by a signal.
[EINVAL]
The shm_open() operation is not supported for the given name.
[EMFILE]
Too many file descriptors are currently in use by this process.
[ENAMETOOLONG]
The length of the name string exceeds {PATH_MAX}, or a pathname component is longer than {NAME_MAX} while _POSIX_NO_TRUNC is in effect.
[ENFILE]
Too many shared memory objects are currently open in the system.
[ENOENT]
O_CREAT is not set and the named shared memory object does not exist.
[ENOSPC]
There is insufficient space for the creation of the new shared memory object.
[ENOSYS]
The function shm_open() is not supported by this implementation.

 EXAMPLES

None.

 APPLICATION USAGE

None.

 FUTURE DIRECTIONS

None.

 SEE ALSO

close(), dup(), exec, fcntl(), mmap(), shmat(), shmctl(), shmdt(), shm_unlink(), umask(), <fcntl.h>, <sys/mman.h>.

DERIVATION

Derived from the POSIX Realtime Extension (1003.1b-1993/1003.1i-1995)

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]