1LOCKFILE(3)                Linux Programmer's Manual               LOCKFILE(3)
2
3
4

NAME

6       flockfile, ftrylockfile, funlockfile - lock FILE for stdio
7

SYNOPSIS

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

DESCRIPTION

16       The  stdio  functions are thread-safe. This is achieved by assigning to
17       each FILE object a lockcount and (if the lockcount is non-zero) an own‐
18       ing thread.  For each library call, these functions wait until the FILE
19       object is no longer locked by a different thread, then lock it, do  the
20       requested I/O, and unlock the object again.
21
22       (Note:  this  locking  has  nothing to do with the file locking done by
23       functions like flock(2) and lockf(3).)
24
25       All this is invisible to the C-programmer, but there may be two reasons
26       to  wish  for more detailed control. On the one hand, maybe a series of
27       I/O actions by one thread belongs together, and should  not  be  inter‐
28       rupted  by  the I/O of some other thread.  On the other hand, maybe the
29       locking overhead should be avoided for greater efficiency.
30
31       To this end, a thread can explicitly lock the FILE object, then do  its
32       series  of  I/O  actions, then unlock. This prevents other threads from
33       coming in between. If the reason for doing this was to achieve  greater
34       efficiency, one does the I/O with the non-locking versions of the stdio
35       functions: with getc_unlocked() and putc_unlocked() instead  of  getc()
36       and putc().
37
38       The  flockfile()  function waits for *filehandle to be no longer locked
39       by a different thread, then makes the current thread owner of *filehan‐
40       dle, and increments the lockcount.
41
42       The funlockfile() function decrements the lock count.
43
44       The  ftrylockfile()  function is a non-blocking version of flockfile().
45       It does nothing in case some other  thread  owns  *filehandle,  and  it
46       obtains ownership and increments the lockcount otherwise.
47

RETURN VALUE

49       The  ftrylockfile()  function  returns  zero  for success (the lock was
50       obtained), and non-zero for failure.
51

ERRORS

53       None.
54

AVAILABILITY

56       These functions  are  available  when  _POSIX_THREAD_SAFE_FUNCTIONS  is
57       defined.  They  are  in  libc since libc 5.1.1 and in glibc since glibc
58       2.0.
59

CONFORMING TO

61       POSIX.1-2001.
62

SEE ALSO

64       unlocked_stdio(3)
65
66
67
68                                  2001-10-18                       LOCKFILE(3)
Impressum