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
Conversion between file descriptors and strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1997-03-10 (14:32)
From: Pawel Wojciechowski <Pawel.Wojciechowski@c...>
Subject: Conversion between file descriptors and strings

Hi all,

I just started programming in the Objective Caml language. At the current 
stage of my project, I need to implement a communication between a process 
and its child process, which is created by 'fork'. I decided to use pipes 
from the Unix library. Instead of exchanging standard descriptors (0 and 1) 
for descriptors fd_in and fd_out created by 'pipe' (as in the expression
(fd_out, fd_in) = pipe()), I'd prefer to pass descriptors fd_out and fd_in,
to a program executed as a forked process, as arguments when invoking
'execvp'. Then, you can pass these values to the program (which is stored 
in a separate file and compiled separately) as an array of *strings*
being a structural argument to the 'execvp'.

My question is how to convert a descriptor of type file_descr into a string 
and vice-versa?

In C, there is no such problems, since descriptors are integers, and you 
can always write accordingly: 

sprintf(arg1, "%d", pipe[0])


descr =  atoi(argv[1])

In strongly-typed (O)Caml, we should expect the following error message:
"This expression has type int but is here used with type Unix.file_descr"

I don't have much experience in using (O)Caml so appologize if you
consider that my problem is trivial but please help me sort it out! :)



ps. Pipe is a function from the Unix library and it has types as follows:

pipe : init -> file_descr * file_desc