![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 57
| Mutual Exclusion and Running a Single Copy [open,lockf,getpid] I'm writing a daemon and now that must be run in a single process (two instance of process should not be executed at the same time). I tried this code, but it didn't work: Code: int lfp=open(LOCK_FILE, O_RDWR|O_CREAT,0640);
if (lfp<0)
cout << "Could not open lockfile" << endl;
if ( lockf(lfp,F_TLOCK,0)<0 )
exit(0); /* can not lock */
/* first instance continues */
char str[10];
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */
close(lfp);
Thanks |
| hosseinyounesi is offline | |
| | #2 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 462
| What are the errors/problems? |
| valaris is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 57
| There is no compile error, but two or more instance of the program can still be executed at the same time (simultaneously) |
| hosseinyounesi is offline | |
| | #4 |
| Registered User Join Date: Nov 2008
Posts: 75
| Try using the library "unique", instead of reinventing the wheel. |
| MisterIO is online now | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 57
| "unique" library?! What do you mean?! It's a C library or ... ?! |
| hosseinyounesi is offline | |
| | #6 |
| Registered User Join Date: Nov 2008
Posts: 75
| Yes, it's a c library. Debian -- Details of package libunique-1.0-0 in sid |
| MisterIO is online now | |
| | #8 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| A more reliable way is to use a UNIX socket. Two processes cannot both bind to the same UNIX socket name. As part of your startup, open and bind a UNIX socket at some known location (probably somewhere in /var). If you fail to bind, you know another instance is already running.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #9 | |
| Registered User Join Date: Nov 2008
Posts: 75
| Quote:
| |
| MisterIO is online now | |
| | #10 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| I would say it's "as realiable", since advisory-locks held by a process are removed when the descriptor is closed - and all descriptors are closed when the process exits. But sockets provide a comm. channel which may be useful. Which brings up the question: Are you sure that both processes are just acquiring and releasing the lock, one after another? If the code you posted is the code you're running, then that's probably the case. Also, you may want to ftruncate() the file once you acquire the lock to prevent any "left-overs" from previous write() of longer pids. >> It's not gnome specific. Sorry, GTK+, X11 dependent would be more accurate. gg Last edited by Codeplug; 09-22-2009 at 12:38 PM. |
| Codeplug is offline | |
| | #11 | ||
| Registered User Join Date: Jul 2009
Posts: 57
| Quote:
Quote:
| ||
| hosseinyounesi is offline | |
| | #12 |
| Registered User Join Date: Jul 2009
Posts: 57
| Hi, I used flock and it is working now ![]() flock(2) - Linux man page Code: int fd=open(LOCK_FILE, O_RDWR|O_CREAT,0640);
if (fd<0)
cout << "Could not open lockfile" << endl;
int x=flock(fd,LOCK_EX | LOCK_NB);
if( x<0 )
exit(1);
/* first instance continues */
|
| hosseinyounesi is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|