该函数可以改变已打开的文件的性质。
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
第三个参数总是一个整数,与上面所示函数原型中的注释部分相对应。但是在作为记录锁用时,第三个参数则是指向一个结构的指针。
fcntl函数有5种功能:
1.复制一个现有的描述符(cmd = F_DUPFD).
2.获得/设置文件描述符标记(cmd = F_GETFD || F_SETFD).
3.获得/设置文件状态标记(cmd = F_GETFL || F_SETFL).
4.获得/设置异步I/O所有权(cmd = F_GETOWN || F_SETOWN).
5.获得/设置记录锁(cmd = F_GETLK, F_SETLK || F_SETLKW).
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
2
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
3
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
6
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
8
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
9
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
10
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
11
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
12
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
13
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
14
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
15
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
16
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
18
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
19
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
20
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
21
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
22
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
24
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
25
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://www.cppblog.com/Images/dot.gif)
26
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
27
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
28
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
29
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
30
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
31
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
32
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
33
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
34
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
35
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
36
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
37
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
38
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
39
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
40
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
42
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
43
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
44
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
46
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
47
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
48
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
49
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
50
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
51
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
52
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
53
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
fcntl文件锁有两种类型:建议性锁和强制性锁
建议性锁是这样规定的:每个使用上锁文件的进程都要检查是否有锁存在,当然还得尊重已有的锁。内核和系统总体上都坚持不使用建议性锁,它们依靠程序员遵守这个规定。
强制性锁是由内核执行的。当文件被上锁来进行写入操作时,在锁定该文件的进程释放该锁之前,内核会阻止任何对该文件的读或写访问,每次读或写访问都得检查锁是否存在。
系统默认fcntl都是建议性锁,强制性锁是非POSIX标准的。如果要使用强制性锁,要使整个系统可以使用强制性锁,那么得需要重新挂载文件系统, mount使用参数 -0 mand打开强制性锁,或者关闭已加锁文件的组执行权限并且打开该文件的set-GID权限位。
建议性锁只在cooperating processes之间才有用,对cooperating process的理解是最重要的,它指的是会影响其它进程的进程或被别的进程所影响的进程,举两个例子:
(1)我们可以同时在两个窗口中运行同一个命令,对同一个文件进行操作,那么这两个进程就是cooperating processes;
(2) cat file | sort, 那么cat和sort产生的进程就是使用了pipe的cooperating processes。
使用fcntl文件锁进行I/O操作必须小心:进程在开始任何I/O操作前如何去处理锁,在对文件解锁前如何完成所有的操作,是必须考虑的。如果在设置锁之前打开文件,或者读取该锁之后关闭文件,另一个进程就可能在上锁/解锁操作和打开/关闭操作之间的几分之一秒内访问该文件。当一个进程对文件加锁后,无论它是否释放所加的锁,只要文件关闭,内核都会自动释放加在文件上的建议性锁(这也是建议性锁和强制性锁的最大区别),所以不要想设置建议性锁来达到永久不让别的进程访问文件的目的(强制性锁才可以);强制性锁则对所有进程起作用。
fcntl使用三个参数
F_SETLK/F_SETLKW, F_UNLCK和F_GETLK, 来分别要求、释放、测试record locks。 record locks是对文件一部分而不是整个文件的锁,这种细致的控制使得进程更好地协作以共享文件资源。fcntl能够用于读取锁和写入锁,read lock也叫shared lock(共享锁),
因为多个cooperatingprocess能够在文件的同一部分建立读取锁;write lock被称为exclusive lock(排斥锁),因为任何时刻只能有一个cooperating process在文件的某部分上建立写入锁。如果cooperatingprocesses对文件进行操作,那么它们可以同时对文件加read lock,在一个cooperating process加writelock之前,必须释放别的cooperating process加在该文件的read lock和wrtielock,也就是说,对于文件只能有一个write
lock存在,read lock和wrtie lock不能共存。
转载转载者,未知原创者