UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » How to check that only istance of process run.

#1 2002-10-11 09:06 AM

bye
Member
Registered: 2002-10-10
Posts: 2

Re: How to check that only istance of process run.

Hello

I need help from guru.

Instance of my programm with given command line should be unique.
In other words such situation is forbidden (pr1 - is my programm):

ps -ef
user1  2860   722  0 10:30:39 pts/18   0:00 /programms/pr1 aaa bbb
user1  2861   722  0 10:30:39 pts/18   0:00 /programms/pr1 aaa bbb
user1  2862   722  0 10:30:39 pts/18   0:00 /home/users/pr1 aaa bbb


But such situation is ok

ps -ef
user1  2860   722  0 10:30:39 pts/18   0:00 /home/users/pr1 aaa bbb
user2  2861   722  0 10:30:39 pts/18   0:00 /home/users/pr1 aaa ccc

What is the best way to orginize such thing on Unix? Semaphor, lock file, checking ps output or something else.

Is there C source code example of such common thing?

regards

Offline

#2 2002-10-11 11:58 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: How to check that only istance of process run.


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#3 2002-10-11 01:07 PM

bye
Member
Registered: 2002-10-10
Posts: 2

Re: How to check that only istance of process run.

Thanks again, Mikhail :)

One more thing.
As far as I understood semaphores are not cleared automatically when master process died. In this case such semaphor may block execution of new process even if it's not needed.
Or my understanding is wrong.

Offline

#4 2002-10-11 01:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: How to check that only istance of process run.

Usually, the typical method of preventing more than one copy of an
app being loaded is to use a run/lock file of some sort...  Eg: on most
systems, in "/var/run/", you'll see a "*.pid" file for various programs
(typically daemons)...  On startup, they'll check to make sure that
file doesn't exist; if it does, they'll read the PID from it, and check to
make sure that PID actually exists; if not, it's a stale lock and can
be reclaimed; if so, they leave it alone, and fail to run...  If they do
run (stale lock file or no pre-existing lock file), they create the file and
log their own PID to it...  Note: you have to be careful of race conditions
where multiple copies might be started at roughly the same time, and
both try to create the run file...  You'll want to use some kind of file
locking to ensure exclusive access to the file, and/or create the file
with O_EXCL to make sure no one else created it between when you
checked and when you try to...  Then, on clean shut down of your
app, you'd typically remove the run file...  But, even on unclean
shut down, if the file hangs around, you should be able to recover
from it, by detecting the stale run file...  (And, it's also easy to intervene
from an admin perspective, and just remove the file if necessary, too...)

Offline

#5 2002-10-11 09:26 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: How to check that only istance of process run.

Actually, if you use "regular" semaphores and the SEM_UNDO flag in the op list and the process exits (crashes, etc.) then it will clear things out so you don't end up with a deadlock situation...

Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

  • Index
  • » Processes
  • » How to check that only istance of process run.

Board footer

Powered by FluxBB