From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C724CBC75 for ; Wed, 16 Feb 2005 20:59:52 +0100 (CET) Received: from mail.hybrid.fi ([195.170.158.2]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j1GJxqKG009994 for ; Wed, 16 Feb 2005 20:59:52 +0100 Received: from charon ([195.170.158.21]:55500 "EHLO imap.hybrid.fi") by hybrid.fi with ESMTP id ; Wed, 16 Feb 2005 21:59:43 +0200 Received: from [127.0.0.1] ([195.170.158.34]) (authenticated user janne@hybrid.fi) by imap.hybrid.fi (Kerio MailServer 6.0.2) for caml-list@yquem.inria.fr; Wed, 16 Feb 2005 21:59:41 +0200 Message-ID: <4213A62D.3080803@hybrid.fi> Date: Wed, 16 Feb 2005 21:59:41 +0200 From: Janne Hellsten User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] interprocess mutex References: <421395A4.3030909@hybrid.fi> <421399A9.8040206@barettadeit.com> In-Reply-To: <421399A9.8040206@barettadeit.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit To: unlisted-recipients: ;(no To-header on input) X-Miltered: at nez-perce with ID 4213A638.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 interprocess:01 rdwr:01 lockf:01 printf:01 printf:01 lockf:01 baretta:01 wrote:01 wrote:01 ocaml's:01 interprocess:01 ocaml:01 unix:01 unix:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: 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 >