ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/ipcsemo.htm

421 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>sem_open()--Open Named Semaphore</title>
<!-- Begin Header Records -->
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Change History: -->
<!-- YYMMDD USERID Change description -->
<!-- Direct1 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
<!-- Edited by Kersten Feb 02 -->
<!-- This file has undergone html cleanup on 05/01/02 by JET -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--End Header Records -->
<!-- Java sync-link -->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<a name="Top_Of_Page"></a>
<h2>sem_open()--Open Named Semaphore</h2>
<div class="box" style="width: 80%;">
<br>
&nbsp;&nbsp;Syntax
<pre>
#include &lt;semaphore.h&gt;
sem_t * sem_open(const char *<em>name</em>, int <em>oflag</em>, ...);
</pre>
<br>
&nbsp;&nbsp;Service Program Name: QP0ZPSEM<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>sem_open()</strong> function opens a named semaphore, returning
a semaphore pointer that may be used on subsequent calls to <strong>
sem_post()</strong>, <strong>sem_post_np()</strong>, <strong>
sem_wait()</strong>, <strong>sem_wait_np()</strong>, <strong>
sem_trywait()</strong>, <strong>sem_getvalue()</strong>, and <strong>
sem_close()</strong>. When a semaphore is being created, the parameters <em>
mode</em> and <em>value</em> must be specified on the call to <strong>
sem_open()</strong>. If a semaphore is created, then the maximum value of the
semaphore is set to SEM_VALUE_MAX and the title of the semaphore is set to the
last 16 characters of the name.</p>
<p>If <strong>sem_open()</strong> is called multiple times within the same
process using the same name, <strong>sem_open()</strong> will return a pointer
to the same semaphore, as long as another process has not used <strong>
sem_unlink()</strong> to unlink the semaphore.</p>
<p>If <strong>sem_open()</strong> is called from a program using data model
LLP64, the returned semaphore pointer must be declared as a <samp>sem_t
*__ptr128</samp>.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>name</strong></dt>
<dd>(Input) A pointer to the null-terminated name of the semaphore to be
opened. The name should begin with a slash ('/') character. If the name does
not begin with a slash ('/') character, the system adds a slash to the
beginning of the name.
<p>This parameter is assumed to be represented in the CCSID (coded character
set identifier) currently in effect for the job. If the CCSID of the job is
65535, this parameter is assumed to be represented in the default CCSID of the
job.</p>
<p>The name is added to a set of names that is used only by named semaphores.
The name has no relationship to any file system path names. The maximum length
of the name is SEM_NAME_MAX.</p>
<p>See <a href="ipcsemou.htm">QlgSem_open()</a>--Open Named Semaphore (using
NLS-enabled path name) for a description and an example of supplying the <em>
name</em> in any CCSID.</p>
</dd>
<dt><strong>oflag</strong></dt>
<dd>(Input) Option flags.
<p>The <em>oflag</em> parameter value is either zero or is obtained by
performing an OR operation on one or more of the following constants:</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<td align="left" valign="top"><em>'0x0008' or O_CREAT</em></td>
<td align="left" valign="top">Creates the named semaphore if it does not
already exist.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0010' or O_EXCL</em></td>
<td align="left" valign="top">Causes <strong>sem_open()</strong> to fail if
O_CREAT is also set and the named semaphore already exists.</td>
</tr>
</table>
<br>
</dd>
<dt><strong>mode</strong></dt>
<dd>(input) Permission flags.
<p>The <em>mode</em> parameter value is either zero or is obtained by
performing an OR operation on one or more of the following list of constants.
For another process to open the semaphore, the process's effective UIDd must be
able to open the semaphore in both read and write mode.</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<td align="left" valign="top"><em>'0x0100' or S_IRUSR</em></td>
<td align="left" valign="top">Permits the creator of the named semaphore to
open the semaphore in read mode.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0080' or S_IWUSR</em></td>
<td align="left" valign="top">Permits the creator of the named semaphore to
open the semaphore in write mode.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0020' or S_IRGRP</em></td>
<td align="left" valign="top">Permits the group associated with the named
semaphore to open the semaphore in read mode.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0010' or S_IWGRP</em></td>
<td align="left" valign="top">Permits the group associated with the named
semaphore to open the semaphore in write mode.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0004' or S_IROTH</em></td>
<td align="left" valign="top">Permits others to open the named semaphore in
read mode.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>'0x0002' or S_IWOTH</em></td>
<td align="left" valign="top">Permits others to open the named semaphore in
write mode.<br>
<br>
</td>
</tr>
</table>
<br>
</dd>
<dt><strong>value</strong></dt>
<dd>(Input) Initial value of the named semaphore.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p><strong><a name="TBLASEMO1">Authorization required for
sem_open()</a></strong></p>
<table border cellpadding="5">
<!-- cols="60 20 20" -->
<tr>
<th align="left" valign="bottom">Object Referred to</th>
<th align="left" valign="bottom">Authority Required</th>
<th align="left" valign="bottom">errno</th>
</tr>
<tr>
<td align="left" valign="top">Named semaphore to be created</td>
<td align="left" valign="top">None</td>
<td align="left" valign="top">None</td>
</tr>
<tr>
<td align="left" valign="top">Existing named semaphore to be accessed</td>
<td align="left" valign="top">*RW</td>
<td align="left" valign="top">EACCES</td>
</tr>
</table>
<br>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top"><em>value</em></td>
<td align="left" valign="top"><strong>sem_open()</strong> was successful. The
value returned is a pointer to the open named semaphore.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>SEM_FAILED</em></td>
<td align="left" valign="top"><strong>sem_open()</strong> was not successful.
The <em>errno</em> variable is set to indicate the error.</td>
</tr>
</table>
<br>
<br>
<h3><a name="ecsemo">Error Conditions</a></h3>
<p>If <strong>sem_open()</strong> is not successful, <em>errno</em> usually
indicates one of the following errors. Under some conditions, <em>errno</em>
could indicate an error other than those listed here.</p>
<dl>
<dt><em>[EACCES]</em></dt>
<dd>
<p>Permission denied.</p>
<p>An attempt was made to access an object in a way forbidden by its object
access permissions.</p>
</dd>
<dt><em>[EEXIST]</em></dt>
<dd>
<p>Semaphore exists.</p>
<p>A named semaphore exists for the parameter <em>name</em>, but O_CREAT and
O_EXCL are both set in <em>oflag</em>.</p>
</dd>
<dt><em>[EINVAL]</em></dt>
<dd>
<p>The value specified for the argument is not correct.</p>
<p>A function was passed incorrect argument values, or an operation was
attempted on an object and the operation specified is not supported for that
type of object.</p>
<p>An argument value is not valid, out of range, or NULL.</p>
<p>The <em>value</em> parameter is greater than SEM_VALUE_MAX.</p>
</dd>
<dt><em>[ENAMETOOLONG]</em></dt>
<dd>
<p>The name is too long. The name is longer than the SEM_NAME_MAX
characters.</p>
</dd>
<dt><em>[ENOENT]</em></dt>
<dd>
<p>No such path or directory.</p>
<p>The name specified on the <strong>sem_open()</strong> call does not refer to
an existing named semaphore and O_CREAT was not set in <em>oflag</em>.</p>
</dd>
<dt><em>[ENOSPC]</em></dt>
<dd>
<p>No space available.</p>
<p>The requested operations required additional space on the device and there
is no space left. This also could be caused by exceeding the user profile
storage limit when creating or transferring ownership of an object.</p>
<p>Insufficient space remains to hold the intended file, directory, or
link.</p>
<p>System semaphore resources have been exhausted.</p>
</dd>
</dl>
<br>
<h3>Error Messages</h3>
<p>None.</p>
<br>
<h3><a name="risemo">Related Information</a></h3>
<ul>
<li>The &lt;<strong>semaphore.h</strong>&gt; file (see <a href="unix13.htm">
Header Files for UNIX-Type Functions</a>)<br>
<br>
</li>
<li><a href="ipcsemou.htm">QlgSem_open()</a> Open Named Semaphore (using
NLS-enabled path name)<br>
<br>
</li>
<li><a href="ipcsemcl.htm">sem_close()</a>--Close Named Semaphore<br>
<br>
</li>
<li><a href="ipcsemge.htm">sem_getvalue()</a>--Get Semaphore Value<br>
<br>
</li>
<li><a href="ipcsemon.htm">sem_open_np()</a>--Open Named Semaphore with Maximum
Value<br>
<br>
</li>
<li><a href="ipcsemp.htm">sem_post()</a>--Post to Semaphore<br>
<br>
</li>
<li><a href="ipcsempn.htm">sem_post_np()</a>--Post Value to Semaphore<br>
<br>
</li>
<li><a href="ipcsemtr.htm">sem_trywait()</a>--Try to Decrement Semaphore<br>
<br>
</li>
<li><a href="ipcsemun.htm">sem_unlink()</a>--Unlink Named Semaphore<br>
<br>
</li>
<li><a href="ipcsemw.htm">sem_wait()</a>--Wait for Semaphore<br>
<br>
</li>
<li><a href="ipcsemwn.htm">sem_wait_np()</a>--Wait for Semaphore with
Timeout</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<p>The following example opens the named semaphore "/mysemaphore" and creates
the semaphore with an initial value of 10 if it does not already exist. If the
semaphore is created, the permissions are set such that only the current user
has access to the semaphore.</p>
<pre>
#include &lt;semaphore.h&gt;
main() {
sem_t * my_semaphore;
int rc;
my_semaphore = sem_open("/mysemaphore",
O_CREAT, S_IRUSR | S_IWUSR, 10);
}
</pre>
<br>
<hr>
API introduced: V4R4
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>