前言 大家好吖,欢迎来到 YY 滴 系列 ,热烈欢迎! 本章主要内容面向接触过C++的老铁 主要内容含:
如下图:进程结构体task_struct有一个文件指针指向files_struct结构体,files_struct结构体经过系统调用open后生成file结构体:
files_struct结构体是Linux内核中定义的一个结构体,用于表示进程级别的文件描述符表。在Linux系统中,每个进程都有一个与之关联的文件描述符表,该表记录了该进程当前打开的所有文件的信息。以下是files_struct结构体的一些关键字段:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("myfile", O_RDONLY);
if(fd < 0){
perror("open");
return 1;
}
printf("fd: %d\n", fd);
close(fd);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
close(0);
//close(2);
int fd = open("myfile", O_RDONLY);
if(fd < 0){
perror("open");
return 1;
}
printf("fd: %d\n", fd);
close(fd);
return 0;
}