How would i ensure only one copy of a process is running at a time

This is a discussion on How would i ensure only one copy of a process is running at a time within the C Programming forums, part of the General Programming Boards category; I have created a process which I would like to make sure that it is the only copy of itself ...

I have created a process which I would like to make sure that it is the only copy of itself running at any given time. What would I need to do to ensure this?

On Windows you can register a window class (even if you don't actually use a window). When your process starts, it can check whether this window class is already registered. If it is, you know the process is already running.

On UNIX, the standard method is a .pid file placed in /var/run. When the program starts you check if this file is present -- if not, you create it. If so, you exit. You have to do this atomically, a la lock file (O_CREAT | O_EXCL)

OK
Be the first to answer (by questioning) i would like to ask :
When i run a program, how can two copies of it, run simultaneously ?
The only way i can think is to execute them in the same time. Is that the point ?

ch4: I have a server which I am running and I would like to ensure that there is only one copy of my server running at a time.

brewbuck: Thank you, I just did a bit of research on what you said and turned up:

Code:

lfp=open("exampled.lock",O_RDWR|O_CREAT,0640);
if (lfp<0) exit(1); /* can not open */
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
/* only first instance continues */
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */

My only question now is do I want this at the start of my main() in the server?

I understood your question and wanted to provide you with the answer, but you gave the answer yourself...
But I don't understand your second question. Do you understand the code? In essence, it will run and exit the process if the process ran before (without terminating).

ch4: I have a server which I am running and I would like to ensure that there is only one copy of my server running at a time.

brewbuck: Thank you, I just did a bit of research on what you said and turned up:

Code:

lfp=open("exampled.lock",O_RDWR|O_CREAT,0640);
if (lfp<0) exit(1); /* can not open */
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
/* only first instance continues */
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */

My only question now is do I want this at the start of my main() in the server?

Yes, you'd do this very close to the start of main() -- at least, prior to doing anything that might be messed up by multiple instances of the program.

One thing that example leaves out is robust exit handling. If your program exits, you need to remove the pid file. This is usually done by registering an atexit() handler. This way, you don't have to remember to remove the lock file at every single call to exit(), if you have more than one of those. It's bad form to call exit() all over the place, anyway.

EDIT: Also, that example uses open() followed by lockf(). The separate lockf() is unnecessary if you use the O_EXCL flag to open(). The purpose of both methods is to prevent the situation where two copies of the program start at exactly the same time and create the lock file at the same time, mistakenly thinking they are the only instance.