共享文件同步(读/写)
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,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())