Does this lockfile method work?

Nick Sayer mrapple at quack.sac.ca.us
Fri Sep 28 03:36:10 AEST 1990


void get_lock()
{
  sprintf(tmpname,"/tmp/t_%d",getpid());
  fp=fopen(tmpname,"w");
  fprintf(fp,"%d\n",getpid());
  fclose(fp);
  while(link(tmpname,"/tmp/LOCK")!=0) sleep(30);
  unlink(tmpname);
}

void release_lock()
{
  unlink("/tmp/LOCK");
}

Presuming for the moment that the possibility of an infinite wait
within the while loop won't occur, and release_lock() won't be
called unless the lock was previously aquired by this process,
does anyone see any exclusivity problems with this code?
In particular, is there any conceivable circumstance where
the link() function fails when multiple processes are trying
to link into the same destination? In this context, "fails" means
anything other than one process completing the link() successfully
and all the others getting EEXISTS.

-- 
Nick Sayer               |  Disclaimer:
N6QQQ [44.2.1.17 soon]   |    "Just because you're reading my post doesn't
mrapple at quack.sac.ca.us  |     mean we're gonna take long showers together."
209-952-5347 (Telebit)   |                      -- Gunnery Sgt. Thomas Highway



More information about the Comp.unix.questions mailing list