Linux 文件相关系统调用函数

本文介绍了5个Linux文件相关的系统调用函数:creat、open、read、write、close.

creat 系统调用

想用 creat 函数创建文本文件:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() { 
    creat("a.txt", O_RDONLY); 
}

这是makefile:

creat: creat.c
    @gcc -o creat creat.c
    @./creat
    @rm creat

结果,通过 ls -l 查看,生成的 a.txt 的权限是 000 ?
这个 O_RDONLY 是 fcntl.h 里的,源码贴过来:

/*
 * File status flags: these are used by open(2), fcntl(2).
 * They are also used (indirectly) in the kernel file structure f_flags,
 * which is a superset of the open/fcntl flags.  Open flags and f_flags
 * are inter-convertible using OFLAGS(fflags) and FFLAGS(oflags).
 * Open/fcntl flags begin with O_; kernel-internal flags begin with F.
 */
/* open-only flags */
#define O_RDONLY    0x0000      /* open for reading only */
#define O_WRONLY    0x0001      /* open for writing only */
#define O_RDWR      0x0002      /* open for reading and writing */
#define O_ACCMODE   0x0003      /* mask for above modes */

随后我试了 O_RDWR, 权限也是 000,又试了 O_WRONLY,权限是 001,试了 O_ACCMODE,权限也是 001 ?离谱。
然后我试了777,结果权限是 411,试了0777,结果权限是 755,试了 000 ,结果权限就是 000,试了 0000 ,权限也是 000,试了 111,结果是 155,试了 0111,结果是 111。


我大概找到规律了,首先,前面加0表示是8进制,不加0表示是10进制。这个计算是按8进制计算,所以10进制会被转成8进制,比如 777 就变成八进制的 01411,然后用这个8进制树去 “与” 0755(0777-umask),1411 & 755 得到 411,所以 777 权限是 411.
这也解释了 O_RDONLY,O_WRONLY,O_RDWR,O_ACCMODE 为什么会得到意料外的结果,因为他们本来就是 1,2,3.
但是这样定义这四种 mode 还有什么意义呢?Linux 系统下也是这样吗?(我用的 Mac)


我懂了,这四个模式是打开文件的时候用的啊!!不是给你创建文件的时候设置访问权限用的!!那个 O 是 open 的意思啊!!!

open 与 close 系统调用

接着往下写:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() { 
    int creat_res = creat("a.txt", 0755);
    int open_res = open("a.txt", O_RDWR);
    int close_res = close(creat_res);
    int close_res_2 = close(open_res);
    printf("creat res: %d\n", creat_res);
    printf("open res: %d\n", open_res);
    printf("close res: %d\n", close_res);
    printf("close res 2: %d\n", close_res_2);
}

Mac 下输出结果:

qiaodisheng@MBP: syscall $ make
creat res: 3
open res: 4
close res: 0
close res 2: 0

Ubuntu 下是 6,7,0,0
跑了很多次都是这个结果
close 系统调用参数是 file descriptor,返回值是 0 if (close successfully) else 1,所以我认为,create 和 open 返回的都是 file descriptor,相当于 a.txt 被打开了两次,两次的 fd 不一样。
为了验证我的想法,写了如下代码:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
int main() { 
    int creat_res = creat("a.txt", 0755);
    int open_res = open("a.txt", O_RDWR);
    int open_res_2 = open("a.txt", O_RDWR);
    printf("creat res: %d\n", creat_res);
    printf("open res: %d\n", open_res);
    printf("open res 2: %d\n", open_res_2);
}

Mac 下输出3,4,5,Ubuntu 下输出 6,7,8,证明当一个文件被多次打开时,会得到不同的 file descriptor。所以 create 和 open 同一个文件也会得到不同的 fd。


write 系统调用

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() { 
    int fd = creat("a.txt", 0755);
    write(fd, "a", 1);
    write(fd, "b", 1);
    write(fd, "c", 1);
    close(fd);
    return 0;
}

creat 在 fcntl.h 头文件中,write 和 close 在 unistd.h 头文件中,执行结果 a.txt 中被写入了 abc。这是一个比较简答的情况,write 的第一个参数是文件描述符,第二个是要写入的字符,第三个是写入字符的长度。返回值是成功写入的字符串长度。 也可以 write(fd, "abc", 3); 这样返回值是3,也不需要引入额外的头文件(c 不是不支持字符串的吗?还是自动帮我引入了,奇怪)


read 系统调用

a.txt 中已经写入 abc

#include <unistd.h>
#include <fcntl.h>
int main() {
    int fd = open("a.txt", O_RDWR);
    char buff[10] = {0};
    int read_res = read(fd, buff, 9);
    printf("read_length: %d\n", read_res);
    printf("read_res: %s\n", buff);

    close(fd);
    return 0;
}

输出结果:

qiaodisheng@MBP: syscall $ make
read_length: 3
read_res: abc

read 系统调用,第一个参数是 fd,第二个参数是读出的数据写到的位置,第三个参数是想读多少数据,返回实际读了多少数据。当文件总长度小于第三个参数时,即返回文件总长度。


最后总结一下

int creat(const char *, mode_t)  // fcntl.h
int open(const char *, int, ...)  // fcntl.h
ssize_t  write(int __fd, const void * __buf, size_t __nbyte) // unistd.h
ssize_t  read(int, void *, size_t) // unistd.h
int close(int) // unistd.h

发现规律:用文件名进行操作的(creat、open)在 fcntl.h 头文件中,用 fd 进行操作的(read、write、close)在 unistd.h 头文件中。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容