Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

ftrylockfile(3) [netbsd man page]

FLOCKFILE(3)						   BSD Library Functions Manual 					      FLOCKFILE(3)

NAME
flockfile, ftrylockfile, funlockfile -- stdio stream locking functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> void flockfile(FILE *file); int ftrylockfile(FILE *file); void funlockfile(FILE *file); DESCRIPTION
The flockfile(), ftrylockfile(), and funlockfile() functions provide applications with explicit control of locking of stdio stream objects. They can be used by a thread to execute a sequence of I/O operations as a unit, without interference from another thread. Locks on stdio streams are recursive, and a lock count is maintained. stdio streams are created unlocked, with a lock count of zero. After successful acquisition of the lock, its count is incremented to one, indicating locked state of the stdio stream. Each subsequent relock operation performed by the owner thread increments the lock count by one, and each subsequent unlock operation performed by the owner thread decrements the lock count by one, allowing matching lock and unlock operations to be nested. After its lock count is decremented to zero, the stdio stream returns to unlocked state, and ownership of the stdio stream is relinquished. The flockfile() function acquires the ownership of file for the calling thread. If file is already owned by another thread, the calling thread is suspended until the acquisition is possible (i.e., file is relinquished again and the calling thread is scheduled to acquire it). The ftrylockfile() function acquires the ownership of file for the calling thread only if file is available. The funlockfile() function relinquishes the ownership of file previously granted to the calling thread. Only the current owner of file may funlockfile() it. RETURN VALUES
If successful, the ftrylockfile() function returns 0. Otherwise, it returns non-zero to indicate that the lock cannot be acquired. SEE ALSO
flock(2), getc_unlocked(3), getchar_unlocked(3), lockf(3), putc_unlocked(3), putchar_unlocked(3) STANDARDS
The flockfile(), ftrylockfile() and funlockfile() functions conform to IEEE Std 1003.1-2001 (``POSIX.1''). HISTORY
The flockfile() function first appeared in FreeBSD 2.0. BUGS
The design of these interfaces does not allow for addressing the problem of priority inversion. BSD
October 15, 2011 BSD

Check Out this Related Man Page

flockfile(3C)						   Standard C Library Functions 					     flockfile(3C)

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 call- ing 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). SunOS 5.10 10 Sep 2003 flockfile(3C)
Man Page