[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: file descriptor VS file handle



On Thu, Jul 13, 2023 at 08:49:38AM +0200, Thomas Schmitt wrote:
> Hi,
> 
> coreyh@free.fr wrote:
> > In linux systems, are file descriptor and file handle meaning the same
> > stuff?
> 
> In the programming language C on Linux (more generally: on POSIX systems)
> "File descriptor" is an integer number handed out by system calls like
> open(2), pipe(2), socket(2), and others. It can be used by calls like
> read(2), write(2), recv(2), send(2) to get or to deliver data.
> (Execute e.g.
>    man 2 open
> to get the manual page of the desired system call.)

Exactly.

> I am not aware that "file handle" has such a well defined meaning in
> POSIX. To my understanding it just means a programming object that can be
> used for similar activities as a file descriptor (and possibly for more
> file related activities).

In the Unix-ish C world it plainly doesn't exist. It is customary
in Perl to designate a data structure wrapping (among other things)
a file descriptor, but also buffers and things. Akin to a FILE * in
(POSIX-ish) C.

> Maybe you mean "file pointer", which usually refers to the return value
> "FILE *" of fopen(3) and can be used by calls like fread(3), fscanf(3),
> fwrite(3), or fprintf(3). This is implemented on top of file descriptor.
> The call fileno(3) can obtain the file descriptor of a file pointer, so
> that the above calls of man chapter 2 can be used.
> The call fdopen(3) can create a file pointer from a file descriptor.

Yep. This is the C "moral equivalent" to a Perl "file handle". I don't
know any other context where this is used (but it probably resonates
with the Windows folks, for whom "everything is a window handle" since
times Windows 3.1 immemorial(.

> Why there are two sets of file accessing system calls must have historical
> reasons. They were already present in the mid 1980s. But obviously file
> descriptor is the more fundamental one.

I think they just have different uses. Plus, file descriptor is a
kernel thing, FILE* is libc, i.e. userspace [1] (I know *you* know it,
but it might be interesting for others).

Cheers

[1] "God made the integers, all else is the work of man"
    https://en.wikipedia.org/wiki/Leopold_Kronecker
-- 
t

Attachment: signature.asc
Description: PGP signature


Reply to: