xref: /titanic_50/usr/src/man/man3c/flockfile.3c (revision aab83bb83be7342f6cfccaed8d5fe0b2f404855d)
te
Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
Copyright 1989 AT&T
Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
http://www.opengroup.org/bookstore/.
The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
This notice shall appear on any product containing this material.
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
FLOCKFILE 3C "Sep 10, 2003"
NAME
flockfile, funlockfile, ftrylockfile - acquire and release stream lock
SYNOPSIS

#include <stdio.h>

void flockfile(FILE *stream);

void funlockfile(FILE *stream);

int ftrylockfile(FILE *stream);
DESCRIPTION

The flockfile() function acquires an internal lock of a stream stream. If the lock is already acquired by another thread, the thread calling flockfile() is suspended until it can acquire the lock. In the case that the stream lock is available, flockfile() not only acquires the lock, but keeps track of the number of times it is being called by the current thread. This implies that the stream lock can be acquired more than once by the same thread.

The funlockfile() function releases the lock being held by the current thread. In the case of recursive locking, this function must be called the same number of times flockfile() was called. After the number of funlockfile() calls is equal to the number of flockfile() calls, the stream lock is available for other threads to acquire.

The ftrylockfile() function acquires an internal lock of a stream stream, only if that object is available. In essence ftrylockfile() is a non-blocking version of flockfile().

RETURN VALUES

The ftrylockfile() function returns 0 on success and non-zero to indicate a lock cannot be acquired.

EXAMPLES

Example 1 A sample program of flockfile().

The following example prints everything out together, blocking other threads that might want to write to the same file between calls to fprintf(3C):

FILE iop;
flockfile(iop);
fprintf(iop, "hello ");
fprintf(iop, "world);
fputc(iop, 'a');
funlockfile(iop);

An unlocked interface is available in case performance is an issue. For example:

flockfile(iop);
while (!feof(iop)) {
 *c++ = getc_unlocked(iop);
}
funlockfile(iop);
ATTRIBUTES

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE ATTRIBUTE VALUE
Interface Stability Standard
MT-Level MT-Safe
SEE ALSO

Intro(3), __fsetlocking(3C), ferror(3C), fprintf(3C), getc(3C), putc(3C), stdio(3C), ungetc(3C), attributes(5), standards(5)

NOTES

The interfaces on this page are as specified in IEEE Std 1003.1:2001. See standards(5).