caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Unix file descriptors vs. in/out channels
@ 2014-08-18 14:42 Thomas Braibant
  2014-08-18 16:10 ` Adrien Nader
  2014-08-18 16:33 ` Xavier Leroy
  0 siblings, 2 replies; 9+ messages in thread
From: Thomas Braibant @ 2014-08-18 14:42 UTC (permalink / raw)
  To: OCaML Mailing List

Hi list,

[summary] I would like to open a file in read-write mode, and use it
(mainly) to stream a big data-structure in it and (sometime) reading
the content of this data-structure.

[problem] I am a bit puzzled w.r.t. the interplay between Pervasives
functions that operate on in/out channels and the Unix function that
operate on file descriptors. From the documentation, I assume that it
is not possible to close an (input) channel that was created using
Unix.in_channel_of_descr without closing the associated file
descriptor. 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). But, is is safe to use both kind of channels?

Btw, while playing with this problem, I found the following strange
behavior: if I uncomment the second line in debug (see below), I can
read data from the input channel, while if the debug line is comment,
reading from the channel yields an End_of_file exception. Is this
expected?

let debug msg i o =
  Printf.printf "[%s] posi:%i poso:%o\n%!" msg (pos_in i) (pos_out o);
  (* Printf.printf "[%s] leni:%i leno:%o\n%!" msg (in_channel_length
i) (out_channel_length o);*)
  ()

let test =
  let open Unix in
  let fd = openfile "foo.bar" [O_RDWR; O_TRUNC; O_CREAT] 0o640 in
  Printf.printf "openfile\n%!";
  let o = out_channel_of_descr fd in
  Printf.printf "out_channel_of_descr\n%!";
  let i = in_channel_of_descr fd in
  Printf.printf "in_channel_of_descr\n%!";
  debug "1" i o;
  let _ = Printf.fprintf o "test1\n%!" in
  debug "2" i o;
  assert (write fd "test2" 0 5 = 5);
  debug "3" i o;
  let _ = input_char i  in
  debug "4" i o;
  let _ = close_out o in
  Printf.printf "closeout\n%!";
  try
    ignore (write fd "test3" 0 5);
    close fd;
    Printf.printf "success\n%!"
  with
    _ -> Printf.printf "fail\n%!"

Best,
Thomas

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

end of thread, other threads:[~2014-08-18 17:55 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-18 14:42 [Caml-list] Unix file descriptors vs. in/out channels 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
2014-08-18 16:57     ` Xavier Leroy
2014-08-18 17:18       ` Thomas Braibant
2014-08-18 17:55         ` David Sheets

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