fcntl函数:操纵文件描述符,改变已经打开的文件的属性
int fcntl(int fd, int cmd, ... //arg  );
cmd选项:
一、复制文件描述符:F_DUPFD
二、更改设置文件描述标志:F_GETFD 、F_SETFD  文件描述符标志,是体现进程的文件描述符的状态.
当前只定义了一个文件描述符标志FD_CLOEXEC。
   0: exec时不关闭已经打开的文件描述符
   1: exec时关闭已经打开的文件描述符

三、获取或者设置文件状态标识:F_GETFL、 F_SETFL(读写追加阻塞等等)
    文件状态标签中的标志可分为三类:访问方式、打开时标志和I/O操作方式。
 1. 访问方式 访问方式指明允许文件描述字用于读、写或两者兼之,包括O_RDONLY、O_WRONLY和O_RDWR。
这些访问方式在文件被打开时选定,之后便不能再改变。
 2、打开时标志指明打开文件时影响open()行为的一些选项。这些选项一旦文件打开就不保留,
但有一个例外是O_NONBLOCK,因为O_NONBLOCK同时也是一个I/O操作方式,故此标志被保留例如:O_CREAT
 3、I/O操作方式:I/O操作方式影响使用文件描述字进行输入输出操作的工作方式。这些标志由open()设置,
之后可以用fcntl()获取和改变。O_APPEND、O_NONBLOCK、O_SYNC等
四、设置获取文件锁:F_GETLK、F_SETLK,F_SETLKW

 1 #include<unistd.h>
2 #include<sys/types.h>
3 #include<sys/stat.h>
4 #include<fcntl.h>
5 #include<stdlib.h>
6 #include<stdio.h>
7 #include<errno.h>
8 #include<string.h>
9 #define ERR_EXIT(m)\
10 do\
11 {\
12 perror(m);\
13 exit(EXIT_FAILURE);\
14 }while(0) //宏要求一条语句
15 void set_flags(int fd,int flags);
16 void clr_flags(int fd,int flags);
17 int main(int argc,char*argv[])
18 {
19 char buf[1024]={0};
20 int ret;
21 /* flags=fcntl(0,F_GETFL,0);//先获取标准输入的标志
22 if(flags==-1)
23 ERR_EXIT("fcntl get flags error");
24 ret=fcntl(0,F_SETFL,flags|O_NONBLOCK);//将标准输入设置为非阻塞,默认对读是阻塞的。不更改其他状态
25 if(ret==-1)
26 ERR_EXIT("fcntl set flags error");
27 */ set_flags(0,O_NONBLOCK);//设置非阻塞,直接返回 (EAGIAN)read error: Resource temporarily unavailable(资源暂且不可用),如果是socket,会返回EWOUDBOCK
28 //clr_flags(0,O_NONBLOCK);//清除
29 ret=read(0,buf,1024);//标准输入读,read标准输入0默认阻塞(文件状态标志)
30 if(ret==-1)
31 ERR_EXIT("read error");
32 printf("buf=%s\n",buf);
33 return 0;
34 }
35 void set_flags(int fd,int flags)
36 {
37 int val;
38 val=fcntl(fd,F_GETFL,0);//先获取标准输入的标志
39 if(val==-1)
40 ERR_EXIT("fcntl get flags error");
41 val |=flags;
42 if(fcntl(fd,F_SETFL,val)<0)
43 ERR_EXIT("fcntl set flags error");
44 }
45 void clr_flags(int fd,int flags)//清除状态
46 {
47 int val;
48 val=fcntl(fd,F_GETFL,0);//先获取标准输入的标志
49 if(val==-1)
50 ERR_EXIT("fcntl get flags error");
51 val&=(~flags);//原状态中与上 flags 的反
52 if(fcntl(fd,F_SETFL,val)<0)
53 ERR_EXIT("fcntl set flags error");
54 }

下面的例子讲解fcntl更改设置文件描述标志:

 1 //hello.c
2 #include<unistd.h>
3 #include<stdio.h>
4 //hello程序打印程序环境变量
5 extern char** environ;//指针的指针,指向一个指针数组 environ-->[...]数组中每一项指向一个环境信息。例如"TERM=VI00","SHELL=/bin/bash"
6 int main(void)
7 {
8 printf("hello pid=%d\n",getpid());
9 //打印环境变量
10 int i;
11 for(i=0;environ[i]!=NULL;i++)
12 {
13 printf("%s\n",environ[i]);
14 }
15 return 0;
16 }
17
18 //ececlp.标准输出在execlp中已经关闭,无法使用
19 #include<unistd.h>
20 #include<sys/types.h>
21 #include<stdlib.h>
22 #include<stdio.h>
23 #include<errno.h>
24 #include<fcntl.h>
25 #define ERR_EXIT(m)\
26 do\
27 {\
28 perror(m);\
29 exit(EXIT_FAILURE);\
30 }while(0) //宏要求一条语句
31 int main()
32 {
33 printf("Entering main...\n");
34 int flag=fcntl(1,F_GETFD);
35 int ret=fcntl(1,F_SETFD,flag|FD_CLOEXEC);//标准输出EXEC位置1
36 if(ret==-1)
37 perror("fcntl");
38 //使用exec执行的程序里,此描述符被关闭,不能再使用它
39 execlp("./hello","hello",NULL);//ececlp替换是成功的,但是标准输出EXEC位已经被置位,所以hello无法输出。
40 /*输出结果
41 Entering main...
42 */
43 printf("Exiting main...\n");//不输出了
44 return 0;
45 }

fcntl函数用法——操纵文件描述符状态的更多相关文章

  1. fcntl函数用法——复制文件描述符

    文件描述符复制和输出重定向dup() 用最小的文件描述符来复制,从0开始搜素.复制文件描述符后,最小文件描述符指向被复制描述符指向的文件.dup2(int oldfd,int newfd) 强制用ne ...

  2. linux系统编程之文件与IO(八):文件描述符相关操作-dup,dup2,fcntl

    本节目标: 1,文件共享 打开文件内核数据结构 一个进程两次打开同一个文件 两个进程打开同一个文件 2,复制文件描述符(dup.dup2.fcntl) 一,文件共享 1,一个进程打开两个文件内核数据结 ...

  3. UNIX环境编程学习笔记(4)——文件I/O之dup复制文件描述符

    lienhua342014-08-23 UNIX 提供了两个函数 dup 和 dup2 用于复制一个现存的文件描述符. #include <unistd.h> int dup(int fi ...

  4. Unix系统编程()复制文件描述符

    Bourne shell的IO重定向语法2>&1,意在通知shell把标准错误(文件描述符2)重定向到标准输出(文件描述符1).因此下列命令将把标准输出和标准错误写入result.log ...

  5. 文件描述符file descriptor与inode的相关知识

    每个进程在Linux内核中都有一个task_struct结构体来维护进程相关的 信息,称为进程描述符(Process Descriptor),而在操作系统理论中称为进程控制块 (PCB,Process ...

  6. Linux中通过Socket文件描述符寻找连接状态介绍

    针对下文的总结:socket是一种文件描述符 进程的打开文件描述符表 Linux的三个系统调用:open,socket,pipe 返回的都是一个描述符.不同的进程中,他们返回的描述符可以相同.那么,在 ...

  7. dup等复制文件描述符函数

    [root@bogon code]# cat b.c #include<stdio.h> #include<error.h> #include<unistd.h> ...

  8. 进程间传递文件描述符——sendmsg和recvmsg函数

    先引入一个例子,该程序的目的是子进程向父进程传递文件描述符,并通过该文件描述符读取buf. #include <func.h> int main(){ int fds[2]; pipe(f ...

  9. linux fcntl 对文件描述符控制

    linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制 linux fcntl 对文件描述符控制

随机推荐

  1. markdown的基本使用

    1.什么是markdown? markdown是一种轻量级的标记语言 可以转换为html/xhtml和其它格式 可读.直观.学习成本低 当你学会使用markdown编写文档时,你会感觉自己发现了一个新 ...

  2. Mac快捷键整理(随时更新)

    一.文字编辑 快捷键 含义 解释 control + A 到行首 A没找到对应单词,暂时认为A是字母表的开始 control + E 到行尾 E是end的缩写

  3. Token 、Cookie和Session的区别

    本文转至http://blog.csdn.net/tobetheender/article/details/52485948 https://blog.csdn.net/axin66ok/articl ...

  4. 【C语言/C++编程学习笔记】你的第一个Windows程序!高级操作~

    什么是windows编程?了解到Windows API 编程.Windows编程.Windows SDK 编程是一个概念.今天我们运用C语言来实现你的第一个真正的Windows程序. windows. ...

  5. centos7下面 es7.5 搭建

    centos6 搭建 参考 https://www.cnblogs.com/php-linux/p/8758788.html 搭建linux虚拟机 https://www.cnblogs.com/ph ...

  6. 使用原生js模拟jQuery选择器,实现new方法,兼容ie5

    // 考虑到兼容ie5,未使用es6语法 /* 使用方法: 在<head>标签中(需使用ready方法): <script src="./jQuery2.js"& ...

  7. APP脱壳方法三

    第一步 手机启动frida服务 第二步 手机打开要脱壳的app 第三步编辑hook代码 agent.js /* * Author: hluwa <hluwa888@gmail.com> * ...

  8. 【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误

    问题情形 使用EF Core访问数据库,在本地运行正常,发布到App Service后,偶尔出现了Cannot create a DbSet for ** because this type is n ...

  9. Vue踩坑日记-You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.

    记录时间:2019年4月24日16:55:54 在build/webpack.base.conf.js文件中,注释或者删除掉:module->rules中有关eslint的规则

  10. pytest参数化代码笔记

    #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import pytest __author__ = "Carp-Li" __da ...