可以使用flockfile函数来处理进程吗
大家好我想问一个关于flockfile函数的问题我正在阅读描述并了解到它是在线程中使用的。但我正在进行分叉,这意味着将会有不同的进程而不是线程我可以将flockfile与不同的进程一起使用,这有什么区别吗?
谢谢
Hello every one I want to ask a question about flockfile function I was reading the description and came to know that it is used in threads. but I am doing forking which means that there will be different process not threads can I use flockfile with different process does it make any difference?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
flockfile
函数不会锁定文件,而是锁定进程用于访问文件的FILE
数据结构。因此,这是关于进程在文件的地址空间中的表示,而不一定是关于文件本身。即使在一个进程中,如果您在同一个文件上打开了不同的 FILE,您也可以同时写入该文件,即使您已经通过某种方式锁定了每个 FILE
flockfile
的。要锁定文件本身,请查看
flock
和lockf
,但请注意,它们通过同一进程的不同线程访问文件的效果规则很复杂。The
flockfile
function doesn't lock a file but theFILE
data structure that a process uses to access a file. So this is about the representation in address space that a process has of the file, not necessarily about the file itself.Even in a process if you have different
FILE
s open on the same file, you can write simultaneously to that file, even if you have locked each of theFILE
s by means offlockfile
.For locking on the file itself have a look into
flock
andlockf
but beware that the rules of their effects for access files through different threads of the same process are complicated.这些函数只能在一个进程内使用。
来自 POSIX 文档:
该页面的所有其余部分都讨论了线程之间的互斥。不同的进程将具有不同的文件流输入/输出缓冲区,这种锁定实际上没有意义/有效。
These functions can only be used within one process.
From the POSIX docs:
All the rest of that page talks about mutual exclusion between threads. Different processes will have different input/output buffers for file streams, this locking wouldn't really make sense/be effective.