Linux深入内核理解文件IO(内核版本:common-android13-5.15)

文章通过C语言代码和Makefile的使用,探讨了open函数在不同进程中的行为,展示了如何查看和理解内核中fd表的关系,解释了为什么不同PID对应相同fd但指向不同文件的原因,着重分析了内核5.15的机制与早期版本的相似性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要写一个例子:

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

int main(int argc, char **argv) 
{
    int fd = open(argv[1], O_RDONLY);
    if (fd == -1)
    {
        printf("open %s error\n", argv[1]);
    }
    printf("fd = %d\n", fd);
    while (1)
    {
        sleep(100);
    }
    return 0;
}

然后利用上一篇博客:Makefile简单使用及解读韦东山的通用Makefile实例中的Makefile来编译这个C文件。
如下,已经编译好并且分别用编译出来的test打开编译程序本身test和Makefile文件,打印出来的fd都是=3。

virtual-machine:~/Linux_study$ tree open_test/
open_test/
├── Makefile
├── Makefile.build
└── open_test.c
virtual-machine:~/Linux_study/open_test$ make
virtual-machine:~/Linux_study/open_test$ tree .
.
├── built-in.o
├── Makefile
├── Makefile.build
├── open_test.c
├── open_test.o
└── test
virtual-machine
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值