打开文件实际上有什么作用? [英] What does opening a file actually do?

查看:107
本文介绍了打开文件实际上有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有编程语言(至少我使用过)中,必须先打开文件,然后才能对其进行读写.

In all programming languages (that I use at least), you must open a file before you can read or write to it.

但是这个打开操作实际上是做什么的?

But what does this open operation actually do?

典型功能的手册页实际上并没有告诉您任何其他信息,除了它打开文件以供读/写":

Manual pages for typical functions dont actually tell you anything other than it 'opens a file for reading/writing':

http://www.cplusplus.com/reference/cstdio/fopen/

https://docs.python.org/3/library/functions. html#open

很显然,通过使用该函数,您可以知道它牵涉到创建某种有助于访问文件的对象.

Obviously, through usage of the function you can tell it involves creation of some kind of object which facilitates accessing a file.

另一种实现方法是,如果我要实现open函数,那么在Linux上它需要做什么?

Another way of putting this would be, if I were to implement an open function, what would it need to do on Linux?

推荐答案

在几乎每种高级语言中,打开文件的功能都是对应内核系统调用的包装.它可能还会做其他花哨的事情,但是在现代操作系统中,打开文件必须始终通过内核.

In almost every high-level language, the function that opens a file is a wrapper around the corresponding kernel system call. It may do other fancy stuff as well, but in contemporary operating systems, opening a file must always go through the kernel.

这就是为什么fopen库函数或Python的open的参数与open(2)系统调用的参数非常相似的原因.

This is why the arguments of the fopen library function, or Python's open closely resemble the arguments of the open(2) system call.

除了打开文件之外,这些功能通常还设置一个缓冲区,该缓冲区将因此与读/写操作一起使用.此缓冲区的目的是确保无论何时要读取N个字节,相应的库调用都将返回N个字节,而不管对基础系统调用的调用返回的是否较少.

In addition to opening the file, these functions usually set up a buffer that will be consequently used with the read/write operations. The purpose of this buffer is to ensure that whenever you want to read N bytes, the corresponding library call will return N bytes, regardless of whether the calls to the underlying system calls return less.

我实际上对实现自己的功能不感兴趣;只是在了解到底发生了什么...如果您愿意,就超越语言".

I am not actually interested in implementing my own function; just in understanding what the hell is going on...'beyond the language' if you like.

在类似Unix的操作系统中,对open的成功调用将返回文件描述符",该文件描述符在用户进程的上下文中仅为整数.因此,此描述符将传递到与打开的文件进行交互的任何调用,并且在对其调用close后,该描述符将变为无效.

In Unix-like operating systems, a successful call to open returns a "file descriptor" which is merely an integer in the context of the user process. This descriptor is consequently passed to any call that interacts with the opened file, and after calling close on it, the descriptor becomes invalid.

请务必注意,对open的调用就像一个验证点,在此进行各种检查.如果不是所有条件都满足,则调用将通过返回-1而不是描述符来失败,并且错误类型将在errno中指示.基本检查是:

It is important to note that the call to open acts like a validation point at which various checks are made. If not all of the conditions are met, the call fails by returning -1 instead of the descriptor, and the kind of error is indicated in errno. The essential checks are:

  • 文件是否存在;
  • 调用进程是否具有以指定模式打开此文件的特权.通过将文件许可权,所有者ID和组ID与调用过程的相应ID匹配来确定.

在内核的上下文中,进程的文件描述符与物理打开的文件之间必须存在某种映射.映射到描述符的内部数据结构可能还包含另一个处理基于块的设备的缓冲区,或者指向当前读/写位置的内部指针.

In the context of the kernel, there has to be some kind of mapping between the process' file descriptors and the physically opened files. The internal data structure that is mapped to the descriptor may contain yet another buffer that deals with block-based devices, or an internal pointer that points to the current read/write position.

这篇关于打开文件实际上有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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