From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 5C76C7F747 for ; Mon, 18 Aug 2014 16:42:34 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of thomas.braibant@gmail.com) identity=pra; client-ip=74.125.82.44; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="thomas.braibant@gmail.com"; x-sender="thomas.braibant@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of thomas.braibant@gmail.com designates 74.125.82.44 as permitted sender) identity=mailfrom; client-ip=74.125.82.44; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="thomas.braibant@gmail.com"; x-sender="thomas.braibant@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wg0-f44.google.com) identity=helo; client-ip=74.125.82.44; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="thomas.braibant@gmail.com"; x-sender="postmaster@mail-wg0-f44.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoMBANkP8lNKfVIsm2dsb2JhbABZhDcEgnizfJ5wCBYQAQEBAQEGCwsJFCmEHBEEGQEbHgMSEA8CJgIkAREBBQEiGxqICwEDEZxagxlriyuBcoMQiXUKGScNZoRNEQEFDoEekSCBUwEEkiCKIpMRGCmFEDsvgk8BAQE X-IPAS-Result: AoMBANkP8lNKfVIsm2dsb2JhbABZhDcEgnizfJ5wCBYQAQEBAQEGCwsJFCmEHBEEGQEbHgMSEA8CJgIkAREBBQEiGxqICwEDEZxagxlriyuBcoMQiXUKGScNZoRNEQEFDoEekSCBUwEEkiCKIpMRGCmFEDsvgk8BAQE X-IronPort-AV: E=Sophos;i="5.01,886,1400018400"; d="scan'208";a="89717561" Received: from mail-wg0-f44.google.com ([74.125.82.44]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 18 Aug 2014 16:42:33 +0200 Received: by mail-wg0-f44.google.com with SMTP id m15so4985575wgh.3 for ; Mon, 18 Aug 2014 07:42:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=N9ZjM1gTObRHtms4EAh/d+k9uR00ymJ0FWlD4UaKgMU=; b=lI88eJJNJpMOlo/ZSJbYUNcyGcT2vLVkqzEOfpXlEBln6dj+HnEfzmWQjMh2aa0A0d XDfEpNahL65JE/YRZ9KMwLl4PSFZcba0jN4C5PSAN1tRL4gmakrBAdJ9kD+yc8/itg5r lmaO2sxiIIluBZALenFhsAcz6O3cwdMXb03G3Agoii5DT6meMOJwMnrx1Ld3ptJ0db9v EfmfLVG+Nf/qPxGiXX/qWT8MQz0Od0GTThzyr9uIkyFr87ugz2l59oMOv2nQmWJKJs28 AOsVTQ/VX6xt6iHKbmCrHPVtfDXD7ZFgSxem2/+NLhZ+gpb1FYxbv9Yeg2H2klUPMOCT Q7Xw== X-Received: by 10.194.243.230 with SMTP id xb6mr9401161wjc.100.1408372953671; Mon, 18 Aug 2014 07:42:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.9.226 with HTTP; Mon, 18 Aug 2014 07:42:13 -0700 (PDT) From: Thomas Braibant Date: Mon, 18 Aug 2014 16:42:13 +0200 Message-ID: To: OCaML Mailing List Content-Type: text/plain; charset=UTF-8 Subject: [Caml-list] Unix file descriptors vs. in/out channels 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