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



Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> writes:

[...]


/* 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
}

This loop is totally useless, because by the time you do the second
lockf call, the file may or may not be locked and it doesn't matter
what another lockf call happening in the past returned.


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



Relevant Pages

  • Re: My way to check if a progam is already running
    ... Unless source) EAGAIN is used wrongly, ... Not a word on the other error codes here, ... I guess there are several ways one can read that man 3 lockf. ... I have tried several varieties of the code, both testing for EACCES and testing for EAGAIN. ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... This loop is totally useless, because by the time you do the second ... what another lockf call happening in the past returned. ... EAGAIN means test again, and I have tested this soft and forced the EAGAIN condition. ... but errno was EAGAIN then what would you do? ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... what another lockf call happening in the past returned. ... EAGAIN means test again, and I have tested this soft and forced the EAGAIN condition. ... The 'real test' is the lock(..., ... *fer crissake* 'the second loop' refers to THE SECOND LOOP, ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... This loop is totally useless, because by the time you do the second ... what another lockf call happening in the past returned. ... EAGAIN means test again, and I have tested this soft and forced the EAGAIN condition. ... The 'real test' is the lock(..., ...
    (comp.os.linux.development.apps)
  • Re: My way to check if a progam is already running
    ... what another lockf call happening in the past returned. ... EAGAIN means test again, and I have tested this soft and forced the EAGAIN condition. ... Unless source) EAGAIN is used wrongly, the loop is correct. ...
    (comp.os.linux.development.apps)