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

501 lines
13 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!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_np()--Open Named Semaphore with Maximum Value</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_np()--Open Named Semaphore with Maximum Value</h2>
<div class="box" style="width: 80%;">
<br>
&nbsp;&nbsp;Syntax
<pre>
#include &lt;semaphore.h&gt;
sem_t * sem_open_np(const char *<em>name</em>, int <em>oflag</em>,
mode_t <em>mode</em>, unsigned int <em>value</em>,
sem_attr_np_t * <em>attr</em>);
</pre>
&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_np()</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>. If a named semaphore is being created, the parameters
<em>mode</em>, <em>value</em>, and <em>attr</em> are used to set the
permissions, value, and maximum value of the created semaphore.</p>
<p>If <strong>sem_open_np()</strong> is called multiple times within the same
process using the same name, <strong>sem_open_np()</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_np()</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 used by named semaphores only. 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="ipcsemonu.htm">QlgSem_open_np()</a>--Open Named Semaphore with
Maximum Value (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_np()</strong> to fail if
O_CREAT is also set and the named semaphore already exists.<br>
<br>
</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 list of constants. For another
process to open the semaphore, the process's effective UID 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) The initial value of the named semaphore.<br>
<br>
</dd>
<dt><strong>attr</strong></dt>
<dd>(Input) Attributes for the semaphore.
<p>The members of the sem_attr_np_t structure are as follows:</p>
<table cellpadding="5">
<!-- cols="15 85" -->
<tr>
<td align="left" valign="top" nowrap><em>unsigned int reserved1[1]</em></td>
<td align="left" valign="top">A reserved field that must be set to zero.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>unsigned int maxvalue</em></td>
<td align="left" valign="top">The maximum value that the semaphore may obtain.
<em>maxvalue</em> must be greater than zero. If a <strong>sem_post()</strong>
or <strong>sem_post_np()</strong> operation would cause the value of a
semaphore to exceed its maximum value, the operation will fail, returning
EINVAL.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>unsigned int reserved2[1]</em></td>
<td align="left" valign="top">A reserved field that must be set to zero.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>char title[16]</em></td>
<td align="left" valign="top">The title of the semaphore. The title is a
null-terminated string that has a maximum length of 16 bytes. The string is
associated with the semaphore. If the first byte is zero, then the system
assigns a title to the semaphore that is based on the semaphore name. The title
is retrieved using the Open List of Interprocess Communication Objects
(QP0ZOLIP) API.<br>
<br>
</td>
</tr>
<tr>
<td align="left" valign="top"><em>void * reserved3[2]</em></td>
<td align="left" valign="top">A reserved field that must be set to zero.</td>
</tr>
</table>
</dd>
</dl>
<br>
<h3>Authorities</h3>
<p><strong><a name="TBLASEMON1">Authorization required for
sem_open_np()</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_np()</strong> was successful.
The value returned is a pointer to the opened 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_np()</strong> was not
successful. The <em>errno</em> variable is set to indicate the error.</td>
</tr>
</table>
<br>
<h3><a name="ecsemon">Error Conditions</a></h3>
<p>If <strong>sem_open_np()</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>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>[EFAULT]</em></dt>
<dd>
<p>The address used for an argument is not correct.</p>
<p>In attempting to use an argument in a call, the system detected an address
that is not valid.</p>
<p>While attempting to access a parameter passed to this function, the system
detected an address that is not valid.</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 <samp>maxvalue</samp> field of the <em>attr</em> argument is greater
than SEM_VALUE_MAX.</p>
<p>The <samp>maxvalue</samp> field of the <em>attr</em> argument is equal to
zero.</p>
<p>The <em>value</em> argument is greater than the <samp>maxvalue</samp> field
of the <em>attr</em> argument.</p>
<p>The reserved fields of the <em>attr</em> argument are not set to zero.</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 directory or a component of the path name specified does not exist.</p>
<p>A named file or directory does not exist or is an empty string.</p>
<p>The name specified on the <strong>sem_open_np()</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="risemon">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="ipcsemonu.htm">QlgSem_open_np()</a>--Open Named Semaphore with
Maximum Value (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="ipcsemo.htm">sem_open()</a>--Open Named Semaphore<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&gt;</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 and a maxiumum value of 11. 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;
sem_attr_np_t attr;
memset(&amp;attr, 0, sizeof(attr));
attr.maxvalue=11;
my_semaphore = sem_open_np("/mysemaphore",
O_CREAT|O_EXCL,
S_IRUSR | S_IWUSR,
10,
&amp;attr);
}
</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>