Version française
Home     About     Download     Resources     Contact us    
Browse thread
Unix module troubles & Time functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William D. Neumann <wneumann@c...>
Subject: Re: [Caml-list] Unix module troubles & Time functions
On Tue, 18 Jul 2006, Julien Michel wrote:

> and trying to compile this way:
> ocamlc -custom -o main unix.cma *ml
> returns this error while executing on my target:
> Fatal error: unknown C primitive `unix_dup\'
>
> What can be wrong with my system so that I cannot use the Unix.module?

Well, you don't have the dup functionality available.  Possibly other 
things are missing as well.  You cauld look into how the Unix build is 
configured and modify it to skip the functionality you don't have (or 
better yet raise an exception).  But that might be more work than you 
need.

> The Gettimeofday unix function usually works well on my target, is there another
> way for having access to it with Caml?

Yes.  You can write your own interface to the gettimeofday function 
(that's all the Unix module's function is).

timer.c
====================================================================

#include <sys/time.h>
#include <caml/mlvalues.h>
#include <caml/alloc.h>
#include <caml/memory.h>
#include <caml/fail.h>

CAMLprim value my_gettimeofday(value unit)
{
   struct timeval tp;
   if (gettimeofday(&tp, NULL) == -1) caml_failwith("gettimeofday: 
failure");
   return copy_double((double) tp.tv_sec + (double) tp.tv_usec / 1e6);
}

====================================================================

timer.ml
====================================================================

external timeofday : unit -> float = "my_gettimeofday"

let _ =
   let t1 = timeofday ()
   and t2 = Unix.gettimeofday () in
   Printf.printf "mytime: %f, utime: %f\n" t1 t2;;

====================================================================

[414]  9:24AM% ocamlc timer.c 
[415]  9:29AM% ocamlc -custom -o timer.bc timer.o unix.cma timer.ml 
[416]  9:29AM% ./timer.bc 
mytime: 1153236588.033843, utime: 1153236588.033860

(Note, you won't have the Unix module, so you don't need the unix.cma part 
when you compile your use of the new timeofday function, I just neede it 
to compare to the Unix.gettimeofday function)

Read chapter 18 of the OCaml manual for more info on interfacing OCaml 
with C.

William D. Neumann

---

"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder