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?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

3 Answers3

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

Community
  • 1
  • 1
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51
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.

tdammers
  • 20,353
  • 1
  • 39
  • 56
  • Great, does it mean that beej's definition is ambiguous? They said that the programs write "on" that integer. – Aquarius_Girl Sep 13 '11 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 Sep 13 '11 at 11:15
  • Whatever you're reading, try to think along with the author... often, things become clear when put into context. – tdammers Sep 13 '11 at 11:34
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.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • and I don't understand `file descriptors are implemented as an integer.` Elaborate in detail please. – Aquarius_Girl Sep 13 '11 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? – Aquarius_Girl Sep 13 '11 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 Sep 13 '11 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? – Aquarius_Girl Sep 13 '11 at 11:11
  • @Anisha - exactly, which is why the actual work has to be done by a system call which inspects the file descriptor and sees where to actually send the write – Flexo Sep 13 '11 at 11:13
  • So, the statement by Beej is ambiguous? Isn't? – Aquarius_Girl Sep 13 '11 at 11:14
  • 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 Sep 13 '11 at 11:19
  • Okay, yeah, but it could have been written in much better way! :D Thanks. – Aquarius_Girl Sep 13 '11 at 11:23