PDA

View Full Version : File discriptors



dotunix
02-08-2009, 03:47 PM
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...


darijo@dakt:~/programming/src/sockets$ ./a.out
PID = 11241
Socket FD# = 3


here how things look in /proc FS:


darijo@dakt:/proc/11241/fd$ ls -l
total 0
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:[60726]


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???


Thank you for your time and sorry for bad English.

sean
02-08-2009, 05:40 PM
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.

sean
02-08-2009, 05:41 PM
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!

brewbuck
02-08-2009, 08:44 PM
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.

matsp
02-09-2009, 03:34 AM
/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!

--
Mats

dotunix
02-09-2009, 04:37 AM
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!

Okay

dotunix
02-09-2009, 04:40 AM
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.

matsp
02-09-2009, 04:50 AM
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.

--
Mats