正如进程监视器所观察到的,写入 FileStream 的行为很奇怪

发布于 2024-07-08 06:06:19 字数 4471 浏览 9 评论 0原文

我正在使用 FileStream 写入文件,并使用 进程监视器 监视底层系统调用。 我在生产部署中遇到了一些文件锁定问题,因此我正在仔细查看详细信息。

此示例代码:

        using (FileStream fs = new FileStream("c:\\temp\\test.txt", FileMode.Create, FileAccess.Write, FileShare.Read))
        {
            fs.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
            fs.Close();
        }

导致以下系统调用:

9:27:09.4561092 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Generic Read/Write, Disposition: OverwriteIf, Options: Synchronous IO Non-Alert, Non-Directory File, Open No Recall, Attributes: n/a, ShareMode: Read, AllocationSize: 0, OpenResult: Overwritten
9:27:12.2217409 AM  ConsoleApplication1.vshost.exe  2320    WriteFile   C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2219901 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 
9:27:12.2234484 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Generic Read, Write Attributes, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Complete If Oplocked, Random Access, Attributes: N, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
9:27:12.2248664 AM  ConsoleApplication1.vshost.exe  2320    QueryBasicInformationFile   C:\temp\test.txt    SUCCESS CreationTime: 10/29/2008 9:04:14 AM, LastAccessTime: 10/29/2008 9:27:12 AM, LastWriteTime: 10/29/2008 9:27:12 AM, ChangeTime: 10/29/2008 9:27:12 AM, FileAttributes: A
9:27:12.2250075 AM  ConsoleApplication1.vshost.exe  2320    SetBasicInformationFile C:\temp\test.txt    SUCCESS CreationTime: -1, LastAccessTime: -1, LastWriteTime: -1, ChangeTime: -1, FileAttributes: n/a
9:27:12.2255889 AM  ConsoleApplication1.vshost.exe  2320    QueryStandardInformationFile    C:\temp\test.txt    SUCCESS AllocationSize: 8, EndOfFile: 5, NumberOfLinks: 1, DeletePending: False, Directory: False
9:27:12.2257571 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2259825 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2261588 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2265161 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 2
9:27:12.2268237 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2271332 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2272841 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2274779 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2276249 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2278766 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2282577 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2284476 AM  ConsoleApplication1.vshost.exe  2320    QueryFileInternalInformationFile    C:\temp\test.txt    SUCCESS IndexNumber: 0x2d000000016b88
9:27:12.2286183 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 
9:27:12.2288759 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Read Attributes, Write Attributes, Disposition: Open, Options: , Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
9:27:12.2305722 AM  ConsoleApplication1.vshost.exe  2320    QueryBasicInformationFile   C:\temp\test.txt    SUCCESS CreationTime: 10/29/2008 9:04:14 AM, LastAccessTime: 10/29/2008 9:27:12 AM, LastWriteTime: 10/29/2008 9:27:12 AM, ChangeTime: 10/29/2008 9:27:12 AM, FileAttributes: A
9:27:12.2307379 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 

前三个 IO 调用是预期的(创建、写入、关闭),但进程继续读取文件十几次,并重复打开和关闭它。

我现在在 Win XP SP2 和 Win 2003 x64 上观察到了这种行为。 然而,我家里的 Windows Vista 机器的行为符合预期(只是前三行)。

关于为什么在非 Vista 运行时发生这种情况的任何线索?

I'm using FileStream to write to a file, and watching the underlying system calls using Process Monitor. I'm having trouble with some file locking issues in a production deployment, so I'm looking at the details closely.

This sample code:

        using (FileStream fs = new FileStream("c:\\temp\\test.txt", FileMode.Create, FileAccess.Write, FileShare.Read))
        {
            fs.Write(new byte[] { 1, 2, 3, 4, 5 }, 0, 5);
            fs.Close();
        }

Causes the following systems calls:

9:27:09.4561092 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Generic Read/Write, Disposition: OverwriteIf, Options: Synchronous IO Non-Alert, Non-Directory File, Open No Recall, Attributes: n/a, ShareMode: Read, AllocationSize: 0, OpenResult: Overwritten
9:27:12.2217409 AM  ConsoleApplication1.vshost.exe  2320    WriteFile   C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2219901 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 
9:27:12.2234484 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Generic Read, Write Attributes, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Complete If Oplocked, Random Access, Attributes: N, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
9:27:12.2248664 AM  ConsoleApplication1.vshost.exe  2320    QueryBasicInformationFile   C:\temp\test.txt    SUCCESS CreationTime: 10/29/2008 9:04:14 AM, LastAccessTime: 10/29/2008 9:27:12 AM, LastWriteTime: 10/29/2008 9:27:12 AM, ChangeTime: 10/29/2008 9:27:12 AM, FileAttributes: A
9:27:12.2250075 AM  ConsoleApplication1.vshost.exe  2320    SetBasicInformationFile C:\temp\test.txt    SUCCESS CreationTime: -1, LastAccessTime: -1, LastWriteTime: -1, ChangeTime: -1, FileAttributes: n/a
9:27:12.2255889 AM  ConsoleApplication1.vshost.exe  2320    QueryStandardInformationFile    C:\temp\test.txt    SUCCESS AllocationSize: 8, EndOfFile: 5, NumberOfLinks: 1, DeletePending: False, Directory: False
9:27:12.2257571 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2259825 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2261588 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2265161 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 2
9:27:12.2268237 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2271332 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2272841 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2274779 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2276249 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2278766 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2282577 AM  ConsoleApplication1.vshost.exe  2320    ReadFile    C:\temp\test.txt    SUCCESS Offset: 0, Length: 5
9:27:12.2284476 AM  ConsoleApplication1.vshost.exe  2320    QueryFileInternalInformationFile    C:\temp\test.txt    SUCCESS IndexNumber: 0x2d000000016b88
9:27:12.2286183 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 
9:27:12.2288759 AM  ConsoleApplication1.vshost.exe  2320    CreateFile  C:\temp\test.txt    SUCCESS Desired Access: Read Attributes, Write Attributes, Disposition: Open, Options: , Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, OpenResult: Opened
9:27:12.2305722 AM  ConsoleApplication1.vshost.exe  2320    QueryBasicInformationFile   C:\temp\test.txt    SUCCESS CreationTime: 10/29/2008 9:04:14 AM, LastAccessTime: 10/29/2008 9:27:12 AM, LastWriteTime: 10/29/2008 9:27:12 AM, ChangeTime: 10/29/2008 9:27:12 AM, FileAttributes: A
9:27:12.2307379 AM  ConsoleApplication1.vshost.exe  2320    CloseFile   C:\temp\test.txt    SUCCESS 

The first three IO calls are expected (create, write, close), but the the process goes on to read the file a dozen more times and repeatedly open and close it.

I've now observed this behavior on Win XP SP2, and Win 2003 x64. However, my Windows Vista machine at home behaves as expected (just the first three lines).

Any clues on why this is happening on non-Vista runtimes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉鱼一梦 2024-07-15 06:06:19

一种可能性是您安装了病毒扫描程序,将其自身注入到每个正在运行的进程中?

One possibility is that you have a virus scanner installed that injects itself into each running process?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文