caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Braibant <thomas.braibant@gmail.com>
To: Xavier Leroy <xavier.leroy@inria.fr>
Cc: OCaML Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Unix file descriptors vs. in/out channels
Date: Mon, 18 Aug 2014 18:52:46 +0200	[thread overview]
Message-ID: <CAHR=VkxNA-_sG8FCRmnib0JN-hCFOymk44L4wvJf=5ZnkEqsQA@mail.gmail.com> (raw)
In-Reply-To: <53F22AEB.5070506@inria.fr>

Hi Xavier.

Thanks for your answer.

>> Therefore, I assume that I cannot use
>> Unix.in_channel_of_descr and Unix.out_channel_of_descr more that once
>> for my file-descriptor (because otherwise, these channels would not be
>> reclaimed).
>
> I don't quite understand your remark.  You need to close (explicitly
> and at once) all in_channels and out_channels associated with your
> file_descr, once you're done with it.  The first close_in/close_out
> will close the underlying FD, and the others will ignore the fact that
> the FD is already closed.

Well, I was thinking about the following situation

  let open Unix in
  let fd = openfile "foo.bar" [O_RDWR; O_TRUNC; O_CREAT] 0o640 in
  let o = out_channel_of_descr fd in
  let i = in_channel_of_descr fd in
  let i2 = in_channel_of_descr fd in
  Printf.printf "1\n%!";
  close_in i;
  Printf.printf "2\n%!";
  close_in i2;
  Printf.printf "3\n%!";
  close_out o;
  Printf.printf "Ok\n%!"

that raises the fatal error: exception Sys_error("Bad file
descriptor"), and now, I do not understand your remark either :(.

> For a file opened in RW mode, the problem is that reads through
> the in_channel may not see the data written through the out_channel,
> even if you religiously flush the out_channel before reading anything.
> The reason is that in_channels are also buffered, and may hold stale
> data corresponding to the state of the file before recent writes.  And
> there is no flush operation for in_channels...
>
> This is another gotcha :-)  in_channels and out_channels maintain
> (their idea of) the current position in the file.  This helps avoiding
> unnecessary "lseek" operations to determine current position and
> length.  However, if you share a FD between two channels, the
> channels's idea of the current position is inconsistent with the
> actual position of the FD.

Good to know (especially the part about the in channels not being flushed).

> Bottom line: for your intended application, it's better to use Unix
> functions exclusively.  The trick with an (in_channel, out_channel) pair
> does work pretty well for sockets, named pipes and terminals, though.

That seems like a very good advice, and I will do likewise. (Even if
one minor issue is that it makes interacting with some third-party
libraries that use in/out channels as abstraction more complex.)

Best,
Thomas

  reply	other threads:[~2014-08-18 16:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-18 14:42 Thomas Braibant
2014-08-18 16:10 ` Adrien Nader
2014-08-18 16:15   ` Edouard Evangelisti
2014-08-18 16:29   ` Thomas Braibant
2014-08-18 16:33 ` Xavier Leroy
2014-08-18 16:52   ` Thomas Braibant [this message]
2014-08-18 16:57     ` Xavier Leroy
2014-08-18 17:18       ` Thomas Braibant
2014-08-18 17:55         ` David Sheets

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='CAHR=VkxNA-_sG8FCRmnib0JN-hCFOymk44L4wvJf=5ZnkEqsQA@mail.gmail.com' \
    --to=thomas.braibant@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=xavier.leroy@inria.fr \
    /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).