如何C文件I / O操作处于较低水平如何处理? [英] How are C file I/O operations handled at low level?

查看:160
本文介绍了如何C文件I / O操作处于较低水平如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要延长title.I想知道操作系统如何处理像FWRITE,FREAD,和的fopen FCLOSE功能。

To extend the title.I am wondering how the OS handles functions like fwrite,fread,fopen and fclose.

究竟是怎样流?

很抱歉,如果我不太清楚。

Sorry if I was not clear enough.

BTW我使用GNU / Linux操作系统Ubuntu 11.04。

BTW I am using GNU/Linux Ubuntu 11.04.

一个好一点解释什么,我想问问。

A bit better explanation of what I am trying to ask.

我想知道是如何如何读入内存写入到硬盘上的文件,后来又怎能是一个句柄给他们created.Is BIOS这样做,通过驱动程序?

I want to know how are files written to HDD how are read into memory and how can is later a handle to them created.Is BIOS doing that through drivers?

推荐答案

C库需要一个功能像的fopen 并将其转换为正确的OS系统调用。在Linux上是POSIX 打开功能。你可以看到这个定义在一个Linux终端与 2人开。在Windows通话将的CreateFile ,你可以在MSDN文档中看到的。在Windows NT上,该功能依次实际NT内核函数的另译 NtCreateFile

The C library takes a function like fopen and converts that to the proper OS system call. On Linux that is the POSIX open function. You can see the definition for this in a Linux terminal with man 2 open. On Windows the call would be CreateFile which you can see in the MSDN documentation. On Windows NT, that function is in turn another translation of the actual NT kernel function NtCreateFile.

在C库流是存储在一个文件结构的信息的集合。这通常是一个'句柄'到文件的操作系统的想法,分配为缓冲,和当前的读与写的位置的存储区。

A stream in the C library is a collection of information stored in a FILE struct. This is usually a 'handle' to the operating system's idea of the file, an area of memory allocated as a 'buffer', and the current read and write positions.

我只注意到你'集'标记这一点。那么你可能想知道真正的低级别的细节。 这似乎是一个很好的文章。

I just noticed you tagged this with 'assembly'. You might then want to know about the really low level details. This seems like a good article.

现在你已经修改了问题想请教一下的甚至更低的水平的。那么,一旦操作系统得到了命令,打开一个文件,它把该命令的VFS(虚拟文件系统)。那件操作系统查找的文件名,包括所需要的任何目录,并进行必要的访问检查。如果这是在RAM缓存则不需要磁盘访问。如果不是,在VFS发送读请求到特定的文件系统,其可能是EXT4。然后,EXT4文件系统驱动程序将决定该目录位于哪些磁盘块,然后它会发送一个读命令将磁盘设备驱动程序。

Now you've changed the question to ask about even lower levels. Well, once the operating system gets a command to open a file, it passes that command to the VFS (Virtual File System). That piece of the operating system looks up the file name, including any directories needed and does the necessary access checks. If this is in RAM cache then no disk access is needed. If not, the VFS sends a read request to the specific file system which is probably EXT4. Then the EXT4 file system driver will determine in what disk block that directory is located in. It will then send a read command to the disk device driver.

假设磁盘驱动程序是AHCI,它会转换读取块成一系列,将成立一个DMA(直接存储器访问)请求寄存器写入请求。 这看起来像一个很好的来源,一些细节。

Assuming that the disk driver is AHCI, it will convert a request to read a block into a series of register writes that will set up a DMA (Direct Memory Access) request. This looks like a good source for some details.

此时主板上的AHCI控制器接管。它将与硬盘控制器通信中读取数据和写入到DMA存储器位置进行合作。

At that point the AHCI controller on the motherboard takes over. It will communicate with the hard disk controller to cooperate in reading the data and writing into the DMA memory location.

虽然这是在操作系统上要置于保留过程,因此它可以与其它的工作继续进行。硬件正在处理后事,并且不需要CPU的关注。磁盘请求将采取在此期间,CPU可以运行数以百万计的指令(毫秒)。

While this is going on the operating system puts the process on hold so it can continue with other work. The hardware is taking care of things and the CPU isn't required to pay attention. The disk request will take many milliseconds during which the CPU can run millions of instructions.

在请求完成的AHCI控制器发出中断。一个系统的CPU将接收中断,看在它的IDT(中断描述符表),并跳转到机器code在该位置:在中断处理程序

When the request is complete the AHCI controller will send an interrupt. One of the system CPUs will receive the interrupt, look in its IDT (Interrupt Descriptor Table) and jump to the machine code at that location: the interrupt handler.

操作系统中断处理程序会读取一些数据,发现它已被AHCI控制器中断,那么它会跳进AHCI驱动code。 AHCI驱动程序会读取控制器上的寄存器,确定读操作完成,把一个标记纳入其业务队列中,告诉它需要运行OS调度器,的然后返回的。没有什么事情发生在这一点上。

The operating system interrupt handler will read some data, find out that it has been interrupted by the AHCI controller, then it will jump into the AHCI driver code. The AHCI driver will read the registers on the controller, determine that the read is complete, put a marker into its operations queue, tell the OS scheduler that it needs to run, then return. Nothing else happens at this point.

操作系统将注意,需要运行AHCI驱动的队列中。当判定要做到这一点(它可能具有一个实时任务的运行,也可能在此刻被阅读的联网数据包),那么它会读取来自标记为DMA中的存储器块中的数据并复制数据到EXT4文件系统驱动程序。然后,该司机EXT4将数据返回给VFS将放入缓存。该VFS将返回操作系统文件句柄打开系统调用,这将返回到的fopen 库调用,将它放入文件 struct和指针返回到该程序。

The operating system will note that it needs to run the AHCI driver's queue. When it decides to do that (it might have a real-time task running or it might be reading networking packets at the moment) it will then go read the data from the memory block marked for DMA and copy that data to the EXT4 file system driver. That EXT4 driver will then return the data to the VFS which will put it into cache. The VFS will return an operating system file handle to the open system call, which will return that to the fopen library call, which will put that into the FILE struct and return a pointer to that to the program.

这篇关于如何C文件I / O操作处于较低水平如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆