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?
大家好我想问一个关于flockfile函数的问题我正在阅读描述并且知道它是在线程中使用的。但我正在做分叉这意味着将有不同的进程而不是线程我可以使用flockfile与不同的进程它有什么区别?
Thanks
2 个解决方案
#1
3
The flockfile
function doesn't lock a file but the FILE
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.
flockfile函数不会锁定文件,而是锁定进程用来访问文件的FILE数据结构。所以这是关于进程对文件的地址空间中的表示,而不一定是文件本身。
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 the FILE
s by means of flockfile
.
即使在一个进程中,如果您在同一文件上打开了不同的FILE,也可以同时写入该文件,即使您已通过flockfile锁定了每个FILE。
For locking on the file itself have a look into flock
and lockf
but beware that the rules of their effects for access files through different threads of the same process are complicated.
要锁定文件本身,请查看flock和lockf,但要注意,通过同一进程的不同线程访问文件的效果规则很复杂。
#2
1
These functions can only be used within one process.
这些功能只能在一个进程中使用。
From the POSIX docs:
来自POSIX文档:
In summary, threads sharing stdio streams with other threads can use flockfile() and funlockfile() to cause sequences of I/O performed by a single thread to be kept bundled.
总之,与其他线程共享stdio流的线程可以使用flockfile()和funlockfile()来使单个线程执行的I / O序列保持捆绑。
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.
该页面的所有其余部分都讨论了线程之间的互斥问题。不同的进程将为文件流提供不同的输入/输出缓冲区,这种锁定实际上没有意义/有效。
#1
3
The flockfile
function doesn't lock a file but the FILE
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.
flockfile函数不会锁定文件,而是锁定进程用来访问文件的FILE数据结构。所以这是关于进程对文件的地址空间中的表示,而不一定是文件本身。
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 the FILE
s by means of flockfile
.
即使在一个进程中,如果您在同一文件上打开了不同的FILE,也可以同时写入该文件,即使您已通过flockfile锁定了每个FILE。
For locking on the file itself have a look into flock
and lockf
but beware that the rules of their effects for access files through different threads of the same process are complicated.
要锁定文件本身,请查看flock和lockf,但要注意,通过同一进程的不同线程访问文件的效果规则很复杂。
#2
1
These functions can only be used within one process.
这些功能只能在一个进程中使用。
From the POSIX docs:
来自POSIX文档:
In summary, threads sharing stdio streams with other threads can use flockfile() and funlockfile() to cause sequences of I/O performed by a single thread to be kept bundled.
总之,与其他线程共享stdio流的线程可以使用flockfile()和funlockfile()来使单个线程执行的I / O序列保持捆绑。
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.
该页面的所有其余部分都讨论了线程之间的互斥问题。不同的进程将为文件流提供不同的输入/输出缓冲区,这种锁定实际上没有意义/有效。