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

    xiaoxiao2022-06-26  156

    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设备。  


    最新回复(0)