Linux C 编程 —— fcntl、ioctl和stat区别

1、 fcntl

#include <unistd.h>
#include <fcntl.h>

int fcntl(int fd, int cmd, ... /* arg */ );

fcntl函数可以改变某进程中一个已打开的文件的属性,可以重新设置读、写、追加、非阻塞等标志(这些标志称为File Status Flag),而不必重新open文件。

通过fcntl设置的都是当前进程如何访问设备或文件的访问控制属性,例如读、写、追加、非阻塞、加锁   等,但并不设置文件或设备本身的属性,例如文件的读写权限、串口波特率等。

2、ioctl

#include <sys/ioctl.h>

int ioctl(int d, int request, ...);

ioctl函数用于设置某些设备本身的属性,例如串口波特率、终端窗口大小。

此函数一般用于底层驱动开发

3、 stat系列

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);

#include <fcntl.h>           /* Definition of AT_* constants */
#include <sys/stat.h>

int fstatat(int dirfd, const char *pathname, struct stat *buf,
            int flags);    

stat系列也是对文件属性本身的修改,文件可以打开也可以不打开,主要针对各种文件和目录等,而ioctl主要是针对IO设备。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值