testlock.C 835 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//
// testlock.C
//
// test the creation and removal of a lock file using the C library
//

#include <stdio.h>    // fopen, fclose, fprintf
#include <sys/stat.h> // stat()
#include <unistd.h>   // fsync()
int main()
{
  // create a lock file "lock.dat"
  FILE *lockfile = fopen("lock.dat","w");
  fprintf(lockfile,"1");
  fclose(lockfile);
  fsync(fileno(lockfile));

  // test for the presence of the lock file
  struct stat statbuf;
  int status;
  do
  {
    // stat returns 0 if the file exists
    status = stat("lock.dat",&statbuf);
    usleep(100000);
  }
  while ( status != 0 );

  // remove the lock file
  remove("lock.dat");
  usleep(100000);

  // test for the absence of the file
  do
  {
    // stat returns 0 if the file exists
    status = stat("lock.dat",&statbuf);
    usleep(100000);
  }
  while ( status == 0 );
}