Re: My way to check if a progam is already running
- From: Michal Nazarewicz <mina86@xxxxxxx>
- Date: Fri, 22 Jun 2007 13:50:11 +0200
Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> writes:
Lockfiles are bad, they show up at times as stale,
and then prevent your program to run.
/* This assumes there is a directory ~/.MyProgram/
struct passwd *userinfo;
char temp[4096];
FILE *fptr;
int a, b;
/* get home directory */
userinfo = getpwuid(getuid() );
home_dir = strsave(userinfo -> pw_dir);
/* test if already a MyProgram running */
sprintf(temp, "pidof MyProgram > %s/.MyProgram/pids", home_dir);
ftr = popen(temp, "w");
pclose(ftr);
sprintf(temp, "%s/.MyProgram/pids", home_dir);
fptr = fopen(temp, "r");
if(fptr) {
a = fscanf(fptr, "%d %d", &b, &b);
fclose(fptr);
if(a == 2) {
fprintf(stderr, "MyProgram: An other MyProgram "
"is already running, aborting.\n");
exit(1);
}
}
Come to think of it it doesn't work. The first instance of the program
is run -- it executes pidof and saves it output to pids file (now there
is only one pid in the file). Then task is switched and another
instance of the program is being run. It executes pidof and saves it
output to pids file (now there are two pids in the file).
What happens next is that both instances read two pids from the file and
both programs think that another instance is already running even though
one of them can safely continue to run. That's why you should not use
temporary file but instead read data from pipe directly.
Still, however, it won't work as in following scenario: The first
instance executes pidof and before pidof does anything task is switched
and another instance executes pidof. Now, there are two instances of
the program and both pidof tools report two pids and both instances
exit.
That's why you should use lock files, ie. you try to lock a file and on
success you're the only instance. On failure you're another instance.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
.
- Follow-Ups:
- Re: My way to check if a progam is already running
- From: Rainer Weikusat
- Re: My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- References:
- My way to check if a progam is already running
- From: Jan Panteltje
- My way to check if a progam is already running
- Prev by Date: stack trace without core
- Next by Date: Re: My way to check if a progam is already running
- Previous by thread: Re: My way to check if a progam is already running
- Next by thread: Re: My way to check if a progam is already running
- Index(es):
Relevant Pages
|
|