caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] process conundrum
@ 2002-02-13 21:43 David Fox
  2002-02-14 13:42 ` Nicolas FRANCOIS
  2002-02-15 16:07 ` Xavier Leroy
  0 siblings, 2 replies; 3+ messages in thread
From: David Fox @ 2002-02-13 21:43 UTC (permalink / raw)
  To: caml-list

Suppose I open a process to reverse the lines of a file like so:

# let readable, writable = 
  Unix.open_process "cat -n | sort -n -r | sed 's:^[ 0-9]*[^ 0-9]::'";;

Then I write a few lines to it:

# output_string writable "a\n";;
# output_string writable "b\n";;
# output_string writable "c\n";;

Now I close the write end to cause an EOF, so the output arrives on readable:

# close_out writable;;
# input_line readable;;
- : string = "c"
# input_line readable;;
- : string = "b"
# input_line readable;;
- : string = "a"
# input_line readable;;
Exception: End_of_file.

How do I cleanly terminate the process?

# Unix.close_process (readable, writable);;
Exception: Sys_error "Bad file descriptor".
# 
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] process conundrum
  2002-02-13 21:43 [Caml-list] process conundrum David Fox
@ 2002-02-14 13:42 ` Nicolas FRANCOIS
  2002-02-15 16:07 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Nicolas FRANCOIS @ 2002-02-14 13:42 UTC (permalink / raw)
  To: David Fox; +Cc: caml-list

On 13 Feb 2002 13:43:14 -0800
David Fox <david@lindows.com> wrote:

> Suppose I open a process to reverse the lines of a file like so:
> 
> # let readable, writable = 
>   Unix.open_process "cat -n | sort -n -r | sed 's:^[ 0-9]*[^ 0-9]::'";;
> 
> Then I write a few lines to it:
> 
> # output_string writable "a\n";;
> # output_string writable "b\n";;
> # output_string writable "c\n";;
> 
> Now I close the write end to cause an EOF, so the output arrives on
> readable:
> 
> # close_out writable;;
> # input_line readable;;
> - : string = "c"
> # input_line readable;;
> - : string = "b"
> # input_line readable;;
> - : string = "a"
> # input_line readable;;
> Exception: End_of_file.

This can be avoided with a

try ... with End_of_file -> ...
 
> How do I cleanly terminate the process?
> 
> # Unix.close_process (readable, writable);;
> Exception: Sys_error "Bad file descriptor".

I tried your example and only got a 

# Unix.close_process (readable, writable);;
- : Unix.process_status = WEXITED 0

So what's the problem ?

\bye

-- 

                   Nicolas FRANCOIS
            http://nicolas.francois.free.fr
 A TRUE Klingon programmer does NOT comment his code
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] process conundrum
  2002-02-13 21:43 [Caml-list] process conundrum David Fox
  2002-02-14 13:42 ` Nicolas FRANCOIS
@ 2002-02-15 16:07 ` Xavier Leroy
  1 sibling, 0 replies; 3+ messages in thread
From: Xavier Leroy @ 2002-02-15 16:07 UTC (permalink / raw)
  To: David Fox; +Cc: caml-list

> Suppose I open a process to reverse the lines of a file like so:
> 
> # let readable, writable = 
>   Unix.open_process "cat -n | sort -n -r | sed 's:^[ 0-9]*[^ 0-9]::'";;
> 
> Then I write a few lines to it:
> Now I close the write end to cause an EOF, so the output arrives on readable:
> How do I cleanly terminate the process?
> # Unix.close_process (readable, writable);;
> Exception: Sys_error "Bad file descriptor".

This is indeed a bug in Unix.close_process: when it closes both
channels, it should not fail if the output channel is already closed.

Notice however a potential problem in your code: Unix pipes are
fixed-size buffers, so if you write a lot of data to one pipe, the
output pipe may fill up and the underlying process will block.  Thus,
Unix.open_process is usable only if you can alternate writes (of small
quantities) and reads, e.g. via two threads.  In other cases, it is
necessary to use an intermediate file, to hold either the input or the
output of the command.

- Xavier Leroy
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2002-02-15 16:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-13 21:43 [Caml-list] process conundrum David Fox
2002-02-14 13:42 ` Nicolas FRANCOIS
2002-02-15 16:07 ` Xavier Leroy

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