[英]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. 我有两个进度
p1
和p2
,还有两个进度之间的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
将任何内容添加到var
时p2
将变得可读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.