* interprocess mutex
@ 2005-02-16 18:49 Janne Hellsten
2005-02-16 19:06 ` [Caml-list] " Alex Baretta
2005-02-16 19:37 ` Frédéric Gava
0 siblings, 2 replies; 4+ messages in thread
From: Janne Hellsten @ 2005-02-16 18:49 UTC (permalink / raw)
To: caml-list
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.
Is it easily doable or do I need to go and start hacking C code? I've
never done it on Unix but I know it's easily done on Windows.
Thanks in advance,
Janne
P.S. I'm using GDBM merely as a cache between process invocations, so
using a more heavyweight DB is out of the question.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] interprocess mutex
2005-02-16 18:49 interprocess mutex Janne Hellsten
@ 2005-02-16 19:06 ` Alex Baretta
2005-02-16 19:59 ` Janne Hellsten
2005-02-16 19:37 ` Frédéric Gava
1 sibling, 1 reply; 4+ messages in thread
From: Alex Baretta @ 2005-02-16 19:06 UTC (permalink / raw)
To: Janne Hellsten; +Cc: caml-list
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
--
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL
tel. +39 02 370 111 55
fax. +39 02 370 111 54
Our technology:
The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>
The FreerP Project
<http://www.freerp.org/>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] interprocess mutex
2005-02-16 18:49 interprocess mutex Janne Hellsten
2005-02-16 19:06 ` [Caml-list] " Alex Baretta
@ 2005-02-16 19:37 ` Frédéric Gava
1 sibling, 0 replies; 4+ messages in thread
From: Frédéric Gava @ 2005-02-16 19:37 UTC (permalink / raw)
To: Janne Hellsten, caml-list
Hi,
you could also used threads. It is easier.
http://caml.inria.fr/ocaml/htmlman/manual038.html
After you could used "Mutex", "Condition" and "Event" to synchronise your
threads...
To synnchronise true "process", you could used "waitpid".
Regards,
Frédéric Gava
> 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.
>
> Is it easily doable or do I need to go and start hacking C code? I've
> never done it on Unix but I know it's easily done on Windows.
>
> Thanks in advance,
> Janne
>
> P.S. I'm using GDBM merely as a cache between process invocations, so
> using a more heavyweight DB is out of the question.
>
>
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] interprocess mutex
2005-02-16 19:06 ` [Caml-list] " Alex Baretta
@ 2005-02-16 19:59 ` Janne Hellsten
0 siblings, 0 replies; 4+ messages in thread
From: Janne Hellsten @ 2005-02-16 19:59 UTC (permalink / raw)
Cc: caml-list
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
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-02-16 19:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-16 18:49 interprocess mutex Janne Hellsten
2005-02-16 19:06 ` [Caml-list] " Alex Baretta
2005-02-16 19:59 ` Janne Hellsten
2005-02-16 19:37 ` Frédéric Gava
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox