0

From Beej's socket programming book:

When Unix programs do any sort of I/O, they do it by reading or writing to a file descriptor. A file descriptor is simply an integer associated with an open file.

Does it say that programs read and "write" to a "integer"? If yes, how is it possible and what does it mean?

3 Answers 3

2

It means that in order to read from a file or write to it, you need to perform a system call, and your file descriptor (the integer) is a parameter you use to tell the kernel which file you are talking about. The fact that they are integers is relatively uninteresting, they could be anything; you only ever use them transparently, that is, you open a file, store the descriptor, then you pass the descriptor back to the kernel for any operations on that file. Its value is only interesting to the kernel. One notable exception are the special file descriptors 0, 1, and 2 (stdin, stdout, and stderr), but even for those, you usually use the predefined constants rather than literal integer values.

3
  • Great, does it mean that beej's definition is ambiguous? They said that the programs write "on" that integer. Commented Sep 13, 2011 at 11:09
  • 1
    @Anisha: The key is in the word "associated". The integer represents the file, rather than being the file. The description is a bit misleading, but not much IMO.
    – tdammers
    Commented Sep 13, 2011 at 11:15
  • Whatever you're reading, try to think along with the author... often, things become clear when put into context.
    – tdammers
    Commented Sep 13, 2011 at 11:34
2

I strugled with file descriptors and beejs book too. The moment of enlightement was when I understood how c dup function works.

PS:This is late answer but maybe it can help someone

1

It's saying that file descriptors are implemented as an integer. That is each open file is given a unique way of referencing it, a file handle which is actually just a unique number.

When you do:

int fd = open("filename", flags);

fd is an integer, which is returned by the implementation. It's unique to the file you opened within your program and given to you so that later you can refer to that same file, e.g. for read/write/close etc. It's nothing more than a token used to associate a sequence of operations.

8
  • and I don't understand file descriptors are implemented as an integer. Elaborate in detail please. Commented Sep 13, 2011 at 11:01
  • That's what I already knew, but the quote is saying that the programs write "on" that integer?? Does it mean that they refer to the file on which they have to write using that integer, only? Commented Sep 13, 2011 at 11:07
  • @Anisha - The quote said "reading or writing to a file descriptor". A file descriptor is just what you get back from open. When you call read(fd, ...) you're making a request to read from a specific file descriptor. Likewise write(fd, ...) is a request to write to a specific file descriptor.
    – Flexo
    Commented Sep 13, 2011 at 11:10
  • Well, write(fd, ...) is a request to write to a file descriptor you can't write to an integer? Can you? YOu can just use that integer to "refer" to some file to which you intend to write? Commented Sep 13, 2011 at 11:11
  • 1
    @Anisha - I wouldn't say it was ambiguous. The implication seems clear enough that the file descriptor is used as part of an abstraction of the IO operations on a specific file. I think it might well be phrased in such a way as to (accidentally) presuppose knowledge of file IO though.
    – Flexo
    Commented Sep 13, 2011 at 11:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.