Detecting Program Instances

The place to discuss creating, porting and modifying Celestia's source code.
Topic author
Rassilon
Posts: 1887
Joined: 29.01.2002
With us: 22 years 9 months
Location: Altair

Detecting Program Instances

Post #1by Rassilon » 24.09.2004, 22:46

Is it possible using C++ and freeglut/glut/OpenGL NO WinMain to detect if the same program is loaded twice? If so how is it done?
I'm trying to teach the cavemen how to play scrabble, its uphill work. The only word they know is Uhh and they dont know how to spell it!

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #2by selden » 25.09.2004, 14:47

A crude but common method is to create a temporary file (called a lock file) that's deleted when the program exits.

That file can contain whatever information you want, like the time the program started and/or info that's specific to the operating system, like the process id number.

Deleting a lock file when the program exits can be tricky sometimes, since your program has to be able to trap all of the ways the user might use to terminate the program. Even so, programs that use them have to provide for cases when lock files accidentally weren't deleted, maybe because the program or computer crashed. This is often handled by asking the user if the program should run anyhow.
Selden

Topic author
Rassilon
Posts: 1887
Joined: 29.01.2002
With us: 22 years 9 months
Location: Altair

Post #3by Rassilon » 25.09.2004, 18:21

Damn why didnt I think of that lol...Thanks selden...Of course if there is a full proof method someone knows about please let me know...
I'm trying to teach the cavemen how to play scrabble, its uphill work. The only word they know is Uhh and they dont know how to spell it!

hjw
Posts: 66
Joined: 02.02.2004
With us: 20 years 9 months
Location: Schwabach/Germany

Post #4by hjw » 26.09.2004, 09:22

Well, on unix systems I use locking functions. The locks are *unlocked*
automatically by the OS as soon as the locking process terminates.
So you don't have to fool around with all the possible ways a process
might terminate (works even if it crashes).
Having no experience at all with windows, I can only suggest looking
for the POSIX "lockf" function.

MfG - Horst

Topic author
Rassilon
Posts: 1887
Joined: 29.01.2002
With us: 22 years 9 months
Location: Altair

Post #5by Rassilon » 27.09.2004, 03:51

Yeah theres a DOS equavilant that locks a file. This would work in cunjunction with seldens method to prevent another program from accessing the same config file...but if I understand your approach you are referring to locking the actual application? I looked up lockf its similar to lock/locking in ANSI C...

So what I would need a full proof method that would access the processes currently running and read the list...In that list if it detects the equivalent of argc[0] which if you program C is the name of the program...So how would I access the processes table or a list of all running progams as a directory dump would?
I'm trying to teach the cavemen how to play scrabble, its uphill work. The only word they know is Uhh and they dont know how to spell it!


Return to “Development”