Not sure if I'm posting on the correct forum...
Ok, so when we say that FD is what it claims to be...a file....is it like an ordinary file on some drive or just some kind of a logical structure somewhere in the memory of the running process?
Here is what I did:
Started a process which creates a stream socket, prints PID and FD number and then waits for incoming connections...
here how things look in /proc FS:
PID = 11241
Socket FD# = 3
So it looks that standard FDs (stdin, stdout, stderr ) are actual files on the disk (correct me if I'm wrong ), the number returned by a socket() call is a link to 'socket number' which is located...who knows where???
darijo@dakt:/proc/11241/fd$ ls -l
lrwx------ 1 darijo darijo 64 2009-02-09 05:56 0 -> /dev/pts/0
lrwx------ 1 darijo darijo 64 2009-02-09 05:56 1 -> /dev/pts/0
lrwx------ 1 darijo darijo 64 2009-02-09 05:56 2 -> /dev/pts/0
lrwx------ 1 darijo darijo 64 2009-02-09 05:56 3 -> socket:
Thank you for your time and sorry for bad English.
File descriptors can be used to refer to actual files, or they can point to other logical structures. Depending on your OS, those logical structures may still be files. For instance, FIFOs ("named pipes") in Unix may seem like a logical structure, because they aren't what you typically think of as a file - but they have a path and they show up in the directory as long as they are in existence, so technically, they are files.
For future reference, a post like this could be place either in the forum of the language (C/C++) you are using, or in the forum of the system you are programming for (Windows, Linux). If you don't think a question falls into any of those categories, the Tech board is nice and general!
As far as a process is concerned, a file descriptor is an integer. There is a memory structure associated with each file descriptor, but this is in the kernel, not the process.
/dev/pts is a "pseudo-terminal", which is a "device that isn't a single physical device". In this case, I would guess that you are using Xterminal or some such to run your application.
/dev/pts acts almost like a pipe or socket, in that it can be read and written, but there's no REAL hardware that the reads/writes are forwarded to. Instead, the information is stored in a buffer, and later on when a corresponding process on the other side (e.g. the XTerminal's display portion) is fetching the information, it gets the output from your program.
Note that if you do "myprog > foo.txt", then stdout (fd 1) will point to the foo.txt file [in the current directory] rather than /dev/pts.
Sockets are also a "pseudo-device" - it acts as a proxy for a interconnect to another process (which could be in the same machine or on a machine the other side of this planet).
/proc is another example of "pseudo-devices".
And as described above, the file descriptor is an integer - the internal representation of a "file" is an internal data-structure in the kernel. Most likely the file-descriptor is simply an index into a table, where each actual file is described, although that is by no means guaranteed - the OS may use any method it likes to store the actual file-information - if it wants to take the file-descriptor and feed it as a seed into a random number generator, and then use the random number to find a position in a linked list, that would be perfectly within the OS's rights - it just would be much more complicated than it has to be!
Originally Posted by sean
Thank you guys, that helped!
So it looks that FD is only a "pointer" to the structure, just like....first part of the logical address is only a pointer to structure in GDT.
Yes, I suppose you can see it that way.
Originally Posted by dotunix