究竟怎样fopen()函数,FCLOSE()工作? [英] How exactly does fopen(), fclose() work?

查看:114
本文介绍了究竟怎样fopen()函数,FCLOSE()工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道关于函数的fopen,FCLOSE,插座和关闭套接字。当调用的fopen或打开一个套接字,究竟发生(尤其是内存明智)?

I was just wondering about the functions fopen, fclose, socket and closesocket. When calling fopen or opening a socket, what exactly is happening (especially memory wise)?

在打开的文件/插座没有关闭它们导致内存泄漏?

Can opening files/sockets without closing them cause memory leaks?

第三,如何创建插座?他们是什么样的内存明智?

And third, how are sockets created and what do they look like memory wise?

我亦在读套接字和发送数据interrested在操作系统(Windows)的作用。

I'm also interrested in the role of the operating system (Windows) in reading the sockets and sending the data.

推荐答案

免责声明:我主要是没有资格谈论这个。这将会是巨大的,如果有人更了解张贴过。

Disclaimer: I'm mostly unqualified to talk about this. It'd be great if someone more knowledgeable posted too.

的怎么样东西的fopen()的实现将取决于操作系统上的很多细节(UNIX已fopen()函数也是如此,例如)。即使版本的Windows可以彼此不同了不少。

The details of how things like fopen() are implemented will depend a lot on the operating system (UNIX has fopen() too, for example). Even versions of Windows can differ a lot from each other.

我会给你我它是如何工作的想法,但它基本上是猜测。

I'll give you my idea of how it works, but it's basically speculation.


  • 当调用的fopen分配文件对象在堆上。请注意,在一个文件对象中的数据是未公开的 - 文件是一个不透明的结构,则只能使用指针到文件从code。

  • 文件对象被初始化。例如,像 fillLevel = 0 其中fillLevel是还没有被刷新尚未缓冲的数据量。

  • 系统调用文件系统驱动程序(FS驱动程序)打开该文件,并提供一个处理它,这是在文件结构放在什么地方。

    • 要做到这一点,FS驱动程序计算出相应的请求的路径HDD地址和内部记住这个地址,HDD,所以它可以在以后履行FREAD等电话

      • 的FS驱动器采用一种分度表(存储在HDD)的找出对应于请求路径硬盘地址。这将根据文件系统类型不同很多 - FAT32,NTFS等

      • 的FS驱动程序依赖于硬盘驱动程序来执行实际的读取和写入硬盘。

      • When called, fopen allocates a FILE object on the heap. Note that the data in a FILE object is undocumented - FILE is an opaque struct, you can only use pointers-to-FILE from your code.
      • The FILE object gets initialized. For example, something like fillLevel = 0 where fillLevel is the amount of buffered data that hasn't been flushed yet.
      • A call to the filesystem driver (FS driver) opens the file and provides a handle to it, which is put somewhere in the FILE struct.
        • To do this, the FS driver figures out the HDD address corresponding to the requested path, and internally remembers this HDD address, so it can later fulfill calls to fread etc.
          • The FS driver uses a sort of indexing table (stored on the HDD) to figure out the HDD address corresponding to the requested path. This will differ a lot depending on the filesystem type - FAT32, NTFS and so on.
          • The FS driver relies on the HDD driver to perform the actual reads and writes to the HDD.

          如果你打开​​一个文件,从不关闭它,有些事情会泄露,是的。文件结构将泄漏的FS驱动程序的内部数据会泄漏,高速缓存(如果有的话)就会泄漏了。

          If you open a file and never close it, some things will leak, yes. The FILE struct will leak, the FS driver's internal data will leak, the cache (if any) will leak too.

          但内存是不是会泄漏的嘛。该的文件本身的会泄漏,因为操作系统会认为它是开放的,当它不是。这可以成为例如在Windows中,其中写模式打开的文件不能被写模式再次,直到它被关闭打开的问题。

          But memory is not the only thing that will leak. The file itself will leak, because the OS will think it's open when it's not. This can become a problem for example in Windows, where a file opened in write-mode cannot be opened in write-mode again until it's been closed.

          如果您的应用程序退出而不关闭某些文件,大多数操作系统会后清理。但是,这没有多大用处,因为你的应用程序将很长一段时间退出之前可能运行,在此期间,它仍然需要正确地关闭所有文件。此外,你不能完全依赖操作系统后清理 - 它不是在C标准保证

          If your app exits without closing some file, most OSes will clean up after it. But that's not much use, because your app will probably run for a long time before exiting, and during that time, it will still need to properly close all files. Also, you can't fully rely on the OS to clean up after you - it's not guaranteed in the C Standard.

          一个套接字的实施将取决于插座的类型 - 网络监听套接字,网络客户端套接字,进程间插座等。

          A socket's implementation will depend on the type of socket - network listen socket, network client socket, inter-process socket, etc.

          各类插座及其可能实现的全面讨论将不适合这里。

          A full discussion of all types of sockets and their possible implementations wouldn't fit here.

          在短:


          • 就像一个文件,插座保持在内存中的一些信息,描述了有关其操作的东西,比如远程主机的IP地址。

          • 它也可以在RAM中缓存性能方面的原因

          • 能守住有限的资源的操作系统,如开放的端口,使他们在其他应用无法使用

          所有这些事情会泄漏,如果你不关闭套接字。

          All these things will leak if you don't close the socket.

          操作系统实现了TCP / IP标准,以太网和调度/发送/接受连接,使它们提供给用户code通过像伯克利套接字的API所需的其他协议。

          The OS implements the TCP/IP standard, Ethernet and other protocols needed to schedule/dispatch/accept connections and to make them available to user code via an API like Berkeley Sockets.

          该操作系统将委托网络I / O(与网卡通信)到网络驱动程序。

          The OS will delegate network I/O (communication with the network card) to the network driver.

          这篇关于究竟怎样fopen()函数,FCLOSE()工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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