1FLOCKFILE(P)               POSIX Programmer's Manual              FLOCKFILE(P)
2
3
4

NAME

6       flockfile, ftrylockfile, funlockfile - stdio locking functions
7

SYNOPSIS

9       #include <stdio.h>
10
11       void flockfile(FILE *file);
12       int ftrylockfile(FILE *file);
13       void funlockfile(FILE *file);
14
15

DESCRIPTION

17       These functions shall provide for explicit application-level locking of
18       stdio ( FILE *) objects. These functions can be used  by  a  thread  to
19       delineate a sequence of I/O statements that are executed as a unit.
20
21       The  flockfile()  function  shall acquire for a thread ownership of a (
22       FILE *) object.
23
24       The ftrylockfile() function shall acquire for a thread ownership of a (
25       FILE  *)  object  if  the object is available; ftrylockfile() is a non-
26       blocking version of flockfile().
27
28       The funlockfile() function shall relinquish the  ownership  granted  to
29       the  thread.  The behavior is undefined if a thread other than the cur‐
30       rent owner calls the funlockfile() function.
31
32       The functions shall behave as if there is a lock count associated  with
33       each  (  FILE  *)  object. This count is implicitly initialized to zero
34       when the ( FILE *) object is created. The ( FILE *) object is  unlocked
35       when  the  count  is zero.  When the count is positive, a single thread
36       owns the ( FILE *) object. When the flockfile() function is called,  if
37       the count is zero or if the count is positive and the caller owns the (
38       FILE *) object, the count shall be incremented.  Otherwise, the calling
39       thread  shall  be  suspended,  waiting for the count to return to zero.
40       Each call to funlockfile()  shall  decrement  the  count.  This  allows
41       matching  calls  to flockfile() (or successful calls to ftrylockfile())
42       and funlockfile() to be nested.
43
44       All functions that reference ( FILE *) objects shall behave as if  they
45       use  flockfile()  and  funlockfile()  internally to obtain ownership of
46       these ( FILE *) objects.
47

RETURN VALUE

49       None for flockfile() and funlockfile().
50
51       The ftrylockfile() function shall return zero for success and  non-zero
52       to indicate that the lock cannot be acquired.
53

ERRORS

55       No errors are defined.
56
57       The following sections are informative.
58

EXAMPLES

60       None.
61

APPLICATION USAGE

63       Applications  using  these  functions may be subject to priority inver‐
64       sion,   as   discussed   in   the   Base    Definitions    volume    of
65       IEEE Std 1003.1-2001, Section 3.285, Priority Inversion.
66

RATIONALE

68       The  flockfile()  and  funlockfile()  functions  provide  an orthogonal
69       mutual-exclusion lock for each FILE. The ftrylockfile()  function  pro‐
70       vides  a  non-blocking  attempt  to  acquire  a file lock, analogous to
71       pthread_mutex_trylock().
72
73       These locks behave as if they are the same as those used internally  by
74       stdio  for  thread-safety.  This  both  provides thread-safety of these
75       functions without requiring a second  level  of  internal  locking  and
76       allows  functions  in  stdio  to be implemented in terms of other stdio
77       functions.
78
79       Application writers and implementors should be  aware  that  there  are
80       potential  deadlock  problems  on  FILE objects. For example, the line-
81       buffered flushing semantics of stdio (requested via  {_IOLBF})  require
82       that  certain input operations sometimes cause the buffered contents of
83       implementation-defined line-buffered output streams to be  flushed.  If
84       two  threads  each  hold the lock on the other's FILE, deadlock ensues.
85       This type of deadlock can be avoided by acquiring FILE locks in a  con‐
86       sistent  order. In particular, the line-buffered output stream deadlock
87       can typically be avoided by acquiring locks  on  input  streams  before
88       locks on output streams if a thread would be acquiring both.
89
90       In  summary,  threads  sharing stdio streams with other threads can use
91       flockfile() and funlockfile() to cause sequences of I/O performed by  a
92       single  thread  to  be  kept  bundled.   The only case where the use of
93       flockfile() and funlockfile() is required is to provide  a  scope  pro‐
94       tecting  uses  of  the  *_unlocked()  functions/macros.  This moves the
95       cost/performance tradeoff to the optimal point.
96

FUTURE DIRECTIONS

98       None.
99

SEE ALSO

101       getc_unlocked() , putc_unlocked() ,  the  Base  Definitions  volume  of
102       IEEE Std 1003.1-2001, <stdio.h>
103
105       Portions  of  this text are reprinted and reproduced in electronic form
106       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
107       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
108       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
109       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
110       event of any discrepancy between this version and the original IEEE and
111       The  Open Group Standard, the original IEEE and The Open Group Standard
112       is the referee document. The original Standard can be  obtained  online
113       at http://www.opengroup.org/unix/online.html .
114
115
116
117IEEE/The Open Group                  2003                         FLOCKFILE(P)
Impressum