开发者

check if aio_write finished

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.


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 and write 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, or mmap your files and include a mutex or semaphore somewhere in the file.


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 using io_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜