如何同步两个进程访问NAS上的文件? [英] How can I synchronize two processes accessing a file on a NAS?

查看:322
本文介绍了如何同步两个进程访问NAS上的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的东西:我有两个应用程序,用C ++编写并运行在两个机器与不同的操作系统(一个Linux和一个Windows)。其中一个过程是负责更新NAS(网络连接存储)上的XML文件,而另一个读取此文件。

Here's the thing: I have two applications, written in C++ and running on two machines with different OS (one Linux and one Windows). One of this process is in charge of updating an XML file on a NAS (Network Attached Storage) while the other one reads this file.

是否可以同步这两个进程,以避免在修改文件的同时读取该文件?

Is it possible to synchronize these two processes in order to avoid reading of the file at the same time it's being modified?

推荐答案

您可以在执行写入之前创建的服务器上创建锁定文件,等待,然后在完成时写入和删除。

You could create a lock file on the server that is created before you do a write, wait then write and delete on completion., Have the read process check for the token before reading the file.

读写器:检查写锁文件,创建读锁文件,检查写锁定文件,如果存在删除读取文件和中止。

Reader: Check for write lock file, create read lock file, check for write lock file, if exists delete read file and abort.

作者:检查读锁文件,创建写锁文件,检查读锁文件,如果存在删除写锁定文件和中止。

Writer: Check for read lock file, create write lock file, check for read lock file, if exists delete write lock file and abort.

这将停止你的进程在彼此上践踏,但可能会发生潜在的竞争条件,你可能有两个进程检查,创建然后重新检查同时虽然这不会导致数据在不一致的状态下读取,但会导致读取和写入过程中止您指定的延迟

This will stop your processes trampling on each other but a potential race condition may occur in that the you could potentially have both processes check, create then recheck simultaneously though this will not cause the data to be read in an inconsistent state but will cause both read and write processes to abort for your specified delay

这篇关于如何同步两个进程访问NAS上的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆