Seth Woolley's Man Viewer

shm_open(3) - shm_open, shm_unlink, shm_open, shm_unlink - Create/open or unlink POSIX shared memory objects - man 3 shm_open

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

SHM_OPEN(3)                Linux Programmer's Manual               SHM_OPEN(3)



NAME
       shm_open,  shm_unlink  -  Create/open(2,3,n)  or  unlink(1,2)  POSIX  shared memory
       objects

SYNOPSIS
       #include <sys/types.h>
       #include <sys/mman.h>

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

       int shm_unlink(const char *name);

DESCRIPTION
       shm_open creates and opens a new, or opens an  existing,  POSIX  shared
       memory  object.   A  POSIX  shared  memory object is in(1,8) effect a handle
       which can be used by unrelated processes to mmap(2) the same region  of
       shared  memory.   The  shm_unlink function performs the converse opera-
       tion, removing an object previously created by shm_open.

       The operation of shm_open is analogous to that of open(2,3,n)(2).  name speci-
       fies  the  shared  memory object to be created or opened.  For portable
       use, name should have an initial slash  (/)  and  contain  no  embedded
       slashes.

       oflag  is  a bit mask created by ORing together exactly one of O_RDONLY
       or O_RWDR and any of the other flags listed here:

       O_RDONLY   Open the object for read(2,n,1 builtins) access.   A  shared  memory  object
                  opened   in(1,8)   this  way  can  only  be  mmap(2)ed  for  read(2,n,1 builtins)
                  (PROT_READ) access.

       O_RDWR     Open the object for read-write access.

       O_CREAT    Create the shared memory object if(3,n) it does not  exist.   The
                  user  and  group  ownership  of  the  object  are set(7,n,1 builtins) as for
                  open(2,3,n)(2), and the object's permission bits are set(7,n,1 builtins)  according
                  to  the low-order 9 bits of mode, except that those bits set(7,n,1 builtins)
                  in(1,8) the process file(1,n) mode creation mask  (see  umask(2))  are
                  cleared for the new object.  (A set(7,n,1 builtins) of macro constants which
                  can be used to define mode is listed in(1,8) open(2,3,n)(2).)

                  A new shared memory object initially has zero length  -  the
                  size  of  the  object  can  be set(7,n,1 builtins) using ftruncate(2).  (The
                  newly-allocated bytes of a shared memory object are automat-
                  ically initialised to 0.)

       O_EXCL     If  O_CREAT  was  also  specified, and a share memory object
                  with the given name already exists, return  an  error.   The
                  check  for  the existence of the object, and its creation if(3,n)
                  it does not exist, are performed atomically.

       O_TRUNC    If the shared memory object already exists, truncate(2,7)  it  to
                  zero bytes.

       On  successful completion shm_open returns a new file(1,n) descriptor refer-
       ring to the shared memory object.  This file(1,n) descriptor  is  guaranteed
       to  be the lowest-numbered file(1,n) descriptor not previously opened within
       the process.  The FD_CLOEXEC flag (see fcntl(2)) is set(7,n,1 builtins)  for  the  file(1,n)
       descriptor.

       The  file(1,n)  descriptor  is  normally  used in(1,8) subsequent calls to ftrun-
       cate(2) (for a newly-created object) and  mmap(2).   After  a  call  to
       mmap(2)  the file(1,n) descriptor may be closed without affecting the memory
       mapping.

       The operation of shm_unlink is analogous to  unlink(1,2)(2):  it  removes  a
       shared  memory  object  name, and, once all processes have unmapped the
       object, de-allocates and destroys the contents of the associated memory
       region.   After a successful shm_unlink, attempts to shm_open an object
       with the same name will fail (unless O_CREAT was  specified,  in(1,8)  which
       case a new, distinct object is created).

RETURN VALUE
       On  success, shm_open returns a non-negative file(1,n) descriptor.  On fail-
       ure, shm_open returns -1.  shm_unlink returns 0 on success,  or  -1  on
       error.

ERRORS
       On  failure,  errno  is set(7,n,1 builtins) to indicate the cause of the error.  Values
       which may appear in(1,8) errno include the following:

       EACCES Permission to shm_unlink the shared memory object was denied.

       EACCES Permission was denied to shm_open name in(1,8) the specified mode, or
              O_TRUNC was specified and the caller does not have write(1,2) permis-
              sion on the object.

       EEXIST Both O_CREAT and O_EXCL  were  specified  to  shm_open  and  the
              shared memory object specified by name already exists.

       EINVAL The name argument to shm_open was invalid.

       EMFILE The process already has the maximum number of files open.

       ENAMETOOLONG
              The length of name exceeds PATH_MAX.

       ENFILE The  limit  on  the total number of files open(2,3,n) on the system has
              been reached.

       ENOENT An attempt was made to shm_open a name that did not  exist,  and
              O_CREAT was not specified.

       ENOENT An attempt was to made to shm_unlink a name that does not exist.

NOTES
       These functions are provided in(1,8) glibc 2.2 and  later.   Programs  using
       these  functions  must  specify  the  -lrt  flag to cc in(1,8) order to link(1,2)
       against the required ("realtime") library.

       POSIX leaves the behavior of the combination of  O_RDONLY  and  O_TRUNC
       unspecified.   On  Linux,  this  will successfully truncate(2,7) an existing
       shared memory object - this may not be so on other Unices.

       The POSIX shared memory object implementation on Linux 2.4 makes use of
       a dedicated file(1,n) system, which is normally mounted under /dev/shm.

CONFORMING TO
       POSIX 1003.1 (2001).

SEE ALSO
       close(2,7,n)(2),   fchmod(2),  fchown(2),  fcntl(2),  fstat(2),  ftruncate(2),
       mmap(2), open(2,3,n)(2), umask(2)



Linux 2.4                         2002-02-22                       SHM_OPEN(3)

References for this manual (incoming links)