fcntl库:IO控制库

原文链接:fcntl库:IO控制库

IO

IO就是一个输入输出流,最基础的IO能够写和读.IO用文件描述符fd标志,操作系统默认0(in用户输入),1(out 屏幕输出),2(error 错误输出),fd是一个正整数

可以通过open(文件),fork子进程,pipe(管道),socket(套接字)创建对应特性的IO.以及通过相应的操作设置fd以达到要求的特性,如阻塞非阻塞。

多个进程可以同时对IO读写,因此需要考虑进程的同步

fcntl:IO的通用控制函数

linux系统下的系统调用,需要<unistd.h>,<fcntl.h>

fcntl库函数
int fcntl(int fd, int cmd); //只有指令参数,用于获取设置状态
int fcntl(int fd, int cmd, long arg); //用于设置状态
int fcntl(int fd, int cmd, struct flock *lock); //为文件锁提供的函数,给文件加锁或解锁如 F_RDLCK F_WRLCK F_UNLCK
    return -1 表示执行错误
指令cmd类型(数字表示对应参数的数值):

F_DUPFD 0:复制现有描述符
    return 新的文件描述符(该文件描述符实际指向原文件)
F_GETFD 1 F_SETFD 2

F_GETFL 3 F_SETFL 4 
    return 返回相应标志
    //O_APPEND(末尾写入),O_DIRECT(cache直接写)
    //O_NONBLOCK非阻塞模式(读写都不阻塞),
    //O_SYNC同步写入(等写操作结束后,只是写阻塞),
    //O_ASYNC异步写入(写),通过SIGIO返回信号通知进程,可以定义信号处理函数,类似CPU的中断处理方法

F_GETOWN 9 F_SETOWN 8
    return 进程ID

F_GETLK 5 F_SETLK 6 F_SETLKW 7
指令实例

熟悉每个指令增加对IO的理解

F_DUPFD :复制当前fd,成功返回复制的fd,否则-1
#include <iostream>
#include<fcntl.h>
#include<unistd.h>
#include<cstring> //strlen函数
using namespace std;
int main()
{  
    int out_fd=1;
    
    int min_fd=100;
    //min_fd指定允许的最小fd
    int new_fd=fcntl(out_fd,F_DUPFD,min_fd);
    if(new_fd==-1){
        cout<<"fcntl F_DUPFD error\n";
    }
    //默认打开的
    cout<<"fcntl F_DUPFD:"<<new_fd<<"\n";
    
    const char* message= "Hello, world!";
    ssize_t bytes_written = write(new_fd, message, strlen(message));
    if(bytes_written==-1){
        cout<<"write error\n";
    }
    close(new_fd);//关闭后无法再写入
    
    return 0;
}

fcntl F_DUPFD:100
Hello, world!
F_GETFD:查看当前fd的FD_CLOEXEC状态

FD_CLOEXEC 是控制当前fd在执行exec指令时是否会被自动关闭
作用是 在多进程或者多线程的环境中,有大量的exec指令,可以帮助节省文件描述符资源

#include <iostream>
#include<fcntl.h>
#include<unistd.h>
using namespace std;
int main()
{  
    int out_fd=1;
    int flag=fcntl(out_fd,F_GETFD);
    if(flag==-1){
        cout<<"fcntl F_GETFD error\n";
    }
    cout<<"fcntl F_GETFD:"<<flag<<"\n";
    
    flag|=FD_CLOEXEC;
    
    if(fcntl(out_fd,F_SETFD,flag)==-1){
        cout<<"fcntl F_SETFD error\n";
    }
    
    int new_flag=fcntl(out_fd,F_GETFD);
    if(new_flag==-1){
        cout<<"fcntl F_GETFD error\n";
    }
    cout<<"fcntl F_GETFD:"<<new_flag<<"\n";
    
    
    return 0;
}
fcntl F_GETFD:0
fcntl F_GETFD:1
F_GETFL和F_SETFL
#include <iostream>
#include<fcntl.h>
#include<unistd.h>
#include <stdio.h>
using namespace std;
int main()
{  
    int fd=open("file.txt",O_RDWR|O_CREAT,0666);
    int label=fcntl(fd,F_GETFL);
    if(label==-1){
        cout<<"fcntl F_GETFL error\n";
    }
    cout<<"fcntl F_GETFL:"<<label<<"\n";
    
    cout<<"O_RDWR:"<<(bool)(O_RDWR&label)<<"\n";
    cout<<"O_NONBLOCK:"<<(bool)(O_NONBLOCK&label)<<"\n";
    
    label|=O_NONBLOCK;
    if(fcntl(fd,F_SETFL,label)==-1){
        cout<<"fcntl F_GETFL error\n";
    }
    cout<<"O_RDWR:"<<(bool)(O_RDWR&label)<<"\n";
    cout<<"O_NONBLOCK:"<<(bool)(O_NONBLOCK&label)<<"\n";
    
    close(fd);
    return 0;
}
fcntl F_GETFL:32770
O_RDWR:1
O_NONBLOCK:0
O_RDWR:1
O_NONBLOCK:1

关于三种label标志,O_SYNC和O_ASYNC只对写进行同步异步处理,因为读不会产生数据冲突,只需要遵守写后读(read after write 英文after意思为读在写的后面)即可.

权限和文件锁后面有涉及就补上

F_GETOWN F_SETOWN
F_GETLK F_SETLK F_SETLKW
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aidroid

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值