English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] process conundrum
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-02-15 (16:07)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] process conundrum
> 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