File Descriptors and File Handles (and C)
Asked Answered
I

2

12

Can someone explain to me precisely the difference between a file descriptor and a file handle (separate from Windows' definition for the term; I'm curious about their definition, but that would require a much longer answer)?

From what I have gathered from Wikipedia, a file descriptor is an index in a file descriptor table, which points to a file name in a file table, which in turn points to an inode in an inode table. File handle is a type of data structure that stores a file descriptor.

  • Is a file descriptor simply an index in the file descriptor table (i.e., the index value simply is the file descriptor)?
  • Or does the file descriptor table element, identified by its index, store the file descriptor (making them likely two different numbers, assuming the index is a number)?
  • Is "file handle" simply a term for the FILE data structure in C which stores file descriptors? Or does file handle refer to some other data structure, which stores a file descriptor, separate from the C data structure (FILE)? What else can anyone tell me about the nature of file handle data structures?
Iroquois answered 5/11, 2015 at 3:17 Comment(2)
In C generaly we speak Descriptors and Streams : gnu.org/software/libc/manual/html_node/…Subchaser
Take a look here for descriptors and streams -gnu.org/software/libc/manual/html_node/…Newton
R
8

First of all we need separate interface and implementation. On POSIX conforming systems, you get int as result of open(2) and after that you can use this int for writing, reading and so on operations. This is interface.

In the kernel, which manage open files for concrete process, map from int to some structure that kernel used to manage files may be implemented in many ways, may be array, may be binary tree and so on.

As for linux kernel, for each open file it has struct file, and to work with int kernel has to map int to address of struct file. It do it in such simple way - fdt->fd[fd], where ftd is pointer to struct fdtable, fd is file descriptor (int) (I do not describe the way of how kernel handle multi thread access to tables). This is current implementation in Linux kernel. It can be changed in the future.

File handle that return CreateFile is used in similar to file descriptor way, it is integer, that used as index to access Windows analog of struct file in per process table.

So I think that file handle and file descriptor may be used as synonyms, but usually when talk about file handle mean HANDLE, and when talk about file descriptors talk about POSIX int return by open. On windows HANDLE also used to manage events, GUI windows and so on, this is not true for file descriptors on POSIX systems, but on POSIX systems file descriptors also used to manage sockets, while on windows this was impossible until winsock 2.

And about FILE. FILE in interface above file descriptor and file handle, it has one of them inside, plus some data to manage buffered I/O. For example from popular linux C runtime glibc:

struct _IO_FILE {
  int _flags;           /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
//...
};

As you can see it has _fileno, it file descriptor which glibc get from OS by open(2) call. But it has a lot of stuff to buffering, translate end of lines etc.

Roveover answered 5/11, 2015 at 13:23 Comment(3)
Thank you for the great answer, even though I am not the person who asked this. Then, from your answer, is _IO_FILE different from the struct file up in the paragraph?Calmative
@SmartHumanism Yes, struct file is part of linux kernel, while _IO_FILE is part of glibc. In other words these are different types defined in different programs.Roveover
I really appreciate your reply, sir. Can I ask you one more, please? Then, is _IO_FILE so-called stream as in managed languages like C# in DotNet?Calmative
S
0

File descriptor is a positive integer index of a process's exlusive file descriptor table. The items of the file descriptor table are references to a system-wide open file description table. The items of the open file description table includes references to inodes and some attributes of the opened resources.

I suppose when you open a resource by open(), first the item of the open file description table is created and added to it, then the item of the file descriptor table does. In the end, the index of the item created of the file descriptor table is returned. When you read a resource by read(), the file descriptor is used to find the item in the file descriptor table to find the associated item in the open file description table by which you get the inodes of the file and some attributes, such as open mode or reading/writing positions. This might no be the exactly process, just my educated guess.

For the FILE struct doesn't find eactly information about it. But it seems it records information of the file descriptor or the items of the open file descritption table or both of them, then using the same above process to acess resources. And it seems that file handle isn't a well defined term, as many specifications don't mention it. But, doubtly, many articles use it as a name for the item of the open file description table.

More detailed explanations: File descriptor and open file description

Schizont answered 7/6 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.