📄 ftok.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-2003 The Open Group, All Rights Reserved --><title>ftok</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="ftok"></a> <a name="tag_03_198"></a><!-- ftok --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_198_01"></a>NAME</h4><blockquote>ftok - generate an IPC key</blockquote><h4><a name="tag_03_198_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 <<a href="../basedefs/sys/ipc.h.html">sys/ipc.h</a>><br><br> key_t ftok(const char *</tt><i>path</i><tt>, int</tt> <i>id</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_198_03"></a>DESCRIPTION</h4><blockquote><p>The <i>ftok</i>() function shall return a key based on <i>path</i> and <i>id</i> that is usable in subsequent calls to <a href="../functions/msgget.html"><i>msgget</i>()</a>, <a href="../functions/semget.html"><i>semget</i>()</a>, and <a href="../functions/shmget.html"><i>shmget</i>()</a>. The application shall ensure that the <i>path</i> argument is the pathname of anexisting file that the process is able to <a href="../functions/stat.html"><i>stat</i>()</a>.</p><p>The <i>ftok</i>() function shall return the same key value for all paths that name the same file, when called with the same<i>id</i> value, and return different key values when called with different <i>id</i> values or with paths that name differentfiles existing on the same file system at the same time. It is unspecified whether <i>ftok</i>() shall return the same key valuewhen called again after the file named by <i>path</i> is removed and recreated with the same name.</p><p>Only the low-order 8-bits of <i>id</i> are significant. The behavior of <i>ftok</i>() is unspecified if these bits are 0.</p></blockquote><h4><a name="tag_03_198_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>ftok</i>() shall return a key. Otherwise, <i>ftok</i>() shall return (<b>key_t</b>)-1 and set<i>errno</i> to indicate the error.</p></blockquote><h4><a name="tag_03_198_05"></a>ERRORS</h4><blockquote><p>The <i>ftok</i>() function shall fail if:</p><dl compact><dt>[EACCES]</dt><dd>Search permission is denied for a component of the path prefix.</dd><dt>[ELOOP]</dt><dd>A loop exists in symbolic links encountered during resolution of the <i>path</i> argument.</dd><dt>[ENAMETOOLONG]</dt><dd>The length of the <i>path</i> argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.</dd><dt>[ENOENT]</dt><dd>A component of <i>path</i> does not name an existing file or <i>path</i> is an empty string.</dd><dt>[ENOTDIR]</dt><dd>A component of the path prefix is not a directory.</dd></dl><p>The <i>ftok</i>() function may fail if:</p><dl compact><dt>[ELOOP]</dt><dd>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>path</i> argument.</dd><dt>[ENAMETOOLONG]</dt><dd>Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_198_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_198_06_01"></a>Getting an IPC Key</h5><p>The following example gets a unique key that can be used by the IPC functions <a href="../functions/semget.html"><i>semget</i>()</a>, <a href="../functions/msgget.html"><i>msgget</i>()</a>, and <a href="../functions/shmget.html"><i>shmget</i>()</a>. The key returned by <i>ftok</i>() for this example is based on the ID value<i>S</i> and the pathname <b>/tmp</b>.</p><pre><tt>#include <sys/ipc.h>...key_t key;char *path = "/tmp";int id = 'S';<br>key = ftok(path, id);</tt></pre><h5><a name="tag_03_198_06_02"></a>Saving an IPC Key</h5><p>The following example gets a unique key based on the pathname <b>/tmp</b> and the ID value <i>a</i>. It also assigns the valueof the resulting key to the <i>semkey</i> variable so that it will be available to a later call to <a href="../functions/semget.html"><i>semget</i>()</a>, <a href="../functions/msgget.html"><i>msgget</i>()</a>, or <a href="../functions/shmget.html"><i>shmget</i>()</a>.</p><pre><tt>#include <sys/ipc.h>...key_t semkey;<br>if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) { perror("IPC error: ftok"); exit(1);}</tt></pre></blockquote><h4><a name="tag_03_198_07"></a>APPLICATION USAGE</h4><blockquote><p>For maximum portability, <i>id</i> should be a single-byte character.</p></blockquote><h4><a name="tag_03_198_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_198_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_198_10"></a>SEE ALSO</h4><blockquote><p><a href="msgget.html"><i>msgget</i>()</a> , <a href="semget.html"><i>semget</i>()</a> , <a href="shmget.html"><i>shmget</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/sys/ipc.h.html"><i><sys/ipc.h></i></a></p></blockquote><h4><a name="tag_03_198_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4, Version 2.</p></blockquote><h4><a name="tag_03_198_12"></a>Issue 5</h4><blockquote><p>Moved from X/OPEN UNIX extension to BASE.</p></blockquote><h4><a name="tag_03_198_13"></a>Issue 6</h4><blockquote><p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p><p>The wording of the mandatory [ELOOP] error condition is updated, and a second optional [ELOOP] error condition is added.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -