📄 ftrylockfile.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-2004 IEEE and The Open Group, All Rights Reserved --><title>flockfile</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="flockfile"></a> <a name="tag_03_168"></a><!-- flockfile --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_168_01"></a>NAME</h4><blockquote>flockfile, ftrylockfile, funlockfile - stdio locking functions</blockquote><h4><a name="tag_03_168_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('TSF')">TSF</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/stdio.h.html">stdio.h</a>><br><br> void flockfile(FILE *</tt><i>file</i><tt>);<br> int ftrylockfile(FILE *</tt><i>file</i><tt>);<br> void funlockfile(FILE *</tt><i>file</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_168_03"></a>DESCRIPTION</h4><blockquote><p>These functions shall provide for explicit application-level locking of stdio ( <b>FILE *</b>) objects. These functions can beused by a thread to delineate a sequence of I/O statements that are executed as a unit.</p><p>The <i>flockfile</i>() function shall acquire for a thread ownership of a ( <b>FILE *</b>) object.</p><p>The <i>ftrylockfile</i>() function shall acquire for a thread ownership of a ( <b>FILE *</b>) object if the object is available;<i>ftrylockfile</i>() is a non-blocking version of <i>flockfile</i>().</p><p>The <i>funlockfile</i>() function shall relinquish the ownership granted to the thread. The behavior is undefined if a threadother than the current owner calls the <i>funlockfile</i>() function.</p><p>The functions shall behave as if there is a lock count associated with each ( <b>FILE *</b>) object. This count is implicitlyinitialized to zero when the ( <b>FILE *</b>) object is created. The ( <b>FILE *</b>) object is unlocked when the count is zero.When the count is positive, a single thread owns the ( <b>FILE *</b>) object. When the <i>flockfile</i>() function is called, ifthe count is zero or if the count is positive and the caller owns the ( <b>FILE *</b>) object, the count shall be incremented.Otherwise, the calling thread shall be suspended, waiting for the count to return to zero. Each call to <i>funlockfile</i>() shalldecrement the count. This allows matching calls to <i>flockfile</i>() (or successful calls to <i>ftrylockfile</i>()) and<i>funlockfile</i>() to be nested.</p><p>All functions that reference ( <b>FILE *</b>) objects shall behave as if they use <i>flockfile</i>() and <i>funlockfile</i>()internally to obtain ownership of these ( <b>FILE *</b>) objects.</p></blockquote><h4><a name="tag_03_168_04"></a>RETURN VALUE</h4><blockquote><p>None for <i>flockfile</i>() and <i>funlockfile</i>().</p><p>The <i>ftrylockfile</i>() function shall return zero for success and non-zero to indicate that the lock cannot be acquired.</p></blockquote><h4><a name="tag_03_168_05"></a>ERRORS</h4><blockquote><p>No errors are defined.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_168_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_168_07"></a>APPLICATION USAGE</h4><blockquote><p>Applications using these functions may be subject to priority inversion, as discussed in the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap03.html#tag_03_285">Section 3.285, Priority Inversion</a>.</p></blockquote><h4><a name="tag_03_168_08"></a>RATIONALE</h4><blockquote><p>The <i>flockfile</i>() and <i>funlockfile</i>() functions provide an orthogonal mutual-exclusion lock for each <b>FILE</b>. The<i>ftrylockfile</i>() function provides a non-blocking attempt to acquire a file lock, analogous to <a href="../functions/pthread_mutex_trylock.html"><i>pthread_mutex_trylock</i>()</a>.</p><p>These locks behave as if they are the same as those used internally by <i>stdio</i> for thread-safety. This both providesthread-safety of these functions without requiring a second level of internal locking and allows functions in <i>stdio</i> to beimplemented in terms of other <i>stdio</i> functions.</p><p>Application writers and implementors should be aware that there are potential deadlock problems on <b>FILE</b> objects. Forexample, the line-buffered flushing semantics of <i>stdio</i> (requested via {_IOLBF}) require that certain input operationssometimes cause the buffered contents of implementation-defined line-buffered output streams to be flushed. If two threads eachhold the lock on the other's <b>FILE</b>, deadlock ensues. This type of deadlock can be avoided by acquiring <b>FILE</b> locks in aconsistent order. In particular, the line-buffered output stream deadlock can typically be avoided by acquiring locks on inputstreams before locks on output streams if a thread would be acquiring both.</p><p>In summary, threads sharing <i>stdio</i> streams with other threads can use <i>flockfile</i>() and <i>funlockfile</i>() to causesequences of I/O performed by a single thread to be kept bundled. The only case where the use of <i>flockfile</i>() and<i>funlockfile</i>() is required is to provide a scope protecting uses of the<i>*_unlocked</i>() functions/macros. This moves the cost/performance tradeoff to the optimalpoint.</p></blockquote><h4><a name="tag_03_168_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_168_10"></a>SEE ALSO</h4><blockquote><p><a href="getc_unlocked.html"><i>getc_unlocked</i>()</a>, <a href="putc_unlocked.html"><i>putc_unlocked</i>()</a>, the BaseDefinitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/stdio.h.html"><i><stdio.h></i></a></p></blockquote><h4><a name="tag_03_168_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 5. Included for alignment with the POSIX Threads Extension.</p></blockquote><h4><a name="tag_03_168_12"></a>Issue 6</h4><blockquote><p>These functions are marked as part of the Thread-Safe Functions option.</p></blockquote><div class="box"><em>End of informative text.</em></div><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 + -