我有一个独特的问题。 尝试访问一个文件有两个进程(P0和P1)。 P0正在向文件写入信息,P1正在读取信息。 在P0完成写入之前P1正在读取的两者之间存在竞争条件。 我考虑过使用Locks,Semaphores等。但是,P1存在于一组代码中,我不允许以任何方式改变它,并且它目前没有实现来支持上面提出的修复。
有没有办法在不触及这两组代码的情况下删除这种竞争条件?
I have a unique problem. There are two processes (P0 and P1) trying to access one file. P0 is writing information to the file and P1 is reading the information. There is a race condition occurring between the two in which P1 is reading before P0 is finished writing. I have considered using Locks, Semaphores, etc. However, P1 exists in a set of code that I am not allowed to alter in any way, and it currently has no implementation to support the above proposed fixes.
Is there any way to remove this race condition without touching both sets of code?
最满意答案
您可以让P0写入文件A并让P1从文件B读取。当P0完成文件写入时,将文件A重命名为文件B.您必须确保重命名操作是原子的。
You can let P0 write to file A and let P1 read from file B. When P0 has completed writing the file, rename file A to file B. You'll have to make sure the rename operation is atomic though.
更多推荐
发布评论