fcntl即F_SETFL,F_GETFL的使用,设置文件的flags
2012-09-18 10:42
447 查看
1、获取文件的flags,即open函数的第二个参数:
flags = fcntl(fd,F_GETFL,0);
2、设置文件的flags:
fcntl(fd,F_SETFL,flags);
3、增加文件的某个flags,比如文件是阻塞的,想设置成非阻塞:
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
4、取消文件的某个flags,比如文件是非阻塞的,想设置成为阻塞:
flags = fcntl(fd,F_GETFL,0);
flags &= ~O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
获取和设置文件flags举例::
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <error.h>
char buf[500000];
int main(int argc,char *argv[])
{
int ntowrite,nwrite;
const char *ptr ;
int flags;
ntowrite = read(STDIN_FILENO,buf,sizeof(buf));
if(ntowrite <0)
{
perror("read STDIN_FILENO fail:");
exit(1);
}
fprintf(stderr,"read %d bytes\n",ntowrite);
if((flags = fcntl(STDOUT_FILENO,F_GETFL,0))==-1)
{
perror("fcntl F_GETFL fail:");
exit(1);
}
flags |= O_NONBLOCK;
if(fcntl(STDOUT_FILENO,F_SETFL,flags)==-1)
{
perror("fcntl F_SETFL fail:");
exit(1);
}
ptr = buf;
while(ntowrite > 0)
{
nwrite = write(STDOUT_FILENO,ptr,ntowrite);
if(nwrite == -1)
{
perror("write file fail:");
}
if(nwrite > 0)
{
ptr += nwrite;
ntowrite -= nwrite;
}
}
flags &= ~O_NONBLOCK;
if(fcntl(STDOUT_FILENO,F_SETFL,flags)==-1)
{
perror("fcntl F_SETFL fail2:");
}
return 0;
flags = fcntl(fd,F_GETFL,0);
2、设置文件的flags:
fcntl(fd,F_SETFL,flags);
3、增加文件的某个flags,比如文件是阻塞的,想设置成非阻塞:
flags = fcntl(fd,F_GETFL,0);
flags |= O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
4、取消文件的某个flags,比如文件是非阻塞的,想设置成为阻塞:
flags = fcntl(fd,F_GETFL,0);
flags &= ~O_NONBLOCK;
fcntl(fd,F_SETFL,flags);
获取和设置文件flags举例::
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <error.h>
char buf[500000];
int main(int argc,char *argv[])
{
int ntowrite,nwrite;
const char *ptr ;
int flags;
ntowrite = read(STDIN_FILENO,buf,sizeof(buf));
if(ntowrite <0)
{
perror("read STDIN_FILENO fail:");
exit(1);
}
fprintf(stderr,"read %d bytes\n",ntowrite);
if((flags = fcntl(STDOUT_FILENO,F_GETFL,0))==-1)
{
perror("fcntl F_GETFL fail:");
exit(1);
}
flags |= O_NONBLOCK;
if(fcntl(STDOUT_FILENO,F_SETFL,flags)==-1)
{
perror("fcntl F_SETFL fail:");
exit(1);
}
ptr = buf;
while(ntowrite > 0)
{
nwrite = write(STDOUT_FILENO,ptr,ntowrite);
if(nwrite == -1)
{
perror("write file fail:");
}
if(nwrite > 0)
{
ptr += nwrite;
ntowrite -= nwrite;
}
}
flags &= ~O_NONBLOCK;
if(fcntl(STDOUT_FILENO,F_SETFL,flags)==-1)
{
perror("fcntl F_SETFL fail2:");
}
return 0;
相关文章推荐
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags,阻塞设置成非阻塞,非阻塞设置成阻塞(这连个在server开发中可以封装为基本函数)
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags,阻塞设置成非阻塞,非阻塞设置成阻塞(这连个在server开发中可以封装为基本函数)
- fcntl即F_SETFL,F_GETFL的使用,设置文件的flags
- 使用 fcntl 函数 获取,设置文件的状态标志
- 第七篇:使用 fcntl 函数 获取,设置文件的状态标志
- Linux使用笔记: 设置Samba服务器中新建文件/目录的权限
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionSta
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 使用 aptoncd 备份已下载的 deb 包为 iso 文件,并设置为本地源
- 将所有程序设置XML集中到一个单独XML配置文件的方法:使用appSettings元素的configSource元素
- 使用Bash编写Liunx 脚本程序——3、文件、用户和外壳的自定义设置
- yum的设置文件设置-CentOS yum 源的配置与使用
- log4j使用: 自动设置备份文件
- 有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 <configuration>\<system.web>\<
- 解决Qt中文乱码以及汉字编码的问题(UTF-8/GBK)——ubuntu环境设置默认是utf-8,文件编码可使用Encodersoft批量转换
- 菜鸟教程之工具使用(十三)——设置Eclipse文件默认打开方式
- Xamarin XAML语言教程隐藏文件使用Progress属性设置进度条