OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Mar 03, 2024 6:11 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: are file descriptors really needed
PostPosted: Mon Dec 11, 2023 8:21 am 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
i was planning my os's file api and realised that i can probably get away with only having read and write functions in the api without having file descriptor tables in the process control block and functions for opening/closing files. i just need to know if there's something that i've missed here or if this is a perfectly reasonable design


Top
 Profile  
 
 Post subject: Re: are file descriptors really needed
PostPosted: Mon Dec 11, 2023 9:28 am 
Offline
Member
Member
User avatar

Joined: Fri Sep 03, 2021 5:20 pm
Posts: 91
What you are talking about is akin to applying the datagram concept to files. Reading and writing data to them ad hoc. However, that has several limitations, not only functionality wise, but also performance wise.

When you want to access a file, you need to search the drive for its structures and load/write the specific parts. If you need to do a lot of access to the files, you'll have a considerable overhead in repeating that search. With a file descriptor, you can keep an in memory reference to that file. File descriptors need not be just an integer like in many OSs, but they are important if you want performance.

Also if you need to somehow lock the file for your process to prevent other processes from messing it up while you use it, if you don't have a file descriptor referring to an open file, it will make your life much more difficult, as well as it will increase the chances of problems. Imagine you have the ability to lock the file system without the lock being tied to a specific process. If your program has a bug in it and forgets to unlock it explicitly or crashes before unlock it for some other reason, then it will remain locked. These mistakes may be difficult to track.

If, on the other hand, you have a file descriptor associated with your process, when the process ends (regardless of reason), the OS knows what your program was messing around with and will be able to release any locks that might still be tied to your process.

On the other hand, if your take is not about not using ANY descriptor but about not using an integer descriptor of some sort or similar, but using something like the file name, then not only you'll have a very inefficient lookup in the open files, but, since in most OSs the file can be renamed or moved from one directory to another while open (and this is a feature that I'd recommend you keep), depending on how you're organizing it the name will lose meaning as soon as that happens and you'll lose track of it anyway. This will also happen if the file location changes between operations if you don't have a file descriptor, as you won't be able to track that same file if you need to perform many operations, which is the most likely behavior to expect from an application.

_________________
Writing a bootloader in under 15 minutes: https://www.youtube.com/watch?v=0E0FKjvTA0M


Top
 Profile  
 
 Post subject: Re: are file descriptors really needed
PostPosted: Mon Dec 11, 2023 10:28 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1582
Another aspect is inheritance. File descriptors and file handles are trivially easy to inherit, so you can do things like
Code:
prog # outputs to screen
prog > file # outputs to file
Note that having this be this easy was one of the reasons for Unix winning out against Multics. Anyway, this works by having a "stdout" that is inherited from the shell, and so the shell can set it up to point to the terminal or to a file, or to a pipe, and the program doesn't have to know or care. If you do your read/write APIs only on file names, this will not be this simple.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group