caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Unix.create_process vs Unix.open_process_out
@ 2008-04-14 12:43 James Jones
  2008-04-14 13:20 ` [Caml-list] " Johann Spies
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: James Jones @ 2008-04-14 12:43 UTC (permalink / raw)
  To: caml-list

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

Consider the following two functions:

let test1 () =
  let w = Unix.open_process_out "cat" in
  assert (Unix.close_process_out w = Unix.WEXITED 0)

let test2 () =
  let r,w = Unix.pipe () in
  let pid = Unix.create_process "cat" [| "cat" |] r Unix.stdout 
Unix.stderr in
  Unix.close w;
  assert (snd (Unix.waitpid [] pid) = Unix.WEXITED 0)


Both execute "cat" and then close its stdin immediately. This should cause 
cat to terminate. In test1 this works as expected but in test2, cat keeps 
running forever. Is this the expected behavior of Unix.create_process?

[-- Attachment #2: Type: text/html, Size: 1257 bytes --]

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

* Re: [Caml-list] Unix.create_process vs Unix.open_process_out
  2008-04-14 12:43 Unix.create_process vs Unix.open_process_out James Jones
@ 2008-04-14 13:20 ` Johann Spies
  2008-04-14 13:32 ` Gerd Stolpmann
  2008-04-14 13:36 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Johann Spies @ 2008-04-14 13:20 UTC (permalink / raw)
  To: caml-list

On Mon, Apr 14, 2008 at 08:43:14AM -0400, James Jones wrote:
> Consider the following two functions:
> 
> let test1 () =
>   let w = Unix.open_process_out "cat" in
>   assert (Unix.close_process_out w = Unix.WEXITED 0)
> 
> let test2 () =
>   let r,w = Unix.pipe () in
>   let pid = Unix.create_process "cat" [| "cat" |] r Unix.stdout Unix.stderr in
>   Unix.close w;
>   assert (snd (Unix.waitpid [] pid) = Unix.WEXITED 0)
> 
> 
> Both execute "cat" and then close its stdin immediately. This should cause cat
> to terminate. In test1 this works as expected but in test2, cat keeps running
> forever. Is this the expected behavior of Unix.create_process?

Did you try "cat <file>" ?

If you just type 'cat' on the commandline it waits for input on
stdin.  If you 'cat <file>' it finishes the job and exit.

Regards
Johann
--
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

     "For the word of God is quick, and powerful, and  
      sharper than any twoedged sword, piercing even to  
      the dividing asunder of soul and spirit, and of the
      joints and marrow, and is a discerner of the thoughts
      and intents of the heart."      Hebrews 4:12 


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

* Re: [Caml-list] Unix.create_process vs Unix.open_process_out
  2008-04-14 12:43 Unix.create_process vs Unix.open_process_out James Jones
  2008-04-14 13:20 ` [Caml-list] " Johann Spies
@ 2008-04-14 13:32 ` Gerd Stolpmann
  2008-04-14 13:36 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2008-04-14 13:32 UTC (permalink / raw)
  To: James Jones; +Cc: caml-list


Am Montag, den 14.04.2008, 08:43 -0400 schrieb James Jones:
> Consider the following two functions:
> 
> let test1 () =
>   let w = Unix.open_process_out "cat" in
>   assert (Unix.close_process_out w = Unix.WEXITED 0)
> 
> let test2 () =
>   let r,w = Unix.pipe () in
>   let pid = Unix.create_process "cat" [| "cat" |] r Unix.stdout
> Unix.stderr in
>   Unix.close w;
>   assert (snd (Unix.waitpid [] pid) = Unix.WEXITED 0)
> 
> 
> Both execute "cat" and then close its stdin immediately. 

Ahem, the second version doesn't. Note that there are 4 involved
descriptors: r and w before forking, and r and w after forking (resp.,
the descriptors in the calling and in the called program). You close
only one of the w's, so the pipe remains open.

> This should cause cat to terminate. In test1 this works as expected
> but in test2, cat keeps running forever. Is this the expected behavior
> of Unix.create_process? 

Yes.

Gerd

> _______________________________________________
> 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
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------



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

* Re: [Caml-list] Unix.create_process vs Unix.open_process_out
  2008-04-14 12:43 Unix.create_process vs Unix.open_process_out James Jones
  2008-04-14 13:20 ` [Caml-list] " Johann Spies
  2008-04-14 13:32 ` Gerd Stolpmann
@ 2008-04-14 13:36 ` Gerd Stolpmann
  2 siblings, 0 replies; 4+ messages in thread
From: Gerd Stolpmann @ 2008-04-14 13:36 UTC (permalink / raw)
  To: James Jones; +Cc: caml-list


Am Montag, den 14.04.2008, 08:43 -0400 schrieb James Jones:
> Consider the following two functions:
> 
> let test1 () =
>   let w = Unix.open_process_out "cat" in
>   assert (Unix.close_process_out w = Unix.WEXITED 0)
> 
> let test2 () =
>   let r,w = Unix.pipe () in
>   let pid = Unix.create_process "cat" [| "cat" |] r Unix.stdout
> Unix.stderr in
>   Unix.close w;
>   assert (snd (Unix.waitpid [] pid) = Unix.WEXITED 0)

P.S. You can close the "other" w by setting the close on exec flag:

let test2 () =
  let r,w = Unix.pipe() in
  Unix.set_close_on_exec w;
  let pid = Unix.create_process "cat" [| "cat" |] r Unix.stdout
Unix.stderr in
  Unix.close w;
  assert (snd (Unix.waitpid [] pid) = Unix.WEXITED 0);;

Gerd


> 
> 
> Both execute "cat" and then close its stdin immediately. This should
> cause cat to terminate. In test1 this works as expected but in test2,
> cat keeps running forever. Is this the expected behavior of
> Unix.create_process? 
> _______________________________________________
> 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
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------



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

end of thread, other threads:[~2008-04-14 13:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-14 12:43 Unix.create_process vs Unix.open_process_out James Jones
2008-04-14 13:20 ` [Caml-list] " Johann Spies
2008-04-14 13:32 ` Gerd Stolpmann
2008-04-14 13:36 ` Gerd Stolpmann

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