Re: threads and file locking doesn't work correct

Thorsten Kukuk (
Tue, 24 Feb 1998 13:18:59 +0100 (MET)


> > I have a big problem with file locking and threads under Linux.
> > Before I start threads, I create a pid file which will be locked with fcntl.
> > After killing the program (killall or Ctrl-C), the lock will not be
> > removed always. When starting the program again, I get the error message,
> > that pid <xxx> owns the lock, but there is no program running with this pid.
> >
> > Any chance to fix this ? I need threads and file locking.
> The problem is that the lock cannot be removed by another thread than the
> one that created it (at least in 2.0.33).

I haven't tried 2.0.33, I use 2.1.85. I create the lock in the main thread,
and I could remove it in another thread (I think this was possible in
2.0.33, too. But I'm not sure).

The problem is, that the lock will not always be removed if the program
exit. Normally, the lock should be given free, if the program exits. But
that doesn't happen always. In the moment I start a thread which acts as
signal handler and wait for signals which would kill a thread, and then
give the lock free. This works in 9 from 10 cases, but not always.

The problem is, after this, you have a locked file which is owned by a non
existent pid, and you couldn't access /proc/locks without core dump
and kernel oops.

Using flock isn't a solution, since the other, single threaded programs
uses fcntl, too.


Thorsten Kukuk
Linux is like a Vorlon.  It is incredibly powerful, gives terse,
cryptic answers and has a lot of things going on in the background.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to