English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Need help! - Unix.read always returning 0
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-11-08 (18:22)
From: James Lamanna <jamesl@a...>
Subject: Need help! - Unix.read always returning 0
I really can't explain this, but Unix.read is always returning 0.
I have the following simple Ocaml program:
let buffer_length = 16 in
let buffer = ref (String.create buffer_length) in
let j = Unix.openfile "/dev/stick" [Unix.O_RDONLY] 0o666 in
  while true do
        let n = Unix.read j !buffer 0 buffer_length in
            print_int n;
            print_string "\n";
            flush stdout
  End_of_file -> ()

/dev/stick is a linux event device, so it should block.
However, it does not block, and Unix.read consistently returns 0.

However, the corresponding C program:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
int main(int argc, char **argv)
    struct input_event ev;

    int fd = open("/dev/stick", O_RDONLY);

    while(1) {
        int r = read(fd, &ev, sizeof(ev));
        printf("%d\n", r);

    return 0;

Exhibits the desired behavior and reads correctly from the device.

Any help? I'm kinda in a crunch on this.
This behavior started when I moved to an AMD64 platform.

Thank you.
James Lamanna