windows code to detect if a process is still alive

Marius Kruger amanic at gmail.com
Fri Apr 15 20:20:36 UTC 2011


On 14 April 2011 03:26, Martin Pool <mbp at canonical.com> wrote:
> For detection of stale lock files, I want to detect whether a given
> process id on the local machine still corresponds to a live process.
> On unix, kill(pid, 0) will do this.  What's the best way to do this
> from inside Python on Windows?

The h2 database has an interesting way of doing this, but it needs a
poller thread
which may be too much overhead for our purposes.
(It is written in java)

http://h2database.com/html/advanced.html#file_locking_protocols
--
File Locking Method 'File'

The default method for database file locking is the 'File Method'. The
algorithm is:

If the lock file does not exist, it is created (using the atomic
operation File.createNewFile). Then, the process waits a little bit
(20 ms) and checks the file again. If the file was changed during this
time, the operation is aborted. This protects against a race condition
when one process deletes the lock file just after another one create
it, and a third process creates the file again. It does not occur if
there are only two writers.
If the file can be created, a random number is inserted together with
the locking method ('file'). Afterwards, a watchdog thread is started
that checks regularly (every second once by default) if the file was
deleted or modified by another (challenger) thread / process. Whenever
that occurs, the file is overwritten with the old data. The watchdog
thread runs with high priority so that a change to the lock file does
not get through undetected even if the system is very busy. However,
the watchdog thread does use very little resources (CPU time), because
it waits most of the time. Also, the watchdog only reads from the hard
disk and does not write to it.
If the lock file exists and was recently modified, the process waits
for some time (up to two seconds). If it was still changed, an
exception is thrown (database is locked). This is done to eliminate
race conditions with many concurrent writers. Afterwards, the file is
overwritten with a new version (challenge). After that, the thread
waits for 2 seconds. If there is a watchdog thread protecting the
file, he will overwrite the change and this process will fail to lock
the database. However, if there is no watchdog thread, the lock file
will still be as written by this thread. In this case, the file is
deleted and atomically created again. The watchdog thread is started
in this case and the file is locked.
This algorithm is tested with over 100 concurrent threads. In some
cases, when there are many concurrent threads trying to lock the
database, they block each other (meaning the file cannot be locked by
any of them) for some time. However, the file never gets locked by two
threads at the same time. However using that many concurrent threads /
processes is not the common use case. Generally, an application should
throw an error to the user if it cannot open a database, and not try
again in a (fast) loop.


-- 
<>< Marius ><>



More information about the bazaar mailing list