LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Thread Tools
Old 10-13-2004, 05:44 AM   #1
V_Ganesh
LQ Newbie
 
Registered: Sep 2004
Location: India
Distribution: RH8,Fedora Core2,OpenBSD,Solaris
Posts: 4
Thanked: 0
Question 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.
V_Ganesh is offline     Reply With Quote
Old 10-13-2004, 06:43 AM   #2
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Thanked: 0
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
Kumar is offline     Reply With Quote
Old 10-13-2004, 07:13 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273
Thanked: 0
In Windows you would use something known as "atoms" to accomplish that. Maybe some googling will reveal if there's something similar in *nix?
Hivemind is offline     Reply With Quote
Old 10-13-2004, 09:25 AM   #4
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101
Thanked: 0
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
gizmo_thunder is offline     Reply With Quote
Old 10-13-2004, 09:34 AM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273
Thanked: 0
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.
Hivemind is offline     Reply With Quote
Old 10-13-2004, 09:51 AM   #6
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101
Thanked: 0
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.
gizmo_thunder is offline     Reply With Quote
Old 10-13-2004, 09:52 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273
Thanked: 0
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.
Hivemind is offline     Reply With Quote
Old 10-13-2004, 11:35 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Thanked: 0
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.
itsme86 is offline     Reply With Quote
Old 10-13-2004, 11:48 AM   #9
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101
Thanked: 0
Will the lock be lost if the process is killed??
gizmo_thunder is offline     Reply With Quote
Old 10-13-2004, 11:52 AM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Thanked: 0
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.
itsme86 is offline     Reply With Quote
Old 10-13-2004, 12:15 PM   #11
gizmo_thunder
Member
 
Registered: Apr 2004
Posts: 101
Thanked: 0
thanks pal that helped
gizmo_thunder is offline     Reply With Quote
Old 03-31-2005, 10:43 PM   #12
V_Ganesh
LQ Newbie
 
Registered: Sep 2004
Location: India
Distribution: RH8,Fedora Core2,OpenBSD,Solaris
Posts: 4
Thanked: 0

Original Poster
Thanx. I think file lock solution is the gud one and it works.
V_Ganesh is offline     Reply With Quote
Old 12-18-2007, 03:18 PM   #13
jCash
Member
 
Registered: Sep 2006
Posts: 57
Thanked: 0
Quote:
Originally Posted by itsme86 View Post
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?
jCash is offline     Reply With Quote
Old 12-18-2007, 04:52 PM   #14
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.3
Posts: 5,605
Thanked: 111
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.
chrism01 is offline     Reply With Quote
Old 01-23-2008, 10:42 AM   #15
RickyRockRat
LQ Newbie
 
Registered: Apr 2005
Location: boise
Distribution: Red Hat - 6.2,7.3,9.0,FC3,FC4, FC5, Debian-3.1
Posts: 10
Thanked: 0
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.
RickyRockRat is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Sound Instance Problems sadarax Linux - Hardware 4 12-27-2006 11:35 AM
multiple instance of X satinet Linux - General 3 03-10-2005 01:05 PM
Mozilla 1.4 Multiple Instance/Profile problem rryjew Linux - Newbie 2 11-30-2003 01:01 AM
limit program to one instance stevenhasty Slackware 2 10-28-2003 04:38 PM
How to find instance of a program mayankjohri Programming 4 12-09-2002 11:39 PM


All times are GMT -5. The time now is 03:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration