390 lines
11 KiB
HTML
390 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>semget()-Get Semaphore Set with Key</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 -->
|
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
<!-- Edited by Kersten Feb 02 -->
|
|
<!-- This file has undergone error condition cleanup on 05/01/02 by JET -->
|
|
</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>semget()--Get Semaphore Set with Key</h2>
|
|
|
|
<div class="box" style="width: 70%;">
|
|
<br>
|
|
Syntax<br>
|
|
<pre>
|
|
#include <sys/sem.h>
|
|
#include <sys/stat.h>
|
|
|
|
int semget(key_t <em>key</em>, int <em>nsems</em>, int <em>semflg</em>);
|
|
</pre>
|
|
|
|
<br>
|
|
Service Program Name: QP0ZCPA<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Default Public Authority: *USE<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Threadsafe: Yes<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
</div>
|
|
|
|
<p>The <strong>semget()</strong> function either creates a new semaphore set
|
|
or returns the semaphore set identifier associated with the <em>key</em>
|
|
parameter for an existing semaphore set. A new semaphore set is
|
|
created if one of the following is true:</p>
|
|
|
|
<ul>
|
|
<li>The <em>key</em> parameter is equal to IPC_PRIVATE.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li>The <em>key</em> parameter does not already have a semaphore set identifier
|
|
associated with it and the IPC_CREAT flag is specified in the <em>semflg</em> parameter.<br>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The system maintains status information about a semaphore set which can be
|
|
retrieved with the <a href="ipcsemct.htm">semctl()</a> function. When a new
|
|
semaphore set is created, the system initializes the members of the
|
|
<samp>semid_ds</samp> structure as follows: </p>
|
|
|
|
<ul>
|
|
<li><samp>sem_perm.cuid</samp> and <samp>sem_perm.uid</samp> are set
|
|
to the current user ID of the thread.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><samp>sem_perm.cgid</samp> and <samp>sem_perm.gid</samp> are set
|
|
to the current group ID of the thread.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li>The low-order 9 bits of <samp>shm_perm.mode</samp> are set equal to the
|
|
low-order 9 bits of the <em>shmflg</em> parameter.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><samp>sem_nsems</samp> is set to the value specified in the <em>
|
|
nsems</em> parameter.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><samp>sem_ctime</samp> is set to the current time.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><samp>sem_otime</samp> is set to zero.</li>
|
|
</ul>
|
|
|
|
<p><strong>Parameters</strong></p>
|
|
|
|
<dl>
|
|
<dt><strong>key</strong></dt>
|
|
|
|
<dd>(Input) Key associated with the semaphore set. A key of
|
|
IPC_PRIVATE (0x00000000) guarantees that a unique semaphore set is created. A key can also
|
|
be specified by the caller or generated by the <a href="p0zftok.htm">ftok()</a>
|
|
function.<br>
|
|
<br>
|
|
</dd>
|
|
|
|
<dt><strong>nsems</strong></dt>
|
|
|
|
<dd>(Input) Number of semaphores in the semaphore set. The number of semaphores
|
|
in the set cannot be changed after the semaphore set is created. If an existing
|
|
semaphore set is being accessed, <em>nsems</em> can be zero.<br>
|
|
<br>
|
|
</dd>
|
|
|
|
<dt><strong>semflg</strong></dt>
|
|
|
|
<dd>(Input) Operations and permission flags.
|
|
The <em>semflg</em> parameter value is either zero, or is obtained by
|
|
performing an OR operation on one or more of the following constants:</dd>
|
|
|
|
</dl>
|
|
<dl>
|
|
|
|
<dt><strong>S_IRUSR (0x00000100)</strong></dt>
|
|
<dd>Allow the owner of the semaphore set to read from it.</dd>
|
|
|
|
<dt><strong>S_IWUSR (0x00000080)</strong></dt>
|
|
<dd>Allow the owner of the semaphore set to write to it.</dd>
|
|
|
|
<dt><strong>S_IRGRP (0x00000020)</strong></dt>
|
|
<dd>Allow the group of the semaphore set to read from it.</dd>
|
|
|
|
<dt><strong>S_IWGRP (0x00000010)</strong></dt>
|
|
<dd>Allow the group of the semaphore set to write to it.</dd>
|
|
|
|
<dt><strong>S_IROTH (0x00000004)</strong></dt>
|
|
<dd>Allow others to read from the semaphore set.</dd>
|
|
|
|
<dt><strong>S_IWOTH (0x00000002)</strong></dt>
|
|
<dd>Allow others to write to the semaphore set.</dd>
|
|
|
|
<dt><strong>IPC_CREAT (0x00000200)</strong></dt>
|
|
<dd>Create the semaphore set if it does not exist.</dd>
|
|
|
|
<dt><strong>IPC_EXCL (0x00000400)</strong></dt>
|
|
<dd>Return an error if the IPC_CREAT flag is set and the semaphore set already exists.</dd>
|
|
</dl>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
<h3>Authorities</h3>
|
|
|
|
<p><strong><a name="TBLASGET1">Authorization Required for
|
|
semget()</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">Semaphore set 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 semaphore set to be accessed</td>
|
|
<td align="left" valign="top">See Note</td>
|
|
<td align="left" valign="top">EACCES</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<p><strong>Note:</strong> If the thread is accessing a semaphore set that
|
|
already exists, the mode specified in the last 9 bits of <em>semflg</em> must
|
|
be a subset of the mode of the existing semaphore set.</p>
|
|
|
|
<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>semget()</strong> was successful. The
|
|
value returned is the semaphore set identifier associated with the <em>key</em>
|
|
parameter.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top"><em>-1</em></td>
|
|
<td align="left" valign="top"><strong>semget()</strong> was not successful. The
|
|
<em>errno</em> variable is set to indicate the error.</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br>
|
|
<br>
|
|
|
|
|
|
<h3>Error Conditions</h3>
|
|
|
|
<p>If <strong>semget()</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>
|
|
|
|
<p>The thread does not have access to the specified file, directory, component,
|
|
or path.</p>
|
|
|
|
<p>A semaphore set identifier exists for the parameter <em>key</em>, but
|
|
permissions specified in the low-order 9 bits of <em>semflg</em>
|
|
are not a subset of the current permissions.</p>
|
|
</dd>
|
|
|
|
<dt><em>[EDAMAGE]</em></dt>
|
|
|
|
<dd>
|
|
<p>A damaged object was encountered.</p>
|
|
|
|
<p>A referenced object is damaged. The object cannot be used.</p>
|
|
|
|
<p>The semaphore set has been damaged by a previous semaphore operation.</p>
|
|
</dd>
|
|
|
|
<dt><em>[EEXIST]</em></dt>
|
|
|
|
<dd>
|
|
<p>File exists.</p>
|
|
|
|
<p>The file specified already exists and the specified operation requires that
|
|
it not exist.</p>
|
|
|
|
<p>The named file, directory, or path already exists.</p>
|
|
|
|
<p>A semaphore identifier exists for the <em>key</em> parameter, and both the
|
|
IPC_CREAT and IPC_EXCL flags are set in the <em>semflg</em> parameter.
|
|
</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>One of the following has occurred:</p>
|
|
|
|
<ul>
|
|
<li>The value of <em>nsems</em> is either less than or equal to zero, or
|
|
greater than the system-imposed limit.</li>
|
|
|
|
<li>A semaphore identifier exists for the parameter <em>key</em>, but the
|
|
number of semaphores in the set associated with it is less than <em>
|
|
nsems</em> and <em>nsems</em> is not zero.</li>
|
|
</ul>
|
|
</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>A semaphore set identifier does not exist for the <em>key</em> parameter, and
|
|
the IPC_CREAT flag is not set in the <em>semflg</em> parameter.</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 could also 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>A semaphore set identifier cannot be created because the system limit on the
|
|
maximum number of allowed semaphore set identifiers would be exceeded.</p>
|
|
</dd>
|
|
|
|
<dt><em>[EUNKNOWN]</em></dt>
|
|
|
|
<dd>
|
|
<p>Unknown system state.</p>
|
|
|
|
<p>The operation failed because of an unknown system state. See any messages in
|
|
the job log and correct any errors that are indicated, then retry the
|
|
operation.</p>
|
|
</dd>
|
|
</dl>
|
|
|
|
<br>
|
|
<h3>Error Messages</h3>
|
|
|
|
<p>None.</p>
|
|
|
|
<br>
|
|
<h3><a name="USAGE_NOTES">Usage Notes</a></h3>
|
|
|
|
<ol>
|
|
|
|
<li>The best way to generate a unique key is to use the <a href="p0zftok.htm">ftok()</a>
|
|
function.<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li>A <a href="ipcsemct.htm">semctl()</a> call specifying a <em>cmd</em> parameter of
|
|
SETALL should be used to initialize the semaphore values after the semaphore
|
|
set is created.</li>
|
|
</ol>
|
|
|
|
<br>
|
|
<h3>Related Information</h3>
|
|
|
|
<ul>
|
|
<li>The <<strong>sys/sem.h</strong>> file (see <a href="unix13.htm">
|
|
Header Files for UNIX-Type Functions</a>)<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><a href="p0zftok.htm">ftok()</a>--Generate IPC Key from File Name<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><a href="ipcsemct.htm">semctl()</a>--Perform Semaphore Control
|
|
Operations<br>
|
|
<br>
|
|
</li>
|
|
|
|
<li><a href="ipcsemop.htm">semop()</a>--Perform Semaphore Operations on
|
|
Semaphore Set</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>For an example of using this function, see <a href="../apiref/apiexusmem.htm">
|
|
Using Semaphores and Shared Memory</a> in Examples: APIs.</p>
|
|
|
|
<br>
|
|
<hr>
|
|
API introduced: V3R6
|
|
|
|
<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>
|
|
|