caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <gerd@gerd-stolpmann.de>
To: fuchs@cs.uiowa.edu
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Strange buffering interaction: stdout / file_descr
Date: Sun, 24 May 2009 12:55:56 +0200	[thread overview]
Message-ID: <1243162556.11739.7.camel@flake.lan.gerd-stolpmann.de> (raw)
In-Reply-To: <1243113461.18993.34.camel@Knecht>

This is the somewhat expected behavior, although a bit surprising at the
first glance. create_process isn't an atomic operation. Actually, it
forks the process (and thus implicitly duplicates the stdout buffer). In
the forked child it tries to exec the new executable. If this fails, it
does a regular exit(), and the buffers are flushed.

One can argue that this is wrong - the implementation should better do
an _exit (i.e. immediately exit the child process without doing any sort
of "cleanup"). AFAIK, system() implementations do this.

The standard way of checking for exec errors is to test the exit code,
and if it is 127, exec or the code immediately before exec failed.
That's a convention only, i.e. nothing enforces this.

Gerd

Am Samstag, den 23.05.2009, 16:17 -0500 schrieb Alexander Fuchs:
> let () =
>   print_string "main";
> 
>   let (stdout_r, stdout_w) = Unix.socketpair Unix.PF_UNIX
> Unix.SOCK_STREAM 0 in
>   let (stdin_r, stdin_w) = Unix.socketpair Unix.PF_UNIX
> Unix.SOCK_STREAM
> 0 in
> 
>   let cmd = "none" in
> 
>   (* without flush reading from stdout_r yields "main" *)
>   (* flush stdout; *)
>   let pid = Unix.create_process cmd [| cmd; "-version" |] stdin_r
> stdout_w stdout_w in
> 
>   Unix.close stdin_r;
>   Unix.close stdout_w;
> 
>   begin
> 
>     let (outs, _, _) = Unix.select [stdout_r] [] [] (-1.0) in
> 
>     match outs with
>     | [out] ->
>         let buffer = String.make 100 ' ' in
>         let size =  Unix.read out buffer 0 100 in
> 
>         if size > 0 then begin
>           print_endline (buffer)
>         end
> 
>         else begin
>           print_endline "create_process failed"
>         end
> 
>     | _ ->
>         assert false;
>   end;
> 
>   Unix.close stdout_r;
>   Unix.close stdin_w;
>   Unix.kill pid Sys.sigkill;
>   let (_, _status) = Unix.waitpid [] pid in
>   ()
> 
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
Phone: +49-6151-153855                  Fax: +49-6151-997714
------------------------------------------------------------



      reply	other threads:[~2009-05-24 10:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-23 21:17 Alexander Fuchs
2009-05-24 10:55 ` Gerd Stolpmann [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1243162556.11739.7.camel@flake.lan.gerd-stolpmann.de \
    --to=gerd@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=fuchs@cs.uiowa.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).