一、接口介绍
fcntl()系统调用对一个打开的文件描述符执行一系列控制操作。
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
参数:
fd: open 打开的文件描述符
cmd: 太多,请看参考文档
F_GETFD
F_SETFD (int) // 这类带参数的都是要用到arg
F_GETLK
F_SETLK (struct flock *)
F_GETFL
F_SETFL (int)
...
arg:具体要看cmd 是否需要
所以由上面可以看出,fcntl其实相当于有三种:
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
返回值:
For a successful call, the return value depends on the operation:
F_DUPFD The new descriptor.
本文详细介绍了Linux系统调用fcntl的功能,包括复制文件描述符、获取/设置文件描述符和文件状态标志、控制异步I/O所有权以及记录锁的操作。通过fcntl,可以实现对文件的精细控制,如非阻塞I/O、追加写入等,并讨论了读锁、写锁及其在多进程并发访问文件时的重要性。同时,提到了强制锁和建议锁的概念,以及如何启用强制锁。
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



