fseek 的实现

#include <stdio.h>

int fseek(FILE *fp, long offset, int whence);

 

fseek 会调用 lseek,lseek 的功能是修改 file table 中 file 数据结构的 offset(内核层),offset(内核层)的值是由 offset(应用层)和 whence 计算而来。lseek system call has nothing to do with the seek operation that positions a disk arm over a particular disk sector。

至此,fseek 的工作就已经完成。

接下来的读、写操作会调用 read 或者 write 系统调用,系统调用把 file table 中的 offset 作为它们的起始字符偏移量。如果该偏移量所对应的文件块已经加载到了内存中,内核会把这部分内容传递到应用层的缓存中,并修改 *fp->ptr,其中 *fp->ptr 是应用层缓存中的指针。如果该偏移量所对应的文件块还没有加载到内存中,那么就会产生一个异常,接下来的工作就由内核的内存管理模块接手,它会计算出由参数 offset(内核层)所指定的字符位于文件的哪个 block(会用到文件的 inode,硬盘的 superblock),然后将该 block 加载到内存中,然后再将控制权交还给 read 或者 write 系统调用。

posted @ 2011-10-21 13:20  only_eVonne  阅读(907)  评论(0编辑  收藏  举报