Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/sem_open.html
2024-02-19 00:21:47 -05:00

225 lines
9.8 KiB
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 The Open Group, All Rights Reserved -->
<title>sem_open</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="sem_open"></a> <a name="tag_03_632"></a><!-- sem_open -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_632_01"></a>NAME</h4>
<blockquote>sem_open - initialize and open a named semaphore (<b>REALTIME</b>)</blockquote>
<h4><a name="tag_03_632_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('SEM')">SEM</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/semaphore.h.html">semaphore.h</a>&gt;<br>
<br>
sem_t *sem_open(const char *</tt><i>name</i><tt>, int</tt> <i>oflag</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_632_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>sem_open</i>() function shall establish a connection between a named semaphore and a process. Following a call to
<i>sem_open</i>() with semaphore name <i>name</i>, the process may reference the semaphore associated with <i>name</i> using the
address returned from the call. This semaphore may be used in subsequent calls to <a href=
"../functions/sem_wait.html"><i>sem_wait</i>()</a>, <a href="../functions/sem_trywait.html"><i>sem_trywait</i>()</a>, <a href=
"../functions/sem_post.html"><i>sem_post</i>()</a>, and <a href="../functions/sem_close.html"><i>sem_close</i>()</a>. The semaphore
remains usable by this process until the semaphore is closed by a successful call to <a href=
"../functions/sem_close.html"><i>sem_close</i>()</a>, <a href="../functions/_exit.html"><i>_exit</i>()</a>, or one of the <i><a
href="../functions/exec.html">exec</a></i> functions.</p>
<p>The <i>oflag</i> argument controls whether the semaphore is created or merely accessed by the call to <i>sem_open</i>(). The
following flag bits may be set in <i>oflag</i>:</p>
<dl compact>
<dt>O_CREAT</dt>
<dd>This flag is used to create a semaphore if it does not already exist. If O_CREAT is set and the semaphore already exists, then
O_CREAT has no effect, except as noted under O_EXCL. Otherwise, <i>sem_open</i>() creates a named semaphore. The O_CREAT flag
requires a third and a fourth argument: <i>mode</i>, which is of type <b>mode_t</b>, and <i>value</i>, which is of type
<b>unsigned</b>. The semaphore is created with an initial value of <i>value</i>. Valid initial values for semaphores are less than
or equal to {SEM_VALUE_MAX}.
<p>The user ID of the semaphore is set to the effective user ID of the process; the group ID of the semaphore is set to a system
default group ID or to the effective group ID of the process. The permission bits of the semaphore are set to the value of the
<i>mode</i> argument except those set in the file mode creation mask of the process. When bits in <i>mode</i> other than the file
permission bits are specified, the effect is unspecified.</p>
<p>After the semaphore named <i>name</i> has been created by <i>sem_open</i>() with the O_CREAT flag, other processes can connect
to the semaphore by calling <i>sem_open</i>() with the same value of <i>name</i>.</p>
</dd>
<dt>O_EXCL</dt>
<dd>If O_EXCL and O_CREAT are set, <i>sem_open</i>() fails if the semaphore <i>name</i> exists. The check for the existence of the
semaphore and the creation of the semaphore if it does not exist are atomic with respect to other processes executing
<i>sem_open</i>() with O_EXCL and O_CREAT set. If O_EXCL is set and O_CREAT is not set, the effect is undefined.
<p>If flags other than O_CREAT and O_EXCL are specified in the <i>oflag</i> parameter, the effect is unspecified.</p>
</dd>
</dl>
<p>The <i>name</i> argument points to a string naming a semaphore object. It is unspecified whether the name appears in the file
system and is visible to functions 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>sem_open</i>() with the same value of
<i>name</i> shall refer to the same semaphore 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 a process makes multiple successful calls to <i>sem_open</i>() with the same value for <i>name</i>, the same semaphore
address shall be returned for each such successful call, provided that there have been no calls to <a href=
"../functions/sem_unlink.html"><i>sem_unlink</i>()</a> for this semaphore.</p>
<p>References to copies of the semaphore produce undefined results.</p>
</blockquote>
<h4><a name="tag_03_632_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, the <i>sem_open</i>() function shall return the address of the semaphore. Otherwise, it shall return
a value of SEM_FAILED and set <i>errno</i> to indicate the error. The symbol SEM_FAILED is defined in the <a href=
"../basedefs/semaphore.h.html"><i>&lt;semaphore.h&gt;</i></a> header. No successful return from <i>sem_open</i>() shall return the
value SEM_FAILED.</p>
</blockquote>
<h4><a name="tag_03_632_05"></a>ERRORS</h4>
<blockquote>
<p>If any of the following conditions occur, the <i>sem_open</i>() function shall return SEM_FAILED and set <i>errno</i> to the
corresponding value:</p>
<dl compact>
<dt>[EACCES]</dt>
<dd>The named semaphore exists and the permissions specified by <i>oflag</i> are denied, or the named semaphore does not exist and
permission to create the named semaphore is denied.</dd>
<dt>[EEXIST]</dt>
<dd>O_CREAT and O_EXCL are set and the named semaphore already exists.</dd>
<dt>[EINTR]</dt>
<dd>The <i>sem_open</i>() operation was interrupted by a signal.</dd>
<dt>[EINVAL]</dt>
<dd>The <i>sem_open</i>() operation is not supported for the given name, or O_CREAT was specified in <i>oflag</i> and <i>value</i>
was greater than {SEM_VALUE_MAX}.</dd>
<dt>[EMFILE]</dt>
<dd>Too many semaphore descriptors or 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 semaphores are currently open in the system.</dd>
<dt>[ENOENT]</dt>
<dd>O_CREAT is not set and the named semaphore does not exist.</dd>
<dt>[ENOSPC]</dt>
<dd>There is insufficient space for the creation of the new named semaphore.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_632_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_632_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The <i>sem_open</i>() function is part of the Semaphores option and need not be available on all implementations.</p>
</blockquote>
<h4><a name="tag_03_632_08"></a>RATIONALE</h4>
<blockquote>
<p>Early drafts required an error return value of -1 with the type <b>sem_t *</b> for the <i>sem_open</i>() function, which is not
guaranteed to be portable across implementations. The revised text provides the symbolic error code SEM_FAILED to eliminate the
type conflict.</p>
</blockquote>
<h4><a name="tag_03_632_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_632_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="semctl.html"><i>semctl</i>()</a> , <a href="semget.html"><i>semget</i>()</a> , <a href="semop.html"><i>semop</i>()</a>
, <a href="sem_close.html"><i>sem_close</i>()</a> , <a href="sem_post.html"><i>sem_post</i>()</a> , <a href=
"sem_timedwait.html"><i>sem_timedwait</i>()</a> , <a href="sem_trywait.html"><i>sem_trywait</i>()</a> , <a href=
"sem_unlink.html"><i>sem_unlink</i>()</a> , <a href="sem_wait.html"><i>sem_wait</i>()</a> , the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/semaphore.h.html"><i>&lt;semaphore.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_632_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_632_12"></a>Issue 6</h4>
<blockquote>
<p>The <i>sem_open</i>() function is marked as part of the Semaphores option.</p>
<p>The [ENOSYS] error condition has been removed as stubs need not be provided if an implementation does not support the Semaphores
option.</p>
<p>The <a href="../functions/sem_timedwait.html"><i>sem_timedwait</i>()</a> function is added to the SEE ALSO section for alignment
with IEEE&nbsp;Std&nbsp;1003.1d-1999.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>