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

Re: file descriptor VS file handle



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.)

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).

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.

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.
A possible motivation to have FILE * is given in
  https://www.gnu.org/software/libc/manual/html_node/Streams-and-File-Descriptors.html


Have a nice day :)

Thomas


Reply to: