The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File descriptor constant vino High Level Programming 4 05-17-2005 10:18 PM
Problems with file descriptor teo High Level Programming 11 05-09-2005 08:47 AM
File Descriptor Help rahulrathod UNIX for Dummies Questions & Answers 3 10-14-2004 02:08 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 10:54 AM
bad file descriptor? ftb UNIX for Dummies Questions & Answers 1 02-20-2002 04:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2007
Registered User
 

Join Date: Dec 2007
Posts: 1
File Descriptor Table

Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB.

I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could explain to me a little about how they are implemented. I know basically how it works, what I am really having a little trouble with now is how file descriptors are mapped to input/output streams. Are their pointers to the pipe / device, or is there some other way its done? Thanks.

-shane
Reply With Quote
Forum Sponsor
  #2  
Old 12-02-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Basically a file descriptor table is a kernel mantained array of pointers to kernel objects representing open files of some kind.

Imagine for a moment that these were C++ object...

Code:
int read(int fd,char *buf,size_t len)
{
    return fds[fd]->read(buf,len);
}

int write(int fd,char *buf,size_t len)
{
    return fds[fd]->write(buf,len);
}

int close(int fd,char *buf,size_t len)
{
    fds[fd]->close();
    fds[fd]=NULL;
}
But as kernels are generally written in C, other mechanisms, (pointers to jump tables etc) are used to provide the polymorphism.

Also reference counting is heavily used, so "close" for instance only really drops a reference count, if it gets to zero then the true close occurs.

What you need to consider in the model is where flags such as whether a file descriptor is blocking or not, and where the file-offset should live. Ask yourself, if I use "dup()" do both file descriptors have the same file offset?

If you do look inside the Linux kernel for instance you will notice that the BSD sockets API is handled quite differently to normal ioctl/read/write.

Last edited by porter; 12-02-2007 at 06:56 PM.
Reply With Quote
  #3  
Old 12-02-2007
Registered User
 

Join Date: Apr 2006
Location: Northern Virginia
Posts: 23
Not sure we're answering your question..

Quote:
Originally Posted by Ashaman0 View Post
Im working on writing a small operating system. I am currently working on implementing dup, dup2, pipe, and close and I need to implement some type of file descriptor table in my PCB.

I was wondering if there is anyone who is familiar with linux/unix implementation of these tables who could explain to me a little about how they are implemented. I know basically how it works, what I am really having a little trouble with now is how file descriptors are mapped to input/output streams. Are their pointers to the pipe / device, or is there some other way its done? Thanks.

-shane
Streams must necessarily have a file descriptor as one of their attributes. Streams are a higher level processing construct than the driver level open/close/read/write/ioctl entry points. Generally they at least manage a buffering logic that works "above" what's actually going on in the kernel's queueing mechanisms.

You can email me direct email address removed if you want a more rapid dialogue.

S.

Last edited by vino; 12-02-2007 at 09:14 PM. Reason: email address removed
Reply With Quote
  #4  
Old 12-02-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by fsahog View Post
Streams must necessarily have a file descriptor as one of their attributes.
Depends if you are referering to Sys V STREAMS or stdio Streams....
Reply With Quote
  #5  
Old 12-02-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by fsahog View Post
You can email me direct email address removed if you want a more rapid dialogue.
Please read the rules. No email replies are encouraged.
Reply With Quote
  #6  
Old 12-03-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,263
Perhaps this is THE BOOK for you:

Andrew S. Tanenbaum
Operating Systems Design and Implementation
Prentice Hall, 1997
ISBN-10: 0136386776
ISBN-13: 978-0136386773

If you can't find answers to your questions in there you won't probably find any answers at all. In my copy (2 vol german translation, Hanser, 1990) there is the complete and commented source code for Minix included.

bakunin
Reply With Quote
  #7  
Old 12-03-2007
Registered User
 

Join Date: Apr 2006
Location: Northern Virginia
Posts: 23
Follow up

Agreed to Porter, and apologies to the moderator on the email thing.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0