简体   繁体   English

在Linux中,我正在寻找一种方法使一个进程发出另一个信号,并带有阻塞

[英]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: 我正在寻找一个简单的事件通知系统:

  1. Process A blocks until it gets notified by... 进程A阻塞,直到得到...的通知

  2. 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.

相关问题 我可以使用“ sendmsg / recvmsg”使用非阻塞套接字将FD从一个进程发送到另一个进程吗? - Can I use non-blocking sockets to send FDs from one process to another using “sendmsg/recvmsg”? 在Linux中从一个进程到另一个进程的地址映射 - address mapping from one process to another in Linux 有什么方法可以将信号从另一个进程发送到线程吗? - Is there any way to send signal to a thread from another process? 使用信号和进程进行 Linux C 编程 - Linux C programming with signal & process 有什么方法可以将子linux进程转移到另一个进程? - There is any way to transfer a child linux process to another process? 寻找一种检测我是否在虚拟机(Linux)中运行的好方法 - Looking for a nice way to detect if I am running in a Virtual Machine (Linux) 如何在 Linux 中判断哪个进程向我的进程发送了信号 - How can I tell in Linux which process sent my process a signal 是否有类似WaitNamedPipe的功能或在C ++ / linux上实现此功能的方法? (因此该过程不会无限期地阻塞管道) - Is there a function like WaitNamedPipe or a way to realize this on C++/linux? (so the process is not blocking on the pipe for infinite time) 寻找一种将丢失的文件从一个目录复制到另一个目录的方法 - Looking for a way to copy missing files from one directory to another 我可以将文件描述符共享给Linux上的另一个进程,还是它们是进程的本地进程? - Can I share a file descriptor to another process on linux or are they local to the process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM