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 ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

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

    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?

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    In reality i can't see what you mean.
    You've created a process which has child processes or just one and it;s running alone ?
    Or what ? Would you mind explain ?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by NuNn View Post
    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)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    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 ?

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    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?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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).

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by NuNn View Post
    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.
    Last edited by brewbuck; 03-14-2009 at 03:05 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to know the number of the process copy ?
    By diskdisk in forum Windows Programming
    Replies: 20
    Last Post: 06-19-2009, 01:38 PM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 12:08 PM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  4. running time
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2002, 09:35 AM
  5. One process at a time
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-06-2001, 01:44 AM

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