跨进程异步IO

发布于 2024-08-06 07:34:49 字数 252 浏览 4 评论 0原文

全部:

我正在编写一个日志记录解决方案。可用的日志端点之一是文本文件。假设我想从多个进程写入该文件:我可以共享打开它,并使用命名互斥体来控制对该文件的并发访问(假设所有访问都发生在同一台计算机上)。但后来我开始想知道异步 IO。在一个进程中,我可以使用 BeginWrite 异步发出写入操作。跨进程或跨机器问题怎么办?在这些情况下异步 IO 安全吗?

(假设当我调用 BeginWrite() 时,我传递的缓冲区包含应保存在一个逻辑“记录”中的所有内容)

All:

I am writing a logging solution. One of the available log endpoints is a text file. Suppose I wanted to write to that file from multiple processes: I could open it shared, and use a named mutex to control concurrent access to the file (assuming all access occurred on the same machine). But then I started wondering about async IO. Within a process, I could use BeginWrite to issue my writes asynchronously. What about cross-process, or cross-machine issues? Is async IO safe in those situations?

(assuming that when I call BeginWrite(), the buffer that I pass contains everything that should be kept together in one logical "record")

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

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

发布评论

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

评论(2

聊慰 2024-08-13 07:34:50

在决定异步 IO 解决方案之前,请注意,您认为异步写入的内容通常最终会被同步处理。对于您的日志记录解决方案来说,特别重要的是隐藏在 异步磁盘 中的一点小知识I/O 在 Windows NT、Windows 2000 和 Windows XP 上显示为同步。讨论中隐藏着这样一个要点:

“在 Windows NT 上,对文件的任何扩展其长度的写入操作都将是同步的。”

尽管这里说的是“Windows NT”,但我的经验是,对于 Windows 2000、Windows XP 和 Server 2003 也是如此。我认为他们的意思是“NTFS”而不是“Windows NT”。我还没有在 Vista 或 Server 2008 上测试过它。

本文继续提供针对该限制的可能解决方案。我认为它们有效,但还没有真正尝试过。

我发现的最可靠的解决方案是生成一个执行同步写入的线程。确实有点脖子痛,但很有效。

Before you decide on the async IO solution, note that very often what you think will be an async write ends up being handled synchronously. Particularly important for your logging solution is a little tidbit hidden in Asynchronous Disk I/O Appears as Synchronous on Windows NT, Windows 2000, and Windows XP. Buried there in the discussion is this nugget:

"On Windows NT, any write operation to a file that extends its length will be synchronous."

Although that says "Windows NT," my experience has been that it's true for Windows 2000, Windows XP, and Server 2003 as well. I think they meant "NTFS" rather than "Windows NT". I haven't tested it on Vista or Server 2008.

The article goes on to provide possible solutions to that limitation. I assume they work, but haven't actually tried them.

The most reliable solution I've found is to spawn a thread that does a synchronous write. Kind of a pain in the neck, true, but effective.

假扮的天使 2024-08-13 07:34:50

我不确定你所说的错误是什么意思。如果您使用适当的跨进程互斥锁来保护对文件的访问,那么即使使用异步 I/O,在给定时间也只会有 1 个进程写入该文件。这是假设您保持互斥体锁定直到异步 I/O 完成。

I'm not sure what you mean by wrong. If you are guarding access to the file with an appropriate cross process mutex, then even with asynchronous I/O, you will only ever have 1 process writing to the file at a given time. That is presuming you keep the mutex locked until the async I/O completes.

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