flockfileのヘルプ・マニュアル
日本語 英語
flockfile --help
man flockfile
FLOCKFILE(3) Linux Programmer’s Manual FLOCKFILE(3)
名前
flockfile, ftrylockfile, funlockfile - 標準入出力 FILE のロックを行う
書式
#include
void flockfile(FILE *filehandle);
int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
上記の全ての関数: _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE
|| _SVID_SOURCE || _POSIX_SOURCE
説明
標準入出力関数はスレッドセーフである。これは、各 FILE オブジェクトに 対
し 、ロック数 (lockcount) と (ロック数が 0 でない場合は) 所有者スレッド
(owner thread) を管理することで実現される。ライブラリの呼び出しが行われ
る 毎に、標準入出力関数は FILE オブジェクトが他のスレッドによってロック
されていない状態になるまで待ち、 FILE オブジェクトをロックし、要求さ れ
て入出力を行い、オブジェクトのロックを解除する。
(注: このロックは、 flock(2) や lockf(3) といった関数が行うロックとは全
く無関係である。)
これらのことはすべて C プログラマには見えない部分で行われるが、より細か
い制御ができた方がよい理由が2つあるだろう。一つは、一つのスレッドが行う
一連の入出力動作は一緒に行われ、他のスレッドの入出力によって中断され な
い 方がよいということであろう。もう一つは、効率を大きく上げるためにはロ
ックのオーバヘッドを避ける必要があるということであろう。
この目的を実現するために、 FILE オブジェクトのロック、一連の入出力動 作
の 実行、ロックの解除をスレッドが明示的に指示することができる。これによ
り、他のスレッドが途中で入出力を行うのを防止する。このようなことを行 う
理 由が効率の向上であるならば、ロックを行わないバージョンの標準入出力関
数を使うこともできる。例えば 、 getc(3) や putc(3) の 代 わ り に
getc_unlocked(3) や putc_unlocked(3) を使用する。
flockfile() 関数は、*filehandle が他のスレッドにロックされていない状態
になるまで待ったのち、現在のスレッドを *filehandle のオーナに設定し、ロ
ック数を加算する。
funlockfile() 関数は、ロック数を減算する。
ftrylockfile() 関数は flockfile() のブロッキングを行わないバージョンで
ある。他のスレッドが *filehandle をロックしている時は何も行わず、そうで
ない場合は *filehandle の所有権を獲得し、ロック数を加算する。
返り値
ftrylockfile() 関数はロックに成功すると 0 を返し、失敗した場合は 0 以外
の値を返す。
エラー
なし。
準拠
POSIX.1-2001.
可用性
_POSIX_THREAD_SAFE_FUNCTIONS が定義されているときにこれらの関数を使用す
ることができる。 5.1.1 以降の libc と 2.0 以降の glibc に存在する。
関連項目
unlocked_stdio(3)
2008-08-29 FLOCKFILE(3)
FLOCKFILE(3) Linux Programmer’s Manual FLOCKFILE(3)
NAME
flockfile, ftrylockfile, funlockfile - lock FILE for stdio
SYNOPSIS
#include
void flockfile(FILE *filehandle);
int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
All functions shown above: _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE ||
_BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
DESCRIPTION
The stdio functions are thread-safe. This is achieved by assigning to
each FILE object a lockcount and (if the lockcount is non-zero) an own-
ing thread. For each library call, these functions wait until the FILE
object is no longer locked by a different thread, then lock it, do the
requested I/O, and unlock the object again.
(Note: this locking has nothing to do with the file locking done by
functions like flock(2) and lockf(3).)
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 a series of
I/O actions by one thread belongs together, and should not be inter-
rupted by the I/O of some other thread. On the other hand, maybe the
locking overhead should be avoided for greater efficiency.
To this end, a thread can explicitly lock the FILE object, then do its
series of I/O actions, then unlock. This prevents other threads from
coming in between. If the reason for doing this was to achieve greater
efficiency, one does the I/O with the non-locking versions of the stdio
functions: with getc_unlocked(3) and putc_unlocked(3) instead of
getc(3) and putc(3).
The flockfile() function waits for *filehandle to be no longer locked
by a different thread, then makes the current thread owner of *filehan-
dle, and increments the lockcount.
The funlockfile() function decrements the lock count.
The ftrylockfile() function is a non-blocking version of flockfile().
It does nothing in case some other thread owns *filehandle, and it
obtains ownership and increments the lockcount otherwise.
RETURN VALUE
The ftrylockfile() function returns zero for success (the lock was
obtained), and non-zero for failure.
ERRORS
None.
CONFORMING TO
POSIX.1-2001.
AVAILABILITY
These functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is
defined. They are in libc since libc 5.1.1 and in glibc since glibc
2.0.
SEE ALSO
unlocked_stdio(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2008-08-29 FLOCKFILE(3)