caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Best way to synchronize OS processes?
@ 2004-05-11 18:05 Ranjan Bagchi
  2004-05-11 18:09 ` Shawn Wagner
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Ranjan Bagchi @ 2004-05-11 18:05 UTC (permalink / raw)
  To: caml-list

Hi --

I'm writing some code which will end up executing concurrently on 
several OS processes.  I'd like to serialize access to some specific OS 
resources (for instance, writing to a single file).  The Unix module 
doesn't appear to offer anything like a critical section or an OS 
mutex.  Is there a preferred way to do this? 

Thanks,

Ranjan

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:05 [Caml-list] Best way to synchronize OS processes? Ranjan Bagchi
@ 2004-05-11 18:09 ` Shawn Wagner
  2004-05-11 18:24   ` Ranjan Bagchi
  2004-05-12 11:27   ` Alex Baretta
  2004-05-11 18:10 ` Richard Jones
  2004-05-12 16:52 ` Shawn Wagner
  2 siblings, 2 replies; 11+ messages in thread
From: Shawn Wagner @ 2004-05-11 18:09 UTC (permalink / raw)
  To: caml-list

On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
> Hi --
> 
> I'm writing some code which will end up executing concurrently on 
> several OS processes.  I'd like to serialize access to some specific OS 
> resources (for instance, writing to a single file).  The Unix module 
> doesn't appear to offer anything like a critical section or an OS 
> mutex.  Is there a preferred way to do this?

File locking with Unix.lockf?


-- 
Shawn Wagner
shawnw@speakeasy.org

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:05 [Caml-list] Best way to synchronize OS processes? Ranjan Bagchi
  2004-05-11 18:09 ` Shawn Wagner
@ 2004-05-11 18:10 ` Richard Jones
  2004-05-12 16:52 ` Shawn Wagner
  2 siblings, 0 replies; 11+ messages in thread
From: Richard Jones @ 2004-05-11 18:10 UTC (permalink / raw)
  Cc: caml-list

On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
> Hi --
> 
> I'm writing some code which will end up executing concurrently on 
> several OS processes.  I'd like to serialize access to some specific OS 
> resources (for instance, writing to a single file).  The Unix module 
> doesn't appear to offer anything like a critical section or an OS 
> mutex.  Is there a preferred way to do this? 

What about Unix.lockf?

Rich.

val lockf : file_descr -> lock_command -> int -> unit
(** [lockf fd cmd size] puts a lock on a region of the file opened
   as [fd]. The region starts at the current read/write position for
   [fd] (as set by {!Unix.lseek}), and extends [size] bytes forward if
   [size] is positive, [size] bytes backwards if [size] is negative,
   or to the end of the file if [size] is zero.
   A write lock (set with [F_LOCK] or [F_TLOCK]) prevents any other
   process from acquiring a read or write lock on the region.
   A read lock (set with [F_RLOCK] or [F_TRLOCK]) prevents any other
   process from acquiring a write lock on the region, but lets
   other processes acquire read locks on it. *)

-- 
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
If I have not seen as far as others, it is because I have been
standing in the footprints of giants.  -- from Usenet

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:09 ` Shawn Wagner
@ 2004-05-11 18:24   ` Ranjan Bagchi
  2004-05-12  7:01     ` Jean-Christophe Filliatre
  2004-05-12 16:35     ` Ranjan Bagchi
  2004-05-12 11:27   ` Alex Baretta
  1 sibling, 2 replies; 11+ messages in thread
From: Ranjan Bagchi @ 2004-05-11 18:24 UTC (permalink / raw)
  To: Shawn Wagner; +Cc: caml-list

Cool - I was playing with that call today, although I'm getting 
occasional EDEADLK exceptions thrown though.  Is there an example of 
correct use? 

What I'm doing (code isn't handy right now) is before writing to the 
file, I get a write lock, write my bit, and then unlock the region.   
Reading bits try to get a Read lock, and then unlock when they're done.

I'm using | in_channel_of_descr| and out_channel_of_descr to do the i/o 
-- is this a problem?

Ranjan


Shawn Wagner wrote:

>On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
>  
>
>>Hi --
>>
>>I'm writing some code which will end up executing concurrently on 
>>several OS processes.  I'd like to serialize access to some specific OS 
>>resources (for instance, writing to a single file).  The Unix module 
>>doesn't appear to offer anything like a critical section or an OS 
>>mutex.  Is there a preferred way to do this?
>>    
>>
>
>File locking with Unix.lockf?
>
>
>  
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:24   ` Ranjan Bagchi
@ 2004-05-12  7:01     ` Jean-Christophe Filliatre
  2004-05-12 16:35     ` Ranjan Bagchi
  1 sibling, 0 replies; 11+ messages in thread
From: Jean-Christophe Filliatre @ 2004-05-12  7:01 UTC (permalink / raw)
  To: Ranjan Bagchi; +Cc: Shawn Wagner, caml-list


Ranjan Bagchi writes:
 > Cool - I was playing with that call today, although I'm getting 
 > occasional EDEADLK exceptions thrown though.  Is there an example of 
 > correct use? 

I use it like this (bd_lock is the lock file) :

======================================================================
...

(* START OF CRITICAL SECTION *)
let fd_lock =
  try
  let fd = openfile bd_lock [O_WRONLY; O_CREAT] 0o666 in
  lockf fd F_LOCK 0;
  fd
  with e -> printf "%s" (Printexc.to_string e); exit 0

...CRITICAL SECTION GOES HERE...

let _ = lockf fd_lock F_ULOCK 0; close fd_lock
(* END OF CRITICAL SECTION *)

...
======================================================================

Hope this helps,
-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:09 ` Shawn Wagner
  2004-05-11 18:24   ` Ranjan Bagchi
@ 2004-05-12 11:27   ` Alex Baretta
  2004-05-12 11:46     ` Ville-Pertti Keinonen
  2004-05-12 12:53     ` Gerd Stolpmann
  1 sibling, 2 replies; 11+ messages in thread
From: Alex Baretta @ 2004-05-12 11:27 UTC (permalink / raw)
  To: Shawn Wagner; +Cc: caml-list, ranjan.bagchi

Shawn Wagner wrote:
> On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
> 
>>Hi --
>>
>>I'm writing some code which will end up executing concurrently on 
>>several OS processes.  I'd like to serialize access to some specific OS 
>>resources (for instance, writing to a single file).  The Unix module 
>>doesn't appear to offer anything like a critical section or an OS 
>>mutex.  Is there a preferred way to do this?
> 
> 
> File locking with Unix.lockf?

I disagree. I faced the same problem and found that it cannot be easily 
solved with lockf. Unix.lockf locks specific regions in a file, whereas 
Ranjan seems to need the equivalent of a mutex, which should be 
implemented as a lock over the entire file. This is done by the flock 
primitive in Posix systems. Unluckily, flock has not been included in 
the Unix module.

I have written my own Flock module, which might be useful to Rajan. It 
supports locking Pervasive channels as well as locking the underlying 
file descriptors. I have sent it to Caml team as a contribution to the 
Unix module, but apparently it was not accepted.

I'm willing to send it to anyone requesting it. Note that it is released 
under the GPL--not the LGPL--so licensing issues might arise.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-12 11:27   ` Alex Baretta
@ 2004-05-12 11:46     ` Ville-Pertti Keinonen
  2004-05-12 13:16       ` Olivier Andrieu
  2004-05-12 12:53     ` Gerd Stolpmann
  1 sibling, 1 reply; 11+ messages in thread
From: Ville-Pertti Keinonen @ 2004-05-12 11:46 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Shawn Wagner, caml-list, ranjan.bagchi


On May 12, 2004, at 2:27 PM, Alex Baretta wrote:

> I disagree. I faced the same problem and found that it cannot be 
> easily solved with lockf. Unix.lockf locks specific regions in a file, 
> whereas Ranjan seems to need the equivalent of a mutex, which should 
> be implemented as a lock over the entire file. This is done by the 
> flock primitive in Posix systems. Unluckily, flock has not been 
> included in the Unix module.

Actually flock is non-POSIX (it's BSD) and is missing from some 
reasonably modern systems.

It's entirely possible to lock the entire file using the fcntl used by 
Unix.lockf, making it equivalent to flock.  The problem is that the 
OCaml Unix module doesn't allow the l_whence parameter to be set, which 
is a serious omission.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-12 11:27   ` Alex Baretta
  2004-05-12 11:46     ` Ville-Pertti Keinonen
@ 2004-05-12 12:53     ` Gerd Stolpmann
  1 sibling, 0 replies; 11+ messages in thread
From: Gerd Stolpmann @ 2004-05-12 12:53 UTC (permalink / raw)
  To: Alex Baretta; +Cc: Shawn Wagner, caml-list, ranjan.bagchi

Am Mit, 2004-05-12 um 13.27 schrieb Alex Baretta:
> Shawn Wagner wrote:
> > On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
> > 
> >>Hi --
> >>
> >>I'm writing some code which will end up executing concurrently on 
> >>several OS processes.  I'd like to serialize access to some specific OS 
> >>resources (for instance, writing to a single file).  The Unix module 
> >>doesn't appear to offer anything like a critical section or an OS 
> >>mutex.  Is there a preferred way to do this?
> > 
> > 
> > File locking with Unix.lockf?
> 
> I disagree. I faced the same problem and found that it cannot be easily 
> solved with lockf. Unix.lockf locks specific regions in a file, whereas 
> Ranjan seems to need the equivalent of a mutex, which should be 
> implemented as a lock over the entire file. This is done by the flock 
> primitive in Posix systems. Unluckily, flock has not been included in 
> the Unix module.

And this is good. flock is not as portable as lockf. flock usually does
not work over NFS, whereas lockf works. Normally, you need flock only to
synchronize with third-party BSD programs such as sendmail.

lockf does not lock regions of a file. It just locks abstract
byteranges, whether you interpret them as file regions is up to you.
That means, you can lock byteranges that do not exist in the file. If
you like, you can view an empty lockf-locked file as 2^32 potential
mutexes (or whatever the maximum file size is).

Gerd 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-12 11:46     ` Ville-Pertti Keinonen
@ 2004-05-12 13:16       ` Olivier Andrieu
  0 siblings, 0 replies; 11+ messages in thread
From: Olivier Andrieu @ 2004-05-12 13:16 UTC (permalink / raw)
  To: caml-list; +Cc: will, alex, shawnw, ranjan.bagchi

 Ville-Pertti Keinonen [Wed, 12 May 2004]:
 > 
 > On May 12, 2004, at 2:27 PM, Alex Baretta wrote:
 > 
 > > I disagree. I faced the same problem and found that it cannot be 
 > > easily solved with lockf. Unix.lockf locks specific regions in a file, 
 > > whereas Ranjan seems to need the equivalent of a mutex, which should 
 > > be implemented as a lock over the entire file. This is done by the 
 > > flock primitive in Posix systems. Unluckily, flock has not been 
 > > included in the Unix module.
 > 
 > Actually flock is non-POSIX (it's BSD) and is missing from some 
 > reasonably modern systems.
 > 
 > It's entirely possible to lock the entire file using the fcntl used by 
 > Unix.lockf, making it equivalent to flock.  

They are not *exactly* equivalent: flock does not work on NFS
filesystems whereas fnctl does, and they have different behaviour with
exec or dup I believe.

 > The problem is that the OCaml Unix module doesn't allow the
 > l_whence parameter to be set, which is a serious omission.

Well, it follows the lockf API, which starts the lock at the current
file position. You could use lseek to reposition it but this may confuse
a Pervasives channel buffering this file.

By the way, lockf can block, waiting for a lock. Shouldn't there be
some enter_blocking_section()/leave_blocking_section() somewhere ?

-- 
   Olivier

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:24   ` Ranjan Bagchi
  2004-05-12  7:01     ` Jean-Christophe Filliatre
@ 2004-05-12 16:35     ` Ranjan Bagchi
  1 sibling, 0 replies; 11+ messages in thread
From: Ranjan Bagchi @ 2004-05-12 16:35 UTC (permalink / raw)
  To: caml-list; +Cc: Shawn Wagner

Incidentally -- I found my problem -- basic Unix system programming, I 
needed to seek back to the start of the area before attempting to unlock 
it.  D'oh!

Ranjan

Ranjan Bagchi wrote:

> Cool - I was playing with that call today, although I'm getting 
> occasional EDEADLK exceptions thrown though.  Is there an example of 
> correct use?
> What I'm doing (code isn't handy right now) is before writing to the 
> file, I get a write lock, write my bit, and then unlock the region.   
> Reading bits try to get a Read lock, and then unlock when they're done.
>
> I'm using | in_channel_of_descr| and out_channel_of_descr to do the 
> i/o -- is this a problem?
>
> Ranjan
>
>
> Shawn Wagner wrote:
>
>> On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
>>  
>>
>>> Hi --
>>>
>>> I'm writing some code which will end up executing concurrently on 
>>> several OS processes.  I'd like to serialize access to some specific 
>>> OS resources (for instance, writing to a single file).  The Unix 
>>> module doesn't appear to offer anything like a critical section or 
>>> an OS mutex.  Is there a preferred way to do this?
>>>   
>>
>>
>> File locking with Unix.lockf?
>>
>>
>>  
>>
>
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] Best way to synchronize OS processes?
  2004-05-11 18:05 [Caml-list] Best way to synchronize OS processes? Ranjan Bagchi
  2004-05-11 18:09 ` Shawn Wagner
  2004-05-11 18:10 ` Richard Jones
@ 2004-05-12 16:52 ` Shawn Wagner
  2 siblings, 0 replies; 11+ messages in thread
From: Shawn Wagner @ 2004-05-12 16:52 UTC (permalink / raw)
  To: caml-list

On Tue, May 11, 2004 at 11:05:23AM -0700, Ranjan Bagchi wrote:
> Hi --
> 
> I'm writing some code which will end up executing concurrently on 
> several OS processes.  I'd like to serialize access to some specific OS 
> resources (for instance, writing to a single file).  The Unix module 
> doesn't appear to offer anything like a critical section or an OS 
> mutex.  Is there a preferred way to do this? 
> 

A related note that I thought of last night: How about when you need
something more complicated than a simple mutex? A quick look at the hump
didn't find a library for working with SysV semaphores in ocaml. Would that
be useful for anyone?

-- 
Shawn Wagner
shawnw@speakeasy.org

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-05-12 16:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-11 18:05 [Caml-list] Best way to synchronize OS processes? Ranjan Bagchi
2004-05-11 18:09 ` Shawn Wagner
2004-05-11 18:24   ` Ranjan Bagchi
2004-05-12  7:01     ` Jean-Christophe Filliatre
2004-05-12 16:35     ` Ranjan Bagchi
2004-05-12 11:27   ` Alex Baretta
2004-05-12 11:46     ` Ville-Pertti Keinonen
2004-05-12 13:16       ` Olivier Andrieu
2004-05-12 12:53     ` Gerd Stolpmann
2004-05-11 18:10 ` Richard Jones
2004-05-12 16:52 ` Shawn Wagner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).