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

266 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>semget</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="semget"></a> <a name="tag_03_639"></a><!-- semget -->
<!--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_639_01"></a>NAME</h4>
<blockquote>semget - get set of XSI semaphores</blockquote>
<h4><a name="tag_03_639_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/sys/sem.h.html">sys/sem.h</a>&gt;<br>
<br>
int semget(key_t</tt> <i>key</i><tt>, int</tt> <i>nsems</i><tt>, int</tt> <i>semflg</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_639_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>semget</i>() function operates on XSI semaphores (see the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a
href="../basedefs/xbd_chap04.html#tag_04_15">Section 4.15, Semaphore</a>). It is unspecified whether this function interoperates
with the realtime interprocess communication facilities defined in <a href="xsh_chap02_08.html#tag_02_08"><i>Realtime</i></a> .</p>
<p>The <i>semget</i>() function shall return the semaphore identifier associated with <i>key</i>.</p>
<p>A semaphore identifier with its associated <b>semid_ds</b> data structure and its associated set of <i>nsems</i> semaphores (see
<a href="../basedefs/sys/sem.h.html"><i>&lt;sys/sem.h&gt;</i></a>) is created for <i>key</i> if one of the following is true:</p>
<ul>
<li>
<p>The argument <i>key</i> is equal to IPC_PRIVATE.</p>
</li>
<li>
<p>The argument <i>key</i> does not already have a semaphore identifier associated with it and (<i>semflg</i> &amp;IPC_CREAT) is
non-zero.</p>
</li>
</ul>
<p>Upon creation, the <b>semid_ds</b> data structure associated with the new semaphore identifier is initialized as follows:</p>
<ul>
<li>
<p>In the operation permissions structure <i>sem_perm.cuid</i>, <i>sem_perm.uid</i>, <i>sem_perm.cgid</i>, and <i>sem_perm.gid</i>
shall be set equal to the effective user ID and effective group ID, respectively, of the calling process.</p>
</li>
<li>
<p>The low-order 9 bits of <i>sem_perm.mode</i> shall be set equal to the low-order 9 bits of <i>semflg</i>.</p>
</li>
<li>
<p>The variable <i>sem_nsems</i> shall be set equal to the value of <i>nsems</i>.</p>
</li>
<li>
<p>The variable <i>sem_otime</i> shall be set equal to 0 and <i>sem_ctime</i> shall be set equal to the current time.</p>
</li>
<li>
<p>The data structure associated with each semaphore in the set shall not be initialized. The <a href=
"../functions/semctl.html"><i>semctl</i>()</a> function with the command SETVAL or SETALL can be used to initialize each
semaphore.</p>
</li>
</ul>
</blockquote>
<h4><a name="tag_03_639_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>semget</i>() shall return a non-negative integer, namely a semaphore identifier; otherwise, it
shall return -1 and set <i>errno</i> to indicate the error.</p>
</blockquote>
<h4><a name="tag_03_639_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>semget</i>() function shall fail if:</p>
<dl compact>
<dt>[EACCES]</dt>
<dd>A semaphore identifier exists for <i>key</i>, but operation permission as specified by the low-order 9 bits of <i>semflg</i>
would not be granted; see <a href="xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess Communication</i></a> .</dd>
<dt>[EEXIST]</dt>
<dd>A semaphore identifier exists for the argument <i>key</i> but ((<i>semflg</i> &amp;IPC_CREAT) &amp;&amp;(<i>semflg</i>
&amp;IPC_EXCL)) is non-zero.</dd>
<dt>[EINVAL]</dt>
<dd>The value of <i>nsems</i> is either less than or equal to 0 or greater than the system-imposed limit, or a semaphore identifier
exists for the argument <i>key</i>, but the number of semaphores in the set associated with it is less than <i>nsems</i> and
<i>nsems</i> is not equal to 0.</dd>
<dt>[ENOENT]</dt>
<dd>A semaphore identifier does not exist for the argument <i>key</i> and (<i>semflg</i> &amp;IPC_CREAT) is equal to 0.</dd>
<dt>[ENOSPC]</dt>
<dd>A semaphore identifier is to be created but the system-imposed limit on the maximum number of allowed semaphores system-wide
would be exceeded.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_639_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_639_06_01"></a>Creating a Semaphore Identifier</h5>
<p>The following example gets a unique semaphore key using the <a href="../functions/ftok.html"><i>ftok</i>()</a> function, then
gets a semaphore ID associated with that key using the <i>semget</i>() function (the first call also tests to make sure the
semaphore exists). If the semaphore does not exist, the program creates it, as shown by the second call to <i>semget</i>(). In
creating the semaphore for the queuing process, the program attempts to create one semaphore with read/write permission for all. It
also uses the IPC_EXCL flag, which forces <i>semget</i>() to fail if the semaphore already exists.</p>
<p>After creating the semaphore, the program uses a call to <a href="../functions/semop.html"><i>semop</i>()</a> to initialize it
to the values in the <i>sbuf</i> array. The number of processes that can execute concurrently without queuing is initially set to
2. The final call to <i>semget</i>() creates a semaphore identifier that can be used later in the program.</p>
<pre>
<tt>#include &lt;sys/types.h&gt;
#include &lt;stdio.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;errno.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pwd.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;limits.h&gt;
...
key_t semkey;
int semid, pfd, fv;
struct sembuf sbuf;
char *lgn;
char filename[PATH_MAX+1];
struct stat outstat;
struct passwd *pw;
...
/* Get unique key for semaphore. */
if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
<br>
/* Get semaphore ID associated with this key. */
if ((semid = semget(semkey, 0, 0)) == -1) {
<br>
/* Semaphore does not exist - Create. */
if ((semid = semget(semkey, 1, IPC_CREAT | IPC_EXCL | S_IRUSR |
S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) != -1)
{
/* Initialize the semaphore. */
sbuf.sem_num = 0;
sbuf.sem_op = 2; /* This is the number of runs
without queuing. */
sbuf.sem_flg = 0;
if (semop(semid, &amp;sbuf, 1) == -1) {
perror("IPC error: semop"); exit(1);
}
}
else if (errno == EEXIST) {
if ((semid = semget(semkey, 0, 0)) == -1) {
perror("IPC error 1: semget"); exit(1);
}
}
else {
perror("IPC error 2: semget"); exit(1);
}
}
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_639_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>The POSIX Realtime Extension defines alternative interfaces for interprocess communication. Application developers who need to
use IPC should design their applications so that modules using the IPC routines described in <a href=
"xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess Communication</i></a> can be easily modified to use the alternative
interfaces.</p>
</blockquote>
<h4><a name="tag_03_639_08"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_639_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_639_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="xsh_chap02_07.html#tag_02_07"><i>XSI Interprocess Communication</i></a> , <a href=
"xsh_chap02_08.html#tag_02_08"><i>Realtime</i></a> , <a href="semctl.html"><i>semctl</i>()</a> , <a href=
"semop.html"><i>semop</i>()</a> , <a href="sem_close.html"><i>sem_close</i>()</a> , <a href=
"sem_destroy.html"><i>sem_destroy</i>()</a> , <a href="sem_getvalue.html"><i>sem_getvalue</i>()</a> , <a href=
"sem_init.html"><i>sem_init</i>()</a> , <a href="sem_open.html"><i>sem_open</i>()</a> , <a href=
"sem_post.html"><i>sem_post</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/sys/sem.h.html"><i>&lt;sys/sem.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_639_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 2. Derived from Issue 2 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_639_12"></a>Issue 5</h4>
<blockquote>
<p>The note about use of POSIX Realtime Extension IPC routines has been moved from FUTURE DIRECTIONS to a new APPLICATION USAGE
section.</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>