共享文件同步(读/写)

发布于 2024-11-15 07:31:52 字数 257 浏览 6 评论 0原文

我用 PHP 代码编写,它将“fopen”一个文件(让我们称之为 TXT)并在其中写入所选的复选框,然后另一个进程(让我们在 C 代码中称之为 PROC1-)需要读取该文件(TXT)。

我需要同步它,我的意思是在 PHP 写入共享资源(TXT)时阻止进程(PROC1)读取。

fopen 会处理它吗?我认为它和 open() 方法之间存在差异。

或者可能使用 pipeline(),但正如我所见,它只运行命令而不打开文件。

再次感谢。

I wrote in PHP code that it will "fopen" a file(lets call it TXT) and write there the chosen checkbox, then later another process(lets call it PROC1- in c code) needs to read the file(TXT).

I need to synchronized it, I mean to block the process(PROC1) from reading, while the PHP writes into the shared resource (TXT).

does fopen take care of it? I think that the diffident between it and the open() method..

or maybe to use pipe(), but as I've seen it runs only commands and not opening a file..

Thanks again in advance..

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

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

发布评论

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

评论(1

别忘他 2024-11-22 07:31:52

不,fopen 不处理这个问题。您可以使用 flock 来同步对文件的访问。

请注意,所有进程在使用文件之前都必须对该文件进行flock(),这取决于平台,但flock()通常是咨询锁,这意味着任何人都可以根据需要绕过该锁(例如,如果他们忘记调用flock) ())

No, fopen does not take care of this. You can use flock to synchronize access to the file.

Note that all processes will have to flock() the file before using it, it depends on the platform, but flock() is usually advisory locks, meaning anyone can just bypass the lock if they so wish (e.g. if they forget to call flock())

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