caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unix module and threads
@ 2004-09-05  8:00 Janne Hellsten
  2004-09-05 13:32 ` David Brown
  2004-09-05 13:42 ` Markus Mottl
  0 siblings, 2 replies; 8+ messages in thread
From: Janne Hellsten @ 2004-09-05  8:00 UTC (permalink / raw)
  To: caml-list

Hello caml-list,

I wonder what happens if I call [Unix.chdir foo] from two different 
threads, with two different directories?

After chdir I typicall call [Unix.system] and/or 
[Unix.open_process_full] and expect the directory to be the one just 
previously set with [Unix.chdir].

I would like to parallelize a simple building tool that I've written, 
but I'm a bit worried if things will mysteriously break with the 
introduction of multi-threaded control.

I am running both Windows (Cygwin and native) and Linux.

Thank you for your help,
Janne Hellsten



-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05  8:00 [Caml-list] Unix module and threads Janne Hellsten
@ 2004-09-05 13:32 ` David Brown
  2004-09-05 13:42 ` Markus Mottl
  1 sibling, 0 replies; 8+ messages in thread
From: David Brown @ 2004-09-05 13:32 UTC (permalink / raw)
  To: Janne Hellsten; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 567 bytes --]

On Sun, Sep 05, 2004 at 11:00:04AM +0300, Janne Hellsten wrote:

> I wonder what happens if I call [Unix.chdir foo] from two different 
> threads, with two different directories?

I think this is going to depend on how threads are implemented, and as I
understand it, that varies depending on what platform, and which compiler
you are using.

I've attached a small program that will help determine this.  As far as I
can tell, on Linux, there seems to only be one current directory shared
between all of the threads.  This is the same for native and byte-code.

Dave

[-- Attachment #2: threa.ml --]
[-- Type: text/plain, Size: 733 bytes --]

(* Show chdir and threads. *)

let phase = ref 0
let phase_lock = Mutex.create ()
let phase_cond = Condition.create ()

let wait_phase n =
  Mutex.lock phase_lock;
  while !phase != n do
    Condition.wait phase_cond phase_lock
  done;
  Mutex.unlock phase_lock

let mark_phase n =
  Mutex.lock phase_lock;
  phase := n;
  Condition.signal phase_cond;
  Mutex.unlock phase_lock

let thread_a () =
  wait_phase 1;
  Unix.chdir "a";
  mark_phase 2;
  wait_phase 3

let thread_b () =
  (* Execution starts here. *)
  mark_phase 1;
  wait_phase 2;
  Printf.printf "result dir: %S\n" (Unix.getcwd ());
  mark_phase 3

let () =
  let a = Thread.create thread_a () in
  let b = Thread.create thread_b () in
  Thread.join a;
  Thread.join b

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

* Re: [Caml-list] Unix module and threads
  2004-09-05  8:00 [Caml-list] Unix module and threads Janne Hellsten
  2004-09-05 13:32 ` David Brown
@ 2004-09-05 13:42 ` Markus Mottl
  2004-09-05 13:53   ` Janne Hellsten
                     ` (2 more replies)
  1 sibling, 3 replies; 8+ messages in thread
From: Markus Mottl @ 2004-09-05 13:42 UTC (permalink / raw)
  To: Janne Hellsten; +Cc: caml-list

On Sun, 05 Sep 2004, Janne Hellsten wrote:
> I wonder what happens if I call [Unix.chdir foo] from two different 
> threads, with two different directories?

This is not thread-safe: threads share the current working directory of
the process.

> After chdir I typicall call [Unix.system] and/or 
> [Unix.open_process_full] and expect the directory to be the one just 
> previously set with [Unix.chdir].
>
> I would like to parallelize a simple building tool that I've written, 
> but I'm a bit worried if things will mysteriously break with the 
> introduction of multi-threaded control.

Always use absolute paths to avoid problems in multi-threaded contexts.

Regards,
Markus

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05 13:42 ` Markus Mottl
@ 2004-09-05 13:53   ` Janne Hellsten
  2004-09-05 15:27     ` skaller
  2004-09-06  9:07     ` Keith Wansbrough
  2004-09-05 13:57   ` Daniel Andor
  2004-09-05 15:12   ` skaller
  2 siblings, 2 replies; 8+ messages in thread
From: Janne Hellsten @ 2004-09-05 13:53 UTC (permalink / raw)
  To: caml-list

Thank you Markus and David, this is sort of what I was afraid of (e.g., 
curdir is shared among threads). Thank you for clarifying this.

As my build scripts require the working directory to be set, I think I 
have to work-around this and do something along the lines of:

    Unix.system "cd my_working_dir && build_cmd"

I wonder if there's some other way of doing this portably and in a 
thread-safe fashion?

Best regards,
Janne Hellsten

>  
>

-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05 13:42 ` Markus Mottl
  2004-09-05 13:53   ` Janne Hellsten
@ 2004-09-05 13:57   ` Daniel Andor
  2004-09-05 15:12   ` skaller
  2 siblings, 0 replies; 8+ messages in thread
From: Daniel Andor @ 2004-09-05 13:57 UTC (permalink / raw)
  To: Janne Hellsten, caml-list

On Sunday 05 September 2004 2:42 pm, Markus Mottl wrote:
> On Sun, 05 Sep 2004, Janne Hellsten wrote:
> > I wonder what happens if I call [Unix.chdir foo] from two different
> > threads, with two different directories?
>
> This is not thread-safe: threads share the current working directory of
> the process.
>
> > After chdir I typicall call [Unix.system] and/or
> > [Unix.open_process_full] and expect the directory to be the one just
> > previously set with [Unix.chdir].

If you absolutely must use chdir, perhaps you could use a mutex (a `path' 
mutex, if you like) to control modification of the process working directory?

D

-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05 13:42 ` Markus Mottl
  2004-09-05 13:53   ` Janne Hellsten
  2004-09-05 13:57   ` Daniel Andor
@ 2004-09-05 15:12   ` skaller
  2 siblings, 0 replies; 8+ messages in thread
From: skaller @ 2004-09-05 15:12 UTC (permalink / raw)
  To: Markus Mottl; +Cc: Janne Hellsten, caml-list

On Sun, 2004-09-05 at 23:42, Markus Mottl wrote:

> Always use absolute paths to avoid problems in multi-threaded contexts.

I go so far as to suggest relative paths be resolved
into absolute ones ASAP in almost all contexts:
even a correct singly threaded program using relative
paths is likely to be fragile (meaning the correctness
property is easy to clobber with small changes).

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05 13:53   ` Janne Hellsten
@ 2004-09-05 15:27     ` skaller
  2004-09-06  9:07     ` Keith Wansbrough
  1 sibling, 0 replies; 8+ messages in thread
From: skaller @ 2004-09-05 15:27 UTC (permalink / raw)
  To: janne; +Cc: caml-list

On Sun, 2004-09-05 at 23:53, Janne Hellsten wrote:
> Thank you Markus and David, this is sort of what I was afraid of (e.g., 
> curdir is shared among threads). Thank you for clarifying this.
> 
> As my build scripts require the working directory to be set, I think I 
> have to work-around this and do something along the lines of:
> 
>     Unix.system "cd my_working_dir && build_cmd"
> 
> I wonder if there's some other way of doing this portably and in a 
> thread-safe fashion?

You could do this:

(1) use a mutex to freeze the current directory
(2) launch a new process (and return without waiting)
(3) release he mutex
(4) wait for the process

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
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] 8+ messages in thread

* Re: [Caml-list] Unix module and threads
  2004-09-05 13:53   ` Janne Hellsten
  2004-09-05 15:27     ` skaller
@ 2004-09-06  9:07     ` Keith Wansbrough
  1 sibling, 0 replies; 8+ messages in thread
From: Keith Wansbrough @ 2004-09-06  9:07 UTC (permalink / raw)
  To: janne; +Cc: caml-list

Janne Hellsten <janne@hybrid.fi> writes:

> Thank you Markus and David, this is sort of what I was afraid of (e.g., 
> curdir is shared among threads). Thank you for clarifying this.
> 
> As my build scripts require the working directory to be set, I think I 
> have to work-around this and do something along the lines of:
> 
>     Unix.system "cd my_working_dir && build_cmd"
> 
> I wonder if there's some other way of doing this portably and in a 
> thread-safe fashion?

Instead of using Unix.system, do a fork()-and-exec() (as Unix.system
presumably does under the hood), and set the current directory in the
child process after the fork and before the exec.  Take a look at the
implementation of Unix.system in otherlibs/unix/unix.ml to see what I
mean.

--KW 8-)

-------------------
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] 8+ messages in thread

end of thread, other threads:[~2004-09-06  9:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-05  8:00 [Caml-list] Unix module and threads Janne Hellsten
2004-09-05 13:32 ` David Brown
2004-09-05 13:42 ` Markus Mottl
2004-09-05 13:53   ` Janne Hellsten
2004-09-05 15:27     ` skaller
2004-09-06  9:07     ` Keith Wansbrough
2004-09-05 13:57   ` Daniel Andor
2004-09-05 15:12   ` skaller

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).