man-pages/man3/flockfile.3

126 lines
3.6 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
2004-11-03 13:51:07 +00:00
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
2004-11-03 13:51:07 +00:00
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
2007-12-10 09:09:08 +00:00
.TH FLOCKFILE 3 2007-07-26 "" "Linux Programmer's Manual"
2004-11-03 13:51:07 +00:00
.SH NAME
flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
.SH SYNOPSIS
.nf
.B #include <stdio.h>
.sp
.BI "void flockfile(FILE *" filehandle );
.br
.BI "int ftrylockfile(FILE *" filehandle );
.br
.BI "void funlockfile(FILE *" filehandle );
.fi
.sp
.in -4n
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.in
.ad l
.sp
All functions shown above:
_POSIX_C_SOURCE || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
.ad b
2004-11-03 13:51:07 +00:00
.SH DESCRIPTION
The stdio functions are thread-safe.
This is achieved by assigning
2007-06-22 19:42:52 +00:00
to each
2007-06-22 20:40:07 +00:00
.I FILE
2007-12-25 22:02:19 +00:00
object a lockcount and (if the lockcount is nonzero)
2004-11-03 13:51:07 +00:00
an owning thread.
2007-06-22 19:42:52 +00:00
For each library call, these functions wait until the
2007-06-22 20:40:07 +00:00
.I FILE
2007-06-22 19:42:52 +00:00
object
2004-11-03 13:51:07 +00:00
is no longer locked by a different thread, then lock it, do the
requested I/O, and unlock the object again.
.LP
(Note: this locking has nothing to do with the file locking done
by functions like
.BR flock (2)
and
.BR lockf (3).)
.LP
All this is invisible to the C-programmer, but there may be two
reasons to wish for more detailed control.
On the one hand, maybe
2004-11-03 13:51:07 +00:00
a series of I/O actions by one thread belongs together, and should
not be interrupted by the I/O of some other thread.
On the other hand, maybe the locking overhead should be avoided
for greater efficiency.
.LP
2007-06-22 19:42:52 +00:00
To this end, a thread can explicitly lock the
2007-06-22 20:40:07 +00:00
.I FILE
2007-06-22 19:42:52 +00:00
object,
then do its series of I/O actions, then unlock.
This prevents
other threads from coming in between.
If the reason for doing
2004-11-03 13:51:07 +00:00
this was to achieve greater efficiency, one does the I/O with
the non-locking versions of the stdio functions: with
.BR getc_unlocked (3)
and
.BR putc_unlocked (3)
instead of
.BR getc (3)
and
.BR putc (3).
2004-11-03 13:51:07 +00:00
.LP
The
.BR flockfile ()
2007-12-22 21:40:53 +00:00
function waits for \fI*filehandle\fP to be
2004-11-03 13:51:07 +00:00
no longer locked by a different thread, then makes the
2007-12-22 21:40:53 +00:00
current thread owner of \fI*filehandle\fP, and increments
2004-11-03 13:51:07 +00:00
the lockcount.
.LP
The
.BR funlockfile ()
function decrements the lock count.
2004-11-03 13:51:07 +00:00
.LP
The
.BR ftrylockfile ()
function is a non-blocking version
of
.BR flockfile ().
It does nothing in case some other thread
2007-12-22 21:40:53 +00:00
owns \fI*filehandle\fP, and it obtains ownership and increments
2004-11-03 13:51:07 +00:00
the lockcount otherwise.
.SH "RETURN VALUE"
The
.BR ftrylockfile ()
function returns zero for success
2007-12-25 22:02:19 +00:00
(the lock was obtained), and nonzero for failure.
2004-11-03 13:51:07 +00:00
.SH ERRORS
None.
.SH "CONFORMING TO"
POSIX.1-2001.
2004-11-03 13:51:07 +00:00
.SH AVAILABILITY
2007-07-08 16:21:19 +00:00
These functions are available when
.B _POSIX_THREAD_SAFE_FUNCTIONS
is defined.
They are in libc since libc 5.1.1 and in glibc
2004-11-03 13:51:07 +00:00
since glibc 2.0.
.SH "SEE ALSO"
.BR unlocked_stdio (3)