flockfile 中文man页面

系统
标准输入输出库 stdio 函数是线程安全的。这是通过为每个文件对象 FILE 赋予一个锁定计数和 (当锁定计数非零时) 一个所有者线程来实现的。对每个库函数调用,这些函数等待直到文件对象 FILE 不再被一个不同的线程锁定,然后锁定它,进行所需的 I/O 操作,再次对它解锁。

NAME

flockfile, ftrylockfile, funlockfile - 为标准输入输出锁定文件 FILE  

SYNOPSIS 总览

#include <stdio.h>

void flockfile(FILE *filehandle);

int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);

DESCRIPTION 描述

标准输入输出库 stdio 函数是线程安全的。这是通过为每个文件对象 FILE 赋予一个锁定计数和 (当锁定计数非零时) 一个所有者线程来实现的。对每个库函数调用,这些函数等待直到文件对象 FILE 不再被一个不同的线程锁定,然后锁定它,进行所需的 I/O 操作,再次对它解锁。

(注意:这个锁定与由函数 flock(2) 和 lockf(3) 实现的锁定无关。)

所有这些操作对 C 程序员来说都是不可见的,但是有两种理由,需要进行更加细节的控制。其一,也许某个线程需要进行不可分割的一系列 I/O 操作,不应当被其他线程的 I/O 所终端。其二,出于效率因素,应当避免进行过多的锁定来提高效率。

为此,一个线程可以显式地锁定文件对象 FILE,接着进行它的一系列 I/O 操作,然后解锁。这样可以避免其他线程干扰。如果这样做的原因是需要达到更高的效率,应当使用 stdio 函数的非锁定版本来进行 I/O 操作:使用 getc_unlocked() 和 putc_unlocked() 来代替 getc() 和 putc()。

函数 flockfile() 等待 *filehandle 不再被其他的线程锁定,然后使当前线程成为 *filehandle 的所有者,然后增加锁定计数 lockcount。

函数 funlockfile() 减少锁定计数。

函数 ftrylockfile()flockfile() 的非锁定版本。它在其他线程拥有 *filehandle 时不做任何处理,否则取得所有权并增加锁定计数。  

RETURN VALUE 返回值

函数 ftrylockfile() 返回零,如果成功的话 (获得了锁定);如果失败就返回非零。  

ERRORS

无。  

AVAILABILITY

这些函数当定义了 _POSIX_THREAD_SAFE_FUNCTIONS 时可用。它们存在于 libc 5.1.1 之后的 libc 版本中,以及 glibc 2.0 之后的 glibc 版本中  

CONFORMING TO 标准参考

POSIX.1  

SEE ALSO 参见

unlocked_stdio(3)

#p#

NAME

flockfile, ftrylockfile, funlockfile - lock FILE for stdio  

SYNOPSIS

#include <stdio.h>

void flockfile(FILE *filehandle);

int ftrylockfile(FILE *filehandle);
void funlockfile(FILE *filehandle);

DESCRIPTION

The stdio functions are thread-safe. This is achieved by assigning to each FILE object a lockcount and (if the lockcount is nonzero) an owning 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 interrupted 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() and putc_unlocked() instead of getc() and putc().

The flockfile() function waits for *filehandle to be no longer locked by a different thread, then makes the current thread owner of *filehandle, 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 nonzero for failure.  

ERRORS

None.  

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.  

CONFORMING TO

POSIX.1  

SEE ALSO

unlocked_stdio(3)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-12 14:58:05

killall中文man

2011-07-15 16:58:36

ac中文man

2011-08-15 11:10:48

more中文man

2011-08-25 17:03:51

pclose中文man

2011-08-15 14:10:37

tar中文man

2011-08-16 10:42:30

rmmod中文man

2011-08-18 13:57:38

acct中文man

2011-08-23 17:49:36

zdump中文man

2011-08-15 15:10:49

wall中文man

2011-08-23 15:06:03

quotastats中文man

2011-08-15 17:35:50

ar中文man

2011-08-25 09:07:16

suffixes中文man

2011-08-18 15:21:37

autofs中文man

2011-08-25 15:19:39

dirname中文man

2011-08-25 17:34:50

setlinebuf中文man

2011-08-15 15:17:14

ac中文man
点赞
收藏

51CTO技术栈公众号