File Descriptor Table


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users File Descriptor Table
# 1  
Old 12-02-2007
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
# 2  
Old 12-02-2007
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 09:56 PM..
# 3  
Old 12-02-2007
Not sure we're answering your question..

Quote:
Originally Posted by Ashaman0
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-03-2007 at 12:14 AM.. Reason: email address removed
# 4  
Old 12-02-2007
Quote:
Originally Posted by fsahog
Streams must necessarily have a file descriptor as one of their attributes.
Depends if you are referering to Sys V STREAMS or stdio Streams....
# 5  
Old 12-03-2007
Quote:
Originally Posted by fsahog
You can email me direct email address removed if you want a more rapid dialogue.
Please read the rules. No email replies are encouraged.
# 6  
Old 12-03-2007
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
# 7  
Old 12-03-2007
Follow up

Agreed to Porter, and apologies to the moderator on the email thing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with File Descriptor in a While loop

Hi, I am trying to read a file line-by-line in a while loop, and perform some tasks which involves non-interactive SSH to a remote server. The code looks something like this -- #!/usr/bin/ksh export myFile=/path/to/my/file.load while read line do do something ## Adding the SSH... (2 Replies)
Discussion started by: Subu1987
2 Replies

2. Shell Programming and Scripting

file descriptor KSH

Hello, How can i use file descriptor in a script to read 2 files at the same time and extract line 200 from file 1 and line 500 from file 2. Thanks. (6 Replies)
Discussion started by: LiorAmitai
6 Replies

3. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

4. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

5. Shell Programming and Scripting

Passing a file descriptor

I am trying to right a function which uses a file descriptor to write to a log file. The problem is that the on the print statement the file descriptor is called bad. Now when I first open the file and print to it in the f_open function by passing the descriptor to f_print_log all works well,... (6 Replies)
Discussion started by: robotball
6 Replies

6. Programming

File descriptor constant

I have a requirement to close all the file descriptors from 3 to 1024 for a particular application. Right now, this is how I do it .. for ( int i = 3 ; i <= 1024; ++i ) close(i); The change I am looking at is, I want to do away with the number 1024 and replace it with a constant which... (4 Replies)
Discussion started by: vino
4 Replies

7. Programming

Problems with file descriptor

Hi, look at the following code: The client after estabilishing a connection with the server does the following: if ((peter = fopen(argv, "r")) == NULL){ printf("errore\n"); exit(0); } ... (11 Replies)
Discussion started by: teo
11 Replies

8. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

9. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies

10. UNIX for Dummies Questions & Answers

bad file descriptor?

Ok, I'm sure this is a total newbie question, but I think I'm in the right place, no? I'm trying to call a perl module from a cgi script - Mail::Sendmail - and my web host installed the module in a directory that doesn't seem to be accessible, at least not the way I'm trying. But I thought you... (1 Reply)
Discussion started by: ftb
1 Replies
Login or Register to Ask a Question