[英]In Linux, I'm looking for a way for one process to signal another, with blocking
I'm looking for a simple event notification system: 我正在寻找一个简单的事件通知系统:
Process A blocks until it gets notified by... 进程A阻塞,直到得到...的通知
Process B, which triggers Process A. 流程B,触发流程A。
If I was doing this in Win32 I'd likely use event objects ('A' blocks, when 'B' does a SetEvent). 如果我在Win32中执行此操作,则可能会使用事件对象(“ B”执行SetEvent时使用“ A”块)。 I need something pretty quick and dirty (prefer script rather than C code).
我需要一些快速而又肮脏的东西(最好是脚本而不是C代码)。 What sort of things would you suggest?
您会建议什么样的事情? I'm wondering about file advisory locks but it seems messy.
我想知道文件咨询锁,但看起来很乱。 One of the processes has to actively open the file in order to hold a lock.
进程之一必须主动打开文件才能持有锁。
Quick and dirty? 又快又脏?
Then use fifo . 然后使用fifo 。 It is a named pipe.
这是一个命名管道。 The process A read from the fifo's FD with blocking mode.
进程A从具有阻塞模式的fifo FD读取。 The process B writes to it when needed.
进程B在需要时向其写入。
Simple, indeed. 确实如此。
And here is the bash scripting implementation: 这是bash脚本实现:
Program A: 程序A:
#!/bin/bash
mkfifo /tmp/event
while read -n 1 </tmp/event; do
echo "got message";
done
Program B: 程式B:
#!/bin/bash
echo -n "G" >>/tmp/event
First start script A, then in another shell window repeatedly start script B. 首先启动脚本A,然后在另一个外壳程序窗口中重复启动脚本B。
除了fifo之外,您还可以使用信号和kill来进行中断,并使一个进程进入睡眠状态,直到它收到诸如SIGUSR1之类的信号,然后将其解除阻塞(您可以使用条件变量轻松实现此目的而无需轮询)。
Slow and clean? 慢又干净?
Then use (named) semaphores: either POSIX or SysV (not recommended, but possibly slightly more portable). 然后使用(命名)信号灯:POSIX或SysV(不建议使用,但可能更便于携带)。 Process A does a
sem_wait
(or sem_timedwait
) and Process B calls sem_post
. 进程A执行
sem_wait
(或sem_timedwait
),进程B调用sem_post
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.