Ste*_*nov 20
免责声明:我几乎没有资格谈论这个问题.如果有更多知识渊博的人发布,那就太棒了.
fopen()实现方式的细节将在很大程度上取决于操作系统(例如,UNIX也有fopen()).甚至Windows的版本也可能彼此不同.
我会告诉你它是如何工作的,但这基本上是猜测.
fillLevel = 0fillLevel就是尚未刷新的缓冲数据量.如果你打开一个文件而从不关闭它,有些东西会泄漏,是的.FILE结构将泄漏,FS驱动程序的内部数据将泄漏,缓存(如果有)也将泄漏.
但记忆并不是唯一会泄漏的东西.该文件本身会泄漏,因为操作系统会认为它是开放的,当它不是.这可能会成为一个问题,例如在Windows中,在写入模式下打开的文件在关闭之前无法再以写入模式打开.
如果你的应用程序退出而没有关闭某个文件,大多数操作系统将在它之后清理.但这并没有多大用处,因为您的应用程序可能会在退出之前运行很长时间,在此期间,它仍然需要正确关闭所有文件.此外,您不能完全依赖操作系统来清理 - 在C标准中无法保证.
套接字的实现将取决于套接字的类型 - 网络侦听套接字,网络客户端套接字,进程间套接字等.
所有类型的套接字及其可能的实现的完整讨论都不适合这里.
简而言之:
如果不关闭插座,所有这些都会泄漏.
操作系统实现TCP/IP标准,以太网和调度/分配/接受连接所需的其他协议,并通过Berkeley套接字等API将其提供给用户代码.
操作系统会将网络I/O(与网卡通信)委派给网络驱动程序.
使用Windows 10上的VS2017,可以通过callstack查看内部:
ntdll.dll!NtCreateFile()   Unknown
KernelBase.dll!CreateFileInternal() Unknown
KernelBase.dll!CreateFileW()   Unknown
ucrtbased.dll!create_file(const wchar_t * const path, _SECURITY_ATTRIBUTES * const security_attributes, const `anonymous-namespace'::file_options options) Line 234 C++
ucrtbased.dll!_wsopen_nolock(int * punlock_flag, int * pfh, const wchar_t * path, int oflag, int shflag, int pmode, int secure) Line 702    C++
ucrtbased.dll!_sopen_nolock(int * punlock_flag, int * pfh, const char * path, int oflag, int shflag, int pmode, int secure) Line 852    C++
ucrtbased.dll!__crt_char_traits<char>::tsopen_nolock<int * __ptr64,int * __ptr64,char const * __ptr64 const & __ptr64,int const & __ptr64,int,int const & __ptr64,int>(int * && <args_0>, int * && <args_1>, const char * const & <args_2>, const int & <args_3>, int && <args_4>, const int & <args_5>, int && <args_6>) Line 109  C++
ucrtbased.dll!common_sopen_dispatch<char>(const char * const path, const int oflag, const int shflag, const int pmode, int * const pfh, const int secure) Line 172  C++
ucrtbased.dll!_sopen_dispatch(const char * path, int oflag, int shflag, int pmode, int * pfh, int secure) Line 204  C++
ucrtbased.dll!_sopen_s(int * pfh, const char * path, int oflag, int shflag, int pmode) Line 895 C++
ucrtbased.dll!__crt_char_traits<char>::tsopen_s<int * __ptr64,char const * __ptr64 const & __ptr64,int const & __ptr64,int const & __ptr64,int>(int * && <args_0>, const char * const & <args_1>, const int & <args_2>, const int & <args_3>, int && <args_4>) Line 109 C++
ucrtbased.dll!common_openfile<char>(const char * const file_name, const char * const mode, const int share_flag, const __crt_stdio_stream stream) Line 38   C++
ucrtbased.dll!_openfile(const char * file_name, const char * mode, int share_flag, _iobuf * public_stream) Line 67  C++
ucrtbased.dll!__crt_char_traits<char>::open_file<char const * __ptr64 const & __ptr64,char const * __ptr64 const & __ptr64,int const & __ptr64,_iobuf * __ptr64>(const char * const & <args_0>, const char * const & <args_1>, const int & <args_2>, _iobuf * && <args_3>) Line 109 C++
ucrtbased.dll!common_fsopen<char>(const char * const file_name, const char * const mode, const int share_flag) Line 54  C++
ucrtbased.dll!fopen(const char * file, const char * mode) Line 104  C++
大多数代码在:
C:\Program Files (x86)\Windows Kits\10\Source\10.0.17763.0\ucrt\stdio\fopen.cpp
C:\Program Files (x86)\Windows Kits\10\Source\10.0.17763.0\ucrt\stdio\openfile.cpp
C:\Program Files (x86)\Windows Kits\10\Source\10.0.17763.0\ucrt\lowio\open.cpp
在open.cpp的_wsopen_nolock中,有:
// Allocate the CRT file handle.  Note that if a handle is allocated, it is
// locked when it is returned by the allocation function.  It is our caller's
// responsibility to unlock the file handle (we do not unlock it before
// returning).
*pfh = _alloc_osfhnd();
最后,它调用 Windows API CreateFileW,后者调用隐藏 API“NtCreateFile”,其汇编代码为:
NtCreateFile:
00007FFFD81A0120 mov         r10,rcx  
00007FFFD81A0123 mov         eax,55h  
00007FFFD81A0128 test        byte ptr[7FFE0308h],1  
00007FFFD81A0130 jne         NtCreateFile+15h(07FFFD81A0135h)
00007FFFD81A0132 syscall
00007FFFD81A0134 ret
00007FFFD81A0135 int         2Eh  
00007FFFD81A0137 ret
00007FFFD81A0138 nop         dword ptr[rax + rax]
所以最后它执行进入内核代码的系统调用指令。
| 归档时间: | 
 | 
| 查看次数: | 16037 次 | 
| 最近记录: |