Re: My way to check if a progam is already running



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--
.



Relevant Pages

  • Re: My way to check if a progam is already running
    ... struct passwd *userinfo; ... "MyProgram: An other MyProgram " ... is run -- it executes pidof and saves it output to pids file (now there ... output to pids file. ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... struct passwd *userinfo; ... is run -- it executes pidof and saves it output to pids file (now there ... output to pids file. ... an other instance running. ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... struct passwd *userinfo; ... is run -- it executes pidof and saves it output to pids file (now there ... output to pids file. ... Normal human response is about 200mS IIRC, click a mouse faster then that is ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... struct passwd *userinfo; ... is run -- it executes pidof and saves it output to pids file (now there ... output to pids file. ... about the same time (in computer terms, ...
    (comp.os.linux.development.apps)