views:

870

answers:

5

Normally an application bundle on OS X can only be started once, however by simply copying the bundle the same application can be launched twice. What's the best strategy to detect and stop this possibility?

On Windows this effect can simply be achieved by the application creating a named resource at launch and then exit if the named resource can't be created, indicating that another process is running that has already created the same resource. These resources are released in a reliable way on Windows when the application quits.

The problem I have seen when researching this is that the APIs on OS X keep state in the file system and thus makes the strategy used on windows unreliable, i.e lingering files after an improper exit can falsely indicate that the application is already running.

The APIs I can use to achieve the same effect on OS X are: posix, carbon and boost.

Ideas?

+1  A: 

What about IPC? You could open a socket and negotiate with the other launched instance. You'd have to be careful though, that it works if both apps start at the same time.

I can't provide you with sample code, as I haven't (yet, but I will soon) used it.

Georg
Be careful that you don't break your app's ability to run under multiple users at the same time. An application that quits when another user is already using it is broken.
Peter Hosey
+4  A: 

First off, it's “Mac OS X” or “OS X”. There is no such thing as “OS/X”.

Second, Mac OS X doesn't come with Boost; you would need to bundle it with your application.

Third, most of Carbon is not available in 64-bit. This is a clear signal that those portions of Carbon will go away someday (when Apple abandons 32-bit in its hardware). Sooner or later, you will have to either rewrite your app with Cocoa or abandon the Mac.

Normally an application bundle on OS/X can only be started once, however by simply renaming the bundle the same application can be launched twice.

No it can't. Launching the renamed or moved application will simply activate (bring to the front) the process that was already running; it won't start a new, second process alongside the first one.


There are several ways to tell whether an application is already running. In each case, you do this on launch:

  1. Use Cocoa's NSConnection to register a connection with a single constant name. This will fail if the name is already registered. (You can use Foundation from a Carbon app; it's the Application Kit you have to be careful with.)
  2. Use the Process Manager to scan the process list for processes whose bundle identifier match the one you're looking for. The bundle identifier isn't unchangeable, but it's harder to change than the filename or location.
  3. If you're looking to see when someone runs a second copy of yourself, you can use CFNotificationCenter:

    1. Add yourself as an observer for “com.yourdomain.yourappname.LaunchResponse”.
    2. Post a notification under the name “com.yourdomain.yourappname.LaunchCall”.
    3. Add yourself as an observer for “com.yourdomain.yourappname.LaunchCall”.

    In your observation callback for the Call notification, post the Response notification.
    In your observation callback for the Response notification, exit.

    Thus, when the first process starts, it will Call and get no Response; when the second process starts, it will Call, get a Response from the first process, and exit in deference to the first.

Peter Hosey
I think he meant copy instead of rename. Anyway, you can open a second instance using "open -n TextEdit.app"
Georg
Or launch -m, if you have Nicholas Riley's launch installed.
Peter Hosey
+4  A: 

A low-level solution is to use flock().

Each instance would attempt to lock a file on startup, and if the lock fails then another instance is already running. Flocks are automagically released when your program exits, so no worries about stale locks.

Note that whatever solution you choose, you need to make a conscious decision about what it means to have "multiple instances". Specifically, if multiple users are running your app at the same time, is that ok?

vasi
Thanks, that solution will be fine. The lock files will be per user to not block multiple users on the same machine to start the app at the same time.
Per Ersson
+3  A: 

As has already been mentioned Cocoa applications do not usually allow you to run more than one instance at a time.

In general, a cocoa way to solve this look at launchedApplications in NSWorkspace. This returns an NSArray containing a dictionary for each launched application. You can loop through the array to see if the app you are looking for is already running. I would advise that you use the value with the key NSApplicationBundleIdentifier which will have a value like "com.mycompany.myapp" rather than looking for the name. If you need to find the bundle identifier for an app you can look at its info.plist file in the app package.

Jon Steinmetz
A: 

This is extremely easy in Snow Leopard:

- (void)deduplicateRunningInstances {
    if ([[NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] count] > 1) {
        [[NSAlert alertWithMessageText:[NSString stringWithFormat:@"Another copy of %@ is already running.", [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey]] 
                         defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"This copy will now quit."] runModal];

        [NSApp terminate:nil];
    }
}

See http://blog.jseibert.com/post/1167439217/deduplicating-running-instances-or-how-to-detect-if for more information.

Jeff Seibert