📄 shm_open.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>shm_open</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="shm_open"></a> <a name="tag_03_673"></a><!-- shm_open --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_673_01"></a>NAME</h4><blockquote>shm_open - open a shared memory object (<b>REALTIME</b>)</blockquote><h4><a name="tag_03_673_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('SHM')">SHM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/sys/mman.h.html">sys/mman.h</a>><br><br> int shm_open(const char *</tt><i>name</i><tt>, int</tt> <i>oflag</i><tt>, mode_t</tt> <i>mode</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_673_03"></a>DESCRIPTION</h4><blockquote><p>The <i>shm_open</i>() function shall establish a connection between a shared memory object and a file descriptor. It shallcreate an open file description that refers to the shared memory object and a file descriptor that refers to that open filedescription. The file descriptor is used by other functions to refer to that shared memory object. The <i>name</i> argument pointsto a string naming a shared memory object. It is unspecified whether the name appears in the file system and is visible to otherfunctions that take pathnames as arguments. The <i>name</i> argument conforms to the construction rules for a pathname. If<i>name</i> begins with the slash character, then processes calling <i>shm_open</i>() with the same value of <i>name</i> refer tothe same shared memory object, as long as that name has not been removed. If <i>name</i> does not begin with the slash character,the effect is implementation-defined. The interpretation of slash characters other than the leading slash character in <i>name</i>is implementation-defined.</p><p>If successful, <i>shm_open</i>() shall return a file descriptor for the shared memory object that is the lowest numbered filedescriptor not currently open for that process. The open file description is new, and therefore the file descriptor does not shareit with any other processes. It is unspecified whether the file offset is set. The FD_CLOEXEC file descriptor flag associated withthe new file descriptor is set.</p><p>The file status flags and file access modes of the open file description are according to the value of <i>oflag</i>. The<i>oflag</i> argument is the bitwise-inclusive OR of the following flags defined in the <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a> header. Applications specify exactly one of the first two values (accessmodes) below in the value of <i>oflag</i>:</p><dl compact><dt>O_RDONLY</dt><dd>Open for read access only.</dd><dt>O_RDWR</dt><dd>Open for read or write access.</dd></dl><p>Any combination of the remaining flags may be specified in the value of <i>oflag</i>:</p><dl compact><dt>O_CREAT</dt><dd>If the shared memory object exists, this flag has no effect, except as noted under O_EXCL below. Otherwise, the shared memoryobject is created; the user ID of the shared memory object shall be set to the effective user ID of the process; the group ID ofthe shared memory object is set to a system default group ID or to the effective group ID of the process. The permission bits ofthe shared memory object shall be set to the value of the <i>mode</i> argument except those set in the file mode creation mask ofthe process. When bits in <i>mode</i> other than the file permission bits are set, the effect is unspecified. The <i>mode</i>argument does not affect whether the shared memory object is opened for reading, for writing, or for both. The shared memory objecthas a size of zero.</dd><dt>O_EXCL</dt><dd>If O_EXCL and O_CREAT are set, <i>shm_open</i>() fails if the shared memory object exists. The check for the existence of theshared memory object and the creation of the object if it does not exist is atomic with respect to other processes executing<i>shm_open</i>() naming the same shared memory object with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, theresult is undefined.</dd><dt>O_TRUNC</dt><dd>If the shared memory object exists, and it is successfully opened O_RDWR, the object shall be truncated to zero length and themode and owner shall be unchanged by this function call. The result of using O_TRUNC with O_RDONLY is undefined.</dd></dl><p>When a shared memory object is created, the state of the shared memory object, including all data associated with the sharedmemory object, persists until the shared memory object is unlinked and all other references are gone. It is unspecified whether thename and shared memory object state remain valid after a system reboot.</p></blockquote><h4><a name="tag_03_673_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, the <i>shm_open</i>() function shall return a non-negative integer representing the lowest numberedunused file descriptor. Otherwise, it shall return -1 and set <i>errno</i> to indicate the error.</p></blockquote><h4><a name="tag_03_673_05"></a>ERRORS</h4><blockquote><p>The <i>shm_open</i>() function shall fail if:</p><dl compact><dt>[EACCES]</dt><dd>The shared memory object exists and the permissions specified by <i>oflag</i> are denied, or the shared memory object does notexist and permission to create the shared memory object is denied, or O_TRUNC is specified and write permission is denied.</dd><dt>[EEXIST]</dt><dd>O_CREAT and O_EXCL are set and the named shared memory object already exists.</dd><dt>[EINTR]</dt><dd>The <i>shm_open</i>() operation was interrupted by a signal.</dd><dt>[EINVAL]</dt><dd>The <i>shm_open</i>() operation is not supported for the given name.</dd><dt>[EMFILE]</dt><dd>Too many file descriptors are currently in use by this process.</dd><dt>[ENAMETOOLONG]</dt><dd>The length of the <i>name</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.</dd><dt>[ENFILE]</dt><dd>Too many shared memory objects are currently open in the system.</dd><dt>[ENOENT]</dt><dd>O_CREAT is not set and the named shared memory object does not exist.</dd><dt>[ENOSPC]</dt><dd>There is insufficient space for the creation of the new shared memory object.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_673_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_673_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_673_08"></a>RATIONALE</h4><blockquote><p>When the Memory Mapped Files option is supported, the normal <a href="../functions/open.html"><i>open</i>()</a> call is used toobtain a descriptor to a file to be mapped according to existing practice with <a href="../functions/mmap.html"><i>mmap</i>()</a>.When the Shared Memory Objects option is supported, the <i>shm_open</i>() function shall obtain a descriptor to the shared memoryobject to be mapped.</p><p>There is ample precedent for having a file descriptor represent several types of objects. In the POSIX.1-1990 standard, a filedescriptor can represent a file, a pipe, a FIFO, a tty, or a directory. Many implementations simply have an operations vector,which is indexed by the file descriptor type and does very different operations. Note that in some cases the file descriptor passedto generic operations on file descriptors is returned by <a href="../functions/open.html"><i>open</i>()</a> or <a href="../functions/creat.html"><i>creat</i>()</a> and in some cases returned by alternate functions, such as <a href="../functions/pipe.html"><i>pipe</i>()</a>. The latter technique is used by <i>shm_open</i>().</p><p>Note that such shared memory objects can actually be implemented as mapped files. In both cases, the size can be set after theopen using <a href="../functions/ftruncate.html"><i>ftruncate</i>()</a>. The <i>shm_open</i>() function itself does not create ashared object of a specified size because this would duplicate an extant function that set the size of an object referenced by afile descriptor.</p><p>On implementations where memory objects are implemented using the existing file system, the <i>shm_open</i>() function may beimplemented using a macro that invokes <a href="../functions/open.html"><i>open</i>()</a>, and the <a href="../functions/shm_unlink.html"><i>shm_unlink</i>()</a> function may be implemented using a macro that invokes <a href="../functions/unlink.html"><i>unlink</i>()</a>.</p><p>For implementations without a permanent file system, the definition of the name of the memory objects is allowed not to survivea system reboot. Note that this allows systems with a permanent file system to implement memory objects as data structures internalto the implementation as well.</p><p>On implementations that choose to implement memory objects using memory directly, a <i>shm_open</i>() followed by an <a href="../functions/ftruncate.html"><i>ftruncate</i>()</a> and <a href="../functions/close.html"><i>close</i>()</a> can be used topreallocate a shared memory area and to set the size of that preallocation. This may be necessary for systems without virtualmemory hardware support in order to ensure that the memory is contiguous.</p><p>The set of valid open flags to <i>shm_open</i>() was restricted to O_RDONLY, O_RDWR, O_CREAT, and O_TRUNC because these could beeasily implemented on most memory mapping systems. This volume of IEEE Std 1003.1-2001 is silent on the results if theimplementation cannot supply the requested file access because of implementation-defined reasons, including hardware ones.</p><p>The error conditions [EACCES] and [ENOTSUP] are provided to inform the application that the implementation cannot complete arequest.</p><p>[EACCES] indicates for implementation-defined reasons, probably hardware-related, that the implementation cannot comply with arequested mode because it conflicts with another requested mode. An example might be that an application desires to open a memoryobject two times, mapping different areas with different access modes. If the implementation cannot map a single area into aprocess space in two places, which would be required if different access modes were required for the two areas, then theimplementation may inform the application at the time of the second open.</p><p>[ENOTSUP] indicates for implementation-defined reasons, probably hardware-related, that the implementation cannot comply with arequested mode at all. An example would be that the hardware of the implementation cannot support write-only shared memoryareas.</p><p>On all implementations, it may be desirable to restrict the location of the memory objects to specific file systems forperformance (such as a RAM disk) or implementation-defined reasons (shared memory supported directly only on certain file systems).The <i>shm_open</i>() function may be used to enforce these restrictions. There are a number of methods available to theapplication to determine an appropriate name of the file or the location of an appropriate directory. One way is from theenvironment via <a href="../functions/getenv.html"><i>getenv</i>()</a>. Another would be from a configuration file.</p><p>This volume of IEEE Std 1003.1-2001 specifies that memory objects have initial contents of zero when created. This isconsistent with current behavior for both files and newly allocated memory. For those implementations that use physical memory, itwould be possible that such implementations could simply use available memory and give it to the process uninitialized. This,however, is not consistent with standard behavior for the uninitialized data area, the stack, and of course, files. Finally, it ishighly desirable to set the allocated memory to zero for security reasons. Thus, initializing memory objects to zero isrequired.</p></blockquote><h4><a name="tag_03_673_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_673_10"></a>SEE ALSO</h4><blockquote><p><a href="close.html"><i>close</i>()</a> , <a href="dup.html"><i>dup</i>()</a> , <a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> , <a href="fcntl.html"><i>fcntl</i>()</a> , <a href="mmap.html"><i>mmap</i>()</a> , <ahref="shmat.html"><i>shmat</i>()</a> , <a href="shmctl.html"><i>shmctl</i>()</a> , <a href="shmdt.html"><i>shmdt</i>()</a> , <ahref="shm_unlink.html"><i>shm_unlink</i>()</a> , <a href="umask.html"><i>umask</i>()</a> , the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, <a href="../basedefs/sys/mman.h.html"><i><sys/mman.h></i></a></p></blockquote><h4><a name="tag_03_673_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 5. Included for alignment with the POSIX Realtime Extension.</p></blockquote><h4><a name="tag_03_673_12"></a>Issue 6</h4><blockquote><p>The <i>shm_open</i>() function is marked as part of the Shared Memory Objects option.</p><p>The [ENOSYS] error condition has been removed as stubs need not be provided if an implementation does not support the SharedMemory Objects option.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -