Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
 |
10-13-2004, 05:44 AM
|
#1
|
|
Newbie
Registered: Sep 2004
Location: India
Distribution: RH8,Fedora Core2,OpenBSD,Solaris
Posts: 4
|
restricting multiple instance of a program
[ Log in to get rid of this advertisement]
Hi,
I have written one server program(daemon) in C/Fedora core II. I want to resrict the multiple instance of the same server program running in one machine? How to do that? Thanx in advance.
--Ganesh.
|
|
|
|
10-13-2004, 06:43 AM
|
#2
|
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 102
|
Well..........I can think of 2 options -
1. Put some kind of flag in the filesystem. OR
2. Scan thro the /proc directory to check if another instance is alive or not
|
|
|
|
10-13-2004, 07:13 AM
|
#3
|
|
Member
Registered: Sep 2004
Posts: 271
|
In Windows you would use something known as "atoms" to accomplish that. Maybe some googling will reveal if there's something similar in *nix?
|
|
|
|
10-13-2004, 09:25 AM
|
#4
|
|
Member
Registered: Apr 2004
Posts: 101
|
there is this file called cmdline in every dir of /proc
which tells you the exact location of the executable and path used
so you could use that to identify your process
man proc for details 
|
|
|
|
10-13-2004, 09:34 AM
|
#5
|
|
Member
Registered: Sep 2004
Posts: 271
|
Well, maybe it's good enough for the OP but that won't do if the you have an exakt copy of the program somewhere, only with a different name.
|
|
|
|
10-13-2004, 09:51 AM
|
#6
|
|
Member
Registered: Apr 2004
Posts: 101
|
you could create some file in /tmp dir when ever the daemon is started
which could be checked when the process is starting.. so that even
if the exact same copy with a different name is executing you can stop.
Well but then there might be a situation when the process is killed and
the file may be left without deletion.. this might again cause problem. 
|
|
|
|
10-13-2004, 09:52 AM
|
#7
|
|
Member
Registered: Sep 2004
Posts: 271
|
Maybe he can use a global mutex? That would be the neatest solution but I don't know if such a thing exists for *nix, but I am looking into it now.
|
|
|
|
10-13-2004, 11:35 AM
|
#8
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
|
This seems to work pretty well:
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int fdlock;
int get_lock(void)
{
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 1;
if((fdlock = open("oneproc.lock", O_WRONLY|O_CREAT, 0666)) == -1)
return 0;
if(fcntl(fdlock, F_SETLK, &fl) == -1)
return 0;
return 1;
}
int main(void)
{
if(!get_lock())
{
fputs("Process already running!\n", stderr);
return 1;
}
getchar();
return 0;
}
It leaves a file sitting there (you probably want to put it in /tmp), but it only complains when another process actually has a lock on the file...just the fact that the file exists isn't enough to make it complain.
|
|
|
|
10-13-2004, 11:48 AM
|
#9
|
|
Member
Registered: Apr 2004
Posts: 101
|
Will the lock be lost if the process is killed??
|
|
|
|
10-13-2004, 11:52 AM
|
#10
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
|
Quote:
Originally posted by gizmo_thunder
Will the lock be lost if the process is killed??
|
Yes, I tested that. The lock only exists as long as the program is running, which is what I thought you wanted.
|
|
|
|
10-13-2004, 12:15 PM
|
#11
|
|
Member
Registered: Apr 2004
Posts: 101
|
thanks pal that helped 
|
|
|
|
03-31-2005, 10:43 PM
|
#12
|
|
Newbie
Registered: Sep 2004
Location: India
Distribution: RH8,Fedora Core2,OpenBSD,Solaris
Posts: 4
|
Thanx. I think file lock solution is the gud one and it works.
|
|
|
|
12-18-2007, 03:18 PM
|
#13
|
|
Member
Registered: Sep 2006
Posts: 53
|
Quote:
Originally Posted by itsme86
This seems to work pretty well:
It leaves a file sitting there (you probably want to put it in /tmp), but it only complains when another process actually has a lock on the file...just the fact that the file exists isn't enough to make it complain.
|
I have created the oneproc.lock in my temp directory. For the lock file oneproc.lock do I need to give permission to the file as 666?
|
|
|
|
12-18-2007, 04:52 PM
|
#14
|
|
Senior Member
Registered: Aug 2004
Location: Brisbane
Distribution: Fedora 8
Posts: 3,321
|
Depends who is running the prog. Remember perms are ugo (user/group/other). The only 'people' who need access to the file are those that are likely to be using the prog...
If it's always only run by users in one group, then 'other' aka world needs no perms on the file.
|
|
|
|
01-23-2008, 10:42 AM
|
#15
|
|
LQ Newbie
Registered: Apr 2005
Location: boise
Distribution: Red Hat - 6.2,7.3,9.0,FC3,FC4, FC5, Debian-3.1
Posts: 10
|
Linux can use semaphores as well. There is the old stye using semctl and the new POSIX style using sem_open. The old style is available on most all Linux boxes, but the new POSIX style may not be available.
For the old style, you use a ftok to get a key, then use the key to get a semaphore, using IPC_EXCL|IPC_CREAT. These semaphores must be explicitly removed with semctl and the flag IPC_RMID.
If you use the file locking above and want to be able to write to more than one byte of the file, set fl.l_len = 0; This is a special case and will lock the whole file.
|
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:35 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
LQ Podcast
LQ Radio
|
|