简体   繁体   English

在Mac OS X中检测流程实例

[英]Detecting process instances in Mac OS X

I'm trying to implement a way to detect whether another instance of an application has already been started, and have been tinkering with: 1. Shared memory 2. Named mutexes 3. Named pipes 我正在尝试实现一种方法来检测应用程序的另一个实例是否已经启动,并且一直在进行以下修补:1.共享内存2.命名为互斥对象3.命名为管道

Seems all of these have the flaw that if the "main" application crashes, the "IPC" object will be left hanging making all future instances of the application think that there is another instance active (until a reboot). 似乎所有这些都有一个缺陷,如果“主”应用程序崩溃,则“ IPC”对象将被挂起,使该应用程序的所有将来实例都认为有另一个实例处于活动状态(直到重新启动)。 Ideally I'd like to have an object that is automatically removed when the process is terminated, whether normally or by a crash. 理想情况下,我希望有一个在进程终止时自动删除的对象,无论是正常还是崩溃。

Ideas? 想法?

I've seen this done by using a file and file locking. 我已经看到通过使用文件和文件锁定来完成此操作。 The main app check for the existence of a file in /tmp. 主应用程序检查/ tmp中是否存在文件。 If the file is currently locked then the application is already running. 如果该文件当前已锁定,则该应用程序已在运行。 If the file does not exist or is not locked then it is not running. 如果该文件不存在或未锁定,则该文件未运行。 Depending upon the OS you need to do this in a fashion that is atomic to avoid race conditions. 根据操作系统的不同,您需要以一种原子方式来避免竞争情况。

This way if a program crashes the file is automatically closed by the OS. 这样,如果程序崩溃,操作系统将自动关闭文件。

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

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