简体   繁体   English

我可以在C中锁定和解锁fifo(命名管道)吗?

[英]Can I lock and unlock a fifo (named pipe) in C?

I have two progress, p1 and p2 , and a named pipe var for ipc between 2 progress. 我有两个进度p1p2 ,还有两个进度之间的ipc命名管道var
I want to lock var 's rw for p2 when p1 writes, and unlock var when p1 finished write. 我想在p1写入时为p2锁定var的rw,在p1完成写入时解锁var

ps: ps:

I using select for nonblocking, so p2 will get readable when p1 put anything to var .Can I let var become readable when p1 finish write? 我使用select进行非阻塞,因此当p1将任何内容添加到varp2将变得可读p1完成写入后我可以让var变得可读吗?

You could use signals (eg SIGUSR1 ). 您可以使用信号(例如SIGUSR1 )。 The writer makes it pipe non-blocking (so it won't block when the pipe becomes full), writes until it can't write anymore, then send the signal to the other process. 编写器使它成为非阻塞管道(因此,当管道已满时它不会阻塞),写入直到无法写入为止,然后将信号发送到另一个进程。 The reading process reads all (from its non-blocking pipe), then sends a signal to the writer who then continues to write. 读取过程读取所有内容(从其非阻塞管道),然后将信号发送给写入器,写入器随后继续写入。

However, this is really not needed. 但是,实际上并不需要。 The writer can just write, and the reader just read. 作家可以写作,读者可以阅读。 If the pipe becomes full the writer will block until it can write more. 如果管道已满,则写入器将阻塞直到可以写入更多内容为止。 And the same for the reader, it will block if there's nothing to read. 对于读者来说,如果没有东西要阅读,它将阻塞。 Then when the writer has written all data, it will simply close its end of the pipe, which the reader will detect with a read call that returns zero bytes read. 然后,当编写器已写入所有数据时,它将仅关闭管道的末端,而读取器将通过read调用检测到该管道,该read返回零个字节的读取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM