U•ェ•*U follow me !!! 明天写文件锁记得 关注我哦!
1.sync/fsync/fdatasync
1.用于强制磁盘文件与缓冲区同步。
2.sync将所有被修改过的缓冲区排入写队列即返回,不等待写磁盘操作完成。
3.fsync只针对一个文件,且直到写磁盘操作完成才返回。
4.fdatasync只同步文件数据,不同步文件属性。
#include <unistd.h>
void sync(void);
int fsync(int fd);
int fdatasync(int fd);
2.fcntl
#include <unistd.h>
#include <fcntl.h>
int fcntl(
int fd, //文件描述符
int cmd, //操作指令
... /* arg */ //可变参数,因操作指令而异
);
cmd:
1.F_DUPFD //复制fd为不小于arg的文件描述符,若文件描述符已用,该函数会选择比arg大的最小未用值。
1 #include<stdio.h>
2 #include<unistd.h>
3 #include<fcntl.h>
4
5
6 int main(){
7 int fd1 = open("a.txt",O_CREAT|O_WRONLY|O_EXCL,0644);
8 if(fd1 == -1){
9 perror("open:");
10 return -1;
11 }
12 int fd2 = fcntl(fd1,F_DUPFD,1);
13 if(fd2 == -1){
14 perror("fcntl:");
15 return -1;
16 }
17 printf("fd1:%u fd2:%u\n",fd1,fd2);
18 return 0;
19 }
fd1:3 fd2:4
2.F_GETFD \获取文件描述符标志
——通常与FD_CLOEXEC一起使用
3.F_SETFD /设置文件描述符标志
http://hi.baidu.com/passerryan/item/d81eac1671ce3d5cf0090e2b
4.F_GETFL //获取文件状态标志。不能获取O_CREAT/O_EXCL/O_TRUNC.
5.F_SETFL //追加文件状态。只能追加O_APPEND/O_NONBLOCK