'Is there a list of linux file descriptors somewhere? [closed]
I'm having trouble finding them, and how to use them in general. for example i see in x86 functions, the output may be a file descriptor. i cant seem to find much about them on the web so im trying it here. and yes im very new to linux
Solution 1:[1]
A file descriptor is basically just an integer. It gets returned by certain functions, e.g. open().
It is used very similar to a pointer in C, you don't do much with it except pass it to other functions, e.g. read(). I don't think doing pointer arithmetic on file handles makes that much sense, though. The fstat() system call will return you a bunch of information about the file. See the manpage:
man 2 fstat
Each process has its own list of file handles, the numbering always starts at 0 for STDIN, 1 for STDOUT and 2 for STDERR, then continues simply counting up as you open and close files.
Except for the first three, that number has no deeper meaning, it just tells the kernel which one from its list of open files it should operate on. The concept seems to be so simple that nobody has bothered to document it... ;-)
Solution 2:[2]
Is there a list of linux file descriptors somewhere?
Yes, the kernel maintains a list of open file descriptors per process. This is the reason you don't find it in internet (it is very dynamic). The ones a process always receives already open, when the program starts are: 0 (standard input); 1 (standard output); and 2 (standard error). This is the mechanism employed in unix systems to allow the parent process to redirect standard input, standard output or standard error. Other file descriptor numbers are assigned/created by the kernel when you open() a file, create a socket with socket() system call, pipe() system call, or some other way (there are many)
A file descriptor per se is just a unique number (each process has a set of distinct numbers) the kernel uses to convey with the process a reference to a kernel resource (like a name) that the program is going to use. When you open() a file, you provide a name and the way you are going to access it (read only, read/write, write only or none -- this last to deal with resources that don't allow you to read/write them, like directories or the like) and receive in exchange a descriptor number that will be used in all the calls related to that file/socket/etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RealUlli |
| Solution 2 | Luis Colorado |
