fcntl
来源:互联网 发布:混沌战域翅膀进阶数据 编辑:程序博客网 时间:2024/05/17 00:00
概述
fcntl函数是一个多功能文件管理的工具箱,接收2个参数+1个变参。
正文
函数
函数原型
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
参数
fd:文件描述词。
cmd:操作命令。
(1)F_DUPFD这个cmd的作用是复制文件描述符(作用类似于dup和dup2),这个命令的功能是从可用的fd数字列表中找一个比arg大或者和arg一样大的数字作为oldfd的一个复制的fd,和dup2有点像但是不同。dup2返回的就是我们指定的那个newfd否则就会出错,但是F_DUPFD命令返回的是>=arg的最小的那一个数字。
arg:供命令使用的参数。
lock:同上。
返回值
fcntl的返回值: 与命令有关。如果出错,所有命令都返回-1,如果成功则返回某个其他值。
测试代码
int main(int argc,char * argv){ int fd1 = -1,fd2 = -1;/*file descriptor(文件描述符)*/ /*1.open file*/ fd1 = open(MY_FILE,O_RDWR);/*打开了一个不存在的文件*/ if(fd1<0) { perror("open"); goto open_err; } printf("fd1 = %d\r\n",fd1); /*2.fcntl file descriptor*/ fd2 = fcntl(fd1,F_DUPFD,6);/*指定fd2要>=6*/ if(fd2<0) { perror("dup"); goto fcntl_err; } printf("fd2 = %d\r\n",fd2); /*3.close file*/ close(fd1); return 0;fcntl_err: close(fd1);open_err: return -1;}
参考
http://blog.csdn.net/bailyzheng/article/details/7463775
http://www.cnblogs.com/xuyh/p/3273082.html
http://blog.csdn.net/xiaoliangsky/article/details/40303245
阅读全文
0 0
- fcntl()
- fcntl()
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl()
- fcntl
- fcntl
- fcntl
- fcntl函数
- fcntl函数
- fcntl函数
- fcntl函数
- 浅谈外存分配的几种方式
- SQL Server 2008 如何开启远程数据库的连接【sql server 错误号53;17】
- 慕课网-Java入门第二季实战练习-答答租车系统
- 如何存储和获取数据
- chm打不开的办法
- fcntl
- QiYuAdmin-metronic首页的js和css介绍(SpringBoot项目实战)
- sql 中 limit 与 limit,offset连用的区别
- STL编程题-集合动态修改
- Spring 使用注解方式进行事务管理
- 一次性使用的小ATM(笑趴
- java 反射机制小结
- ServletConfig使用说明
- 怎样才算好代码