Process Identification

This is a discussion on Process Identification within the C Programming forums, part of the General Programming Boards category; I was wondering if there was a way to stop a program from opening if that same program was already ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Process Identification

    I was wondering if there was a way to stop a program from opening if that same program was already running. I'm thinking you would do this from looking at a process ID for the original .exe but does windows always generate a unique process ID that I could check to see if it existed to stop the duplicate .exe from opening?

    Would this be the correct way of doing this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'd have to go through the list of running processes, and see if there's any process with the same name as your executable. This could be fooled, of course, if a copy of the executable (with a different name) was run.

    Another way to do it (this is what Firefox does) is to create a "lock" file. When your program is run, create a file somewhere to indicate that the program is running; then subsequent instantiations of the program will check to see if this file exists. If it does, you can simply exit.

    Of course, there's a problem with this idea as well: if your program crashes, and doesn't remove the lock file when it quits, you won't be able to start your program! So you could perhaps, when the program is launched and the lock file is detected, say something like "Another instance of this program may already be running, or perhaps it crashed last time it was run. Would you like to continue running the program anyway?"

    Better yet, you could write the process ID of the current program into the lock file. Other programs, upon seeing the lock file, could check if that process is still running; if not, you can conclude it must be a stale lock file. This isn't perfect, of course (if you reboot, for example, the same process ID may be assigned to a different run of the program). You could make it more foolproof by saving, perhaps, the time and date the process was started into the lock file as well.

    There may be a special Windows way of doing this, I'm just talking about what comes to mind -- and I'm a Linux person, so I really wouldn't know.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Thanks for the suggestions! I'll be working on this. I'll let everyone know how it goes.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See the complete MSDN (C++) sample about Mutex (single instance)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequenceing or queueing multiple process
    By sv_joshi_pune in forum Windows Programming
    Replies: 1
    Last Post: 08-14-2009, 09:43 AM
  2. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21