[plt-scheme] file locking, or appending an email to a mbox file

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue May 13 17:55:24 EDT 2003

At Tue, 13 May 2003 15:04:44 -0400, "A. Ozmen" wrote:
> I am trying to write a function to append an email to a mbox
> file. How can I prevent multiple programs, or threads in the same
> program, modifying the same mbox file?

MzScheme should probably provide something like flock(). It doesn't

If you get to write the programs, then one strategy is to use the
existence of a file as a lock.

For example, `(lib "file.ss")' synchronizes access to the preferences
file via a ".LOCK.plt-prefs.ss" file. A process has to create the file
to acquire the lock, and then delete the file to release the lock. It
can only write to the file when it has the lock. This works because the
default mode for 'open-output-file' implements an atomic

There are problems with this approach. For one, a lock file might get
orphaned due to a crash or power failure. Also, if the lock file
exists, there's no good way for a process to wait without polling.

[The first problem would be solved by building on flock(). The second
problem is more difficult in the general case, where a program needs to
block on multiple file locks.]


Posted on the users mailing list.