检查aio_write是否完成
有什么方法可以检查是否没有 AIO 写入给定文件?我正在我的 Unix 课程上做一个项目,它将是一个上下文无关(基于 UDP)的国际象棋服务器,所有数据都必须存储在文件中。应用程序将是单进程和单线程的(AIO 功能除外)。我的问题是:
1)玩家一发送一些数据来触发对文件的 aio_write 操作,并且该过程进行
2)玩家二请求应从该文件读取的当前棋盘状态,但如果先前的 aio_write 尚未完成,则该文件不是最终文件,因此我不应该读取,而是等待 aio_write 结束。
问题是,因为它是上下文无关的,所以我没有来自 aio_write 调用的 aiocb 结构。
此外,还可能存在来自其他游戏(使用不同文件)的 aio_writes,我不需要关心这些游戏,仅当特定文件当前正在写入时。
Is there any way to check if there are no AIO writes to a given file? Im making an project on my Unix course which will be a context free (based on UDP) chess server and all data has to be stored in files. Application will be single proccess and single threaded (expect for AIO functions). My problem is that:
1)Player One sends some data which triggers aio_write operation to files and the process goes
2)Player Two requests current board state which should be read from that file, but if previous aio_write hasnt finished yet then this file is not final and so I should not read yet but wait for aio_write to end.
Problem is that as it is context free I dont have aiocb structure from aio_write call.
Also there could be aio_writes from other games (which use diffrent files) on which I dont need to care, only if specific file is currently on write.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要使用 AIO? AIO 的目的不是进程之间的同步。这纯粹是为了防止当由于磁盘负载(读)或缓存压力(写)而无法立即满足读或写时,您的进程在内核空间中休眠。对于象棋游戏这样的小事务,AIO 的行为应该与正常的
读
和写
IO 完全相同:所有操作都将立即完成,尽管会稍微多一些开销。如果您的目标是同步文件访问,则可以使用 fcntl 锁定或 mmap 文件并在文件中的某处包含互斥体或信号量。
Why are you using AIO for this? The purpose of AIO is not synchronization between processes. It's purely to prevent your process from sleeping in kernelspace when a read or write can't be satisfied immediately due to disk load (reading) or cache pressure (writing). On such small transactions as a chess game will be working with, AIO should behave exactly the same as normal
read
andwrite
IO: all operations will complete immediately, albeit with moderately more overhead.If your goal is to synchronize file access, you could perhaps use
fcntl
locking, ormmap
your files and include a mutex or semaphore somewhere in the file.它与上下文无关这一事实不应阻止您保留一些状态信息。
您可以保留上下文(我的意思是
io_context
)并重用它(发送读取请求并等待它 -io_getevents
)。io_submit
说它将请求推送到队列中,所以我相信它将保留操作的顺序。或者,(如果您发现 io_submit 不保留顺序)您可以保留足够的状态以了解写入操作正在挂起并使用 io_getevents 等待。 那么阅读就会安全。
我希望我没有误读你的问题。
编辑
看来我确实误读了你的问题。您可能正在谈论 POSIX
aio(7)
。我谈论的是真正的异步 I/O (libaio.h
)。 POSIX aio 通常使用线程来实现(这有点糟糕)。无论哪种方式,如果您可以等待事件(我认为您可以),您仍然可以这样做:保持某种状态来声明写入正在挂起,并且当有人想要读取时等待写入完成。
The fact that it's context-free shouldn't stop you from keeping some state information.
You could keep the context (I mean the
io_context
) and just reuse it (send a read request and wait for it -io_getevents
).io_submit
says it pushes requests in a queue so I believe it will preserve the order of operations.Alternatively, (if you find out that
io_submit
doesn't preserve the order) you could keep enough state to know that a write operation is pending and wait usingio_getevents
. Then reading will be safe.I hope I didn't misread your question.
EDIT
It seems I did misread your question. You are likely talking about POSIX
aio(7)
. I was talking about true asynchronous I/O (libaio.h
). POSIX aio is usually implemented using threads (which kind of sucks).Either way, if you can wait for events (which I think you can) you can still do it: keep some state to declare a write is pending, and when someone wants to read wait for the write to complete.