Re: My way to check if a progam is already running
- From: Jan Panteltje <pNaonStpealmtje@xxxxxxxxx>
- Date: Sun, 24 Jun 2007 19:35:03 GMT
On a sunny day (Sun, 24 Jun 2007 17:48:50 +0200) it happened Michal Nazarewicz
<mina86@xxxxxxx> wrote in <87645dl5el.fsf@xxxxxxxxxxxxxxxx>:
I don't think you need two loops. From what I understood EAGAIN means
that file is already locked (or otherwise cannot be locked):
This is actually better, as while(1) had no timeout, now it has.
int a, i;
char temp[WHAT_SHALL_WE USE];
struct passwd *userinfo;
FILE *lockfile;
/* get user info */
userinfo = getpwuid(getuid() );
/* create a lockfile if it did not exist */
sprintf(temp, "%s/.MyProgram/lockfile", userinfo -> pw_dir);
lockfile = fopen(temp, "w");
if(! lockfile)
{
fprintf(stderr, "MyProgram: could not create lockfile %s aborting.\n", temp);
exit(1);
}
/* test if file is locked */
i = 0;
while(1)
{
a = lockf(fileno(lockfile), F_TEST, 0);
if(a != -1) break;
if(errno != EAGAIN)
{
fprintf(stderr, "MyProgram: An other MyProgram is already running, aborting.\n");
exit(1);
}
i++;
if(i == 1000) // 10 S
{
fprintf(stderr, "MyProgram: timeout EAGAIN waiting for lockf() F_TEST, aborting.\n");
exit(1);
}
usleep(10000); // 10 ms
}
/* lock the file */
i = 0;
while(1)
{
a = lockf(fileno(lockfile), F_TLOCK, 0);
if(a != -1) break;
if(errno != EAGAIN)
{
perror(" MyProgram: could not lock lockfile because: ");
exit(1);
}
i++;
if(i == 1000) // 10 s
{
fprintf(stderr, "MyProgram: timeout EAGAIN waiting for lockf() F_TLOCK, aborting.\n");
exit(1);
}
usleep(10000); // 10 ms
}
/* file stays open, else lock is lost */
.
- 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
- References:
- My way to check if a progam is already running
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- 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
- From: Michal Nazarewicz
- 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
- From: Michal Nazarewicz
- 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
- From: Michal Nazarewicz
- Re: My way to check if a progam is already running
- From: panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- 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
- From: Jan Panteltje
- Re: My way to check if a progam is already running
- From: Michal Nazarewicz
- My way to check if a progam is already running
- Prev by Date: Re: remote control, firefox and document
- Next by Date: Re: collecting "oobs" message for user applications (WAS: Re: stack trace without core)
- 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
|