IO fcntl
来源:互联网 发布:centos7 nginx 编辑:程序博客网 时间:2024/06/05 09:20
fcntl
以read终端设备为例介绍了非阻塞I/O,没有直接对STDIN_FILENO做非阻塞read,而是重新open一遍/dev/tty,因为STDIN_FILENO在程序启动时已经被自动打开了,而我们需要在调用open时指定O_NONBLOCK标志。这里介绍另外一种办法,可以用fcntl函数改变一个已打开的文件的属性,可以重新设置读、写、追加、非阻塞等标志(这些标志称为File Status Flag),而不必重新open文件。
#include
#include
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int fcntl(int fd, int cmd, struct flock *lock);
这个函数和open一样,也是用可变参数实现的,可变参数的类型和个数取决于前面的cmd参数。下面的例子使用F_GETFL和F_SETFL这两种fcntl命令改变STDIN_FILENO的属性,加上O_NONBLOCK选项,实现实例
例. 用fcntl改变File Status Flag
#include
#include
#include
#include
#include
#define MSG_TRY "try again/n"
int main(void)
{
char buf[10];
int n;
int flags;
flags = fcntl(STDIN_FILENO, F_GETFL);
flags |= O_NONBLOCK;
if (fcntl(STDIN_FILENO, F_SETFL, flags) == -1) {
perror("fcntl");
exit(1);
}
tryagain:
n = read(STDIN_FILENO, buf, 10);
if (n < 0) {
if (errno == EAGAIN) {
sleep(1);
write(STDOUT_FILENO, MSG_TRY, strlen(MSG_TRY));
goto tryagain;
}
perror("read stdin");
exit(1);
}
write(STDOUT_FILENO, buf, n);
return 0;
}
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int val;
if (argc != 2) {
fputs("usage: a.out /n", stderr);
exit(1);
}
if ((val = fcntl(atoi(argv[1]), F_GETFL)) < 0) {
printf("fcntl error for fd %d/n", atoi(argv[1]));
exit(1);
}
switch(val & O_ACCMODE) {
case O_RDONLY:
printf("read only");
break;
case O_WRONLY:
printf("write only");
break;
case O_RDWR:
printf("read write");
break;
default:
fputs("invalid access mode/n", stderr);
exit(1);
}
if (val & O_APPEND)
printf(", append");
if (val & O_NONBLOCK)
printf(", nonblocking");
putchar('/n');
return 0;
O_ACCMODE
O_ACCMODE<0003>:读写文件操作时,用于取出flag的低2位 O_RDONLY<00>:只读打开 O_WRONLY<01>:只写打开 O_RDWR<02>:读写打开
- IO fcntl
- 非阻塞IO--fcntl
- 高级IO-fcntl记录锁
- 8-文件IO-fcntl函数
- 文件IO---lseek、fcntl函数
- fcntl()
- fcntl()
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl
- fcntl()
- fcntl
- fcntl
- Coldfusion的另类replace。
- Coldfusion自定义函数的多参数。
- Coldfusion: 建立一个以逗号分开的字串。
- 字符串指针和字符串数组
- 《程序员的未来》
- IO fcntl
- IO ioctl
- IO lseek
- as2 flash firefox session image 上传图片 丢失问题
- 2010年9月開通
- ASP.NET 几种括尖括号+百分号用法 区别简介
- 诺基亚S60手机格机方法
- 小解C++的句柄类
- C++模板特化举例