.\" Copyright (C) 2001 Andries Brouwer . .\" .\" 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 .\" .\" 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. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .TH LOCKFILE 3 2001-10-18 "" "Linux Programmer's Manual" .SH NAME flockfile, ftrylockfile, funlockfile \- 爲標準輸入輸出鎖定文件 FILE .SH "SYNOPSIS 總覽" .nf .B #include .sp .BI "void flockfile(FILE *" filehandle ); .br .BI "int ftrylockfile(FILE *" filehandle ); .br .BI "void funlockfile(FILE *" filehandle ); .fi .SH "DESCRIPTION 描述" 標準輸入輸出庫 stdio 函數是線程安全的。這是通過爲每個文件對象 FILE 賦予一個鎖定計數和 (當鎖定計數非零時) 一個所有者線程來實現的。對每個庫函數調用,這些函數等待直到文件對象 FILE 不再被一個不同的線程鎖定,然後鎖定它,進行所需的 I/O 操作,再次對它解鎖。 .LP (注意:這個鎖定與由函數 .BR flock (2) 和 .BR lockf (3) 實現的鎖定無關。) .LP 所有這些操作對 C 程序員來說都是不可見的,但是有兩種理由,需要進行更加細節的控制。其一,也許某個線程需要進行不可分割的一系列 I/O 操作,不應當被其他線程的 I/O 所終端。其二,出於效率因素,應當避免進行過多的鎖定來提高效率。 .LP 爲此,一個線程可以顯式地鎖定文件對象 FILE,接着進行它的一系列 I/O 操作,然後解鎖。這樣可以避免其他線程干擾。如果這樣做的原因是需要達到更高的效率,應當使用 stdio 函數的非鎖定版本來進行 I/O 操作:使用 \fIgetc_unlocked\fP() 和 \fIputc_unlocked\fP() 來代替 \fIgetc\fP() 和 \fIputc\fP()。 .LP 函數 \fBflockfile()\fP 等待 *\fIfilehandle\fP 不再被其他的線程鎖定,然後使當前線程成爲 *\fIfilehandle\fP 的所有者,然後增加鎖定計數 lockcount。 .LP 函數 \fBfunlockfile()\fP 減少鎖定計數。 .LP 函數 \fBftrylockfile()\fP 是 \fBflockfile()\fP 的非鎖定版本。它在其他線程擁有 *\fIfilehandle\fP 時不做任何處理,否則取得所有權並增加鎖定計數。 .SH "RETURN VALUE 返回值" 函數 \fBftrylockfile()\fP 返回零,如果成功的話 (獲得了鎖定);如果失敗就返回非零。 .SH ERRORS 無。 .SH AVAILABILITY 這些函數當定義了 _POSIX_THREAD_SAFE_FUNCTIONS 時可用。它們存在於 libc 5.1.1 之後的 libc 版本中,以及 glibc 2.0 之後的 glibc 版本中 .SH "CONFORMING TO 標準參考" POSIX.1 .SH "SEE ALSO 參見" .BR unlocked_stdio (3) .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR