[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Janne Hellsten <janne@h...> |
| Subject: | Re: [Caml-list] interprocess mutex |
OK, this is what I came up with (uses ExtLib's Std.finally):
---
let create_lock name =
let chnl = open_out name in
output_string chnl "Lockfile - don't delete";
close_out chnl
let with_write_block name f =
if not (Sys.file_exists name) then
create_lock name;
let fd = Unix.openfile name [Unix.O_RDWR] 0o660 in
Std.finally
(fun () -> Unix.close fd)
(fun _ ->
Unix.lockf fd Unix.F_LOCK 0;
f ()) ()
let test =
with_write_block "lock_name" (fun () -> Printf.printf "this code
blocks if other process already acquired lock!")
---
I hope I'm using lockf correctly.
Thanks for the help!
Best regards,
Janne
Alex Baretta wrote:
> Janne Hellsten wrote:
>
>> Hello,
>>
>> I need to sequentialize two processes accessing the same GDBM
>> database (OCaml's dbm library). Since GDBM allows for only one
>> writer at a time, I need a way to block until the previous writer has
>> finished. I think there is a way to do this with the original GDBM
>> library since it claims to handle the file lockings properly -- I
>> could block based on the returned error codes. However, this
>> functionality does not appear to be exposed through the Dbm module.
>>
>> Of course I can implement this blocking myself with an interprocess
>> mutex. But how can I implement such a mutex in OCaml? I couldn't
>> find anything from the standard library that would resemble my problem.
>
>
> Unix.lockf is the way to go.
>
> Alex
>