Sponsored Content
Full Discussion: File Descriptor Table
Top Forums UNIX for Advanced & Expert Users File Descriptor Table Post 302148521 by porter on Sunday 2nd of December 2007 08:49:25 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
DUP(2)								System Calls Manual							    DUP(2)

NAME
dup, dup2 - duplicate a descriptor SYNOPSIS
newd = dup(oldd) int newd, oldd; dup2(oldd, newd) int oldd, newd; DESCRIPTION
Dup duplicates an existing object descriptor. The argument oldd is a small non-negative integer index in the per-process descriptor table. The value must be less than the size of the table, which is returned by getdtablesize(2). The new descriptor returned by the call, newd, is the lowest numbered descriptor that is not currently in use by the process. The object referenced by the descriptor does not distinguish between references using oldd and newd in any way. Thus if newd and oldd are duplicate references to an open file, read(2), write(2) and lseek(2) calls all move a single pointer into the file, and append mode, non- blocking I/O and asynchronous I/O options are shared between the references. If a separate pointer into the file is desired, a different object reference to the file must be obtained by issuing an additional open(2) call. The close-on-exec flag on the new file descriptor is unset. In the second form of the call, the value of newd desired is specified. If this descriptor is already in use, the descriptor is first deallocated as if a close(2) call had been done first. RETURN VALUE
The value -1 is returned if an error occurs in either call. The external variable errno indicates the cause of the error. ERRORS
Dup and dup2 fail if: [EBADF] Oldd or newd is not a valid active descriptor [EMFILE] Too many descriptors are active. SEE ALSO
accept(2), open(2), close(2), fcntl(2), pipe(2), socket(2), socketpair(2), getdtablesize(2) 4th Berkeley Distribution May 13, 1986 DUP(2)
All times are GMT -4. The time now is 02:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy