File discriptors

This is a discussion on File discriptors within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Not sure if I'm posting on the correct forum... Ok, so when we say that FD is what it claims ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36

    File discriptors

    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...
    Code:
    darijo@dakt:~/programming/src/sockets$ ./a.out
    PID = 11241
    Socket FD# = 3
    here how things look in /proc FS:
    Code:
    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.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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!

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    /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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    Quote Originally Posted by sean View Post
    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

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    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.
    Last edited by dotunix; 02-09-2009 at 03:43 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dotunix View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21