[英]Mac OS X equivalent of CreateEvent() with named object for interprocess communication?
I'm looking for the simplest or most appropriate way on Mac OS X to simply "signal" or notify one process from another. 我正在寻找Mac OS X上最简单或最合适的方式来简单地“发信号”或通知另一个进程。 Coming from a Windows background this could be achieved using something like the following.
来自Windows背景,可以使用以下内容实现。
In Process A: 在流程A中:
// create named event
hCreatedEvent = CreateEvent(NULL, TRUE, FALSE, "MyUniqueNamedEvent");
// wait for it to be signalled
WaitForSingleObject(hCreatedEvent, INFINITE);
and then in Process B: 然后在流程B中:
// open the existing named event
hOpenedEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, "MyUniqueNamedEvent");
// signal it
SetEvent(hOpenedEvent);
So when the SetEvent
call in Process B is executed, Process A would break out from WaitForSingleObject
and do some work. 因此,当执行进程B中的
SetEvent
调用时,进程A将从WaitForSingleObject
突破并执行一些操作。
I don't need to actually send any data so I've ruled out things like Named Pipes (FIFO's) or sockets etc. as being a bit overkill (I've taken a look at this similar question , but as they need to send data, my question is slightly different). 我不需要实际发送任何数据,所以我排除了命名管道(FIFO)或套接字等等有点矫枉过正(我已经看过这个类似的问题 ,但是因为他们需要发送数据,我的问题略有不同)。 Equally, I won't know the PID of the other process (which is why I need some kind of shared object) so I can't use anything that would require that.
同样,我不会知道其他进程的PID(这就是为什么我需要某种共享对象)所以我不能使用任何需要它的东西。
So far on my shortlist is: 到目前为止,我的候选名单是:
sem_open
, sem_wait
and sem_post
to create/open, wait on and signal the event respectively. sem_open
, sem_wait
和sem_post
创建/打开,等待并分别发出事件信号。 Appears fairly straighforward to use. notify(3)
functions - appears fairly straightforward to use, if not a little clunky to consume notifications. notify(3)
函数 - 使用起来相当简单,如果不是有点笨拙消费通知。 NSDistributedNotificationCenter
or CFNotificationCenter
functions - appears the most "Mac like" way of doing things and fairly straightforward. NSDistributedNotificationCenter
或CFNotificationCenter
功能 - 似乎是最“Mac喜欢”的做事方式,相当简单。 However, my code may needs to run as a dylib and according to this unanswered question , that may not work for me. So, does anyone have any advice/tips/horror stories having used any of the above, or even more appropriate alternatives I haven't thought of to achieve what I want? 那么,有没有人有任何建议/提示/恐怖故事使用上述任何一种,甚至更合适的替代品我没有想到实现我想要的?
So after a bit more digging I finally decided to go down the POSIX semaphores route, which seems to work for me, like so: 所以在经过一番挖掘之后,我终于决定沿着POSIX信号量路线走下去,这似乎对我有用,就像这样:
In Process A (waiting on the semaphore): 在进程A中(等待信号量):
// create semaphore, fail if already exists
sem_t *sem = sem_open("MyUniqueSemaphore", O_CREAT | O_EXCL, 0666, 0);
if (sem != SEM_FAILED)
{
// wait on semaphore
if (sem_wait(sem) == 0)
{
// semaphore signalled!
}
// close our "handle" to the semaphore and remove it from the system
sem_close(sem);
sem_unlink("MyUniqueSemaphore");
}
Then in Process B (signalling the semaphore): 然后在进程B中(发信号通知信号量):
// open the existing semaphore created in process A
sem_t *sem = sem_open("MyUniqueSemaphore", 0);
if (sem != SEM_FAILED)
{
// "signal" it
sem_post(sem);
// close our "handle" to the semaphore
sem_close(sem);
}
The semaphore seems to also be an "auto reset" type (in Windows parlance) so reverts back to unsignalled once it has been signalled. 信号量似乎也是一种“自动重置”类型(用Windows术语),一旦发出信号,就会恢复为无信号状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.