LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-02-2017, 03:15 AM   #1
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Rep: Reputation: Disabled
Lightbulb Are file descriptor integers unique system-wide?


After spending all night trying to figure out how file descriptors work, I'm beginning to understand (does anyone else think this is a hard topic to grasp?), I just have one thing i need to help clairfy. Ok so am I right to say that these file descriptors are not necessarily a unique integer for every file, but that try to be by assigning the lowest avaliable integer to a process (besides fd0, fd1, and fd2 as their standard and thus always the same integer). And that the only way that these descriptors portray "uniqueness" is on a per-process-basis. For example a call to the open() system call will give p1 fd3, even though p2 already has fd3 open, because the open() sys call assigns the lowest avaliable fd integer. But if say p1 already had fd3 assigned, then it that case it would have been given fd4 instead, as that would of been the next lowest avalaible fd integer not currently open for that process... To reframe am I wrong and there is in fact fd's integers that cannnot be duplicated? (which would make the reasoning in my post invalid) Is there any point in learning this btw, will it make me a better programmer? Edit: Just learned that fd's are useful when using strace as you can see what config files a program does without having to read the docs (and to learn what it's doing)

Last edited by linux4evr5581; 01-02-2017 at 12:12 PM.
 
Old 01-02-2017, 09:23 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Calls such as fopen() return a pointer to a FILE structure in user-space memory. See man 3 fopen.

Any low-level nonces that might be used to refer to an operating-system resource are concealed within that structure and, like the pointer itself, should be regarded as "unpredictable" since Linux runs on many different platforms. (As does libc, for that matter.)

It is most advisable, therefore, to treat any returned value as a nonce: as an unpredictable value having no intrinsic meaning that will "do the job for you," but about which no other assumptions should be made. "Q: How are these values obtained?" "A: It's Magic.™ Don't ask questions, or you might be turned into a Windows user frog."

Last edited by sundialsvcs; 01-02-2017 at 09:29 AM.
 
Old 01-02-2017, 09:30 AM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,652

Rep: Reputation: 661Reputation: 661Reputation: 661Reputation: 661Reputation: 661Reputation: 661
the file descriptors are a system thing and for normal programming you dont need to worry about them, obviously checking for valid descriptors before using them, the only time i would imagine you needing to knownmore about them, how they are assigned etc is if you are doing kernel or init programming, i have been programming i linux for years and never had to worry about them or learn any more about where they come from how they are assigned etc etc, unless you intend to hack the kernel i wouldn't waste too much time on them
 
Old 01-02-2017, 01:00 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,047

Rep: Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798Reputation: 1798
The file descriptor numbers (the value returned by an open() call) are per-process. A file descriptor number in one process has absolutly nothing to do with that same file descriptor number in a different process.
 
Old 01-02-2017, 01:26 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Basically, there's a "layer of software" (in your process) that maintains the file-connections of your process in a set of FILE structures. The exact location of this structure in-memory is both unpredictable and meaningless. Somewhere within that structure will be an actual corresponding operating-system token of some kind that is used when glibc issues physical I/O requests.

All of this should be considered "opaque." You cannot and should not make any assumptions about what these values might be. The operating system can do as it damn well pleases.

(And I happen to know that some "hardened" operating systems deliberately obfuscate these "magic handles" to make it even more difficult to perceive what the operating system is actually doing.)

Last edited by sundialsvcs; 01-02-2017 at 01:28 PM.
 
Old 01-02-2017, 01:40 PM   #6
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Ok so i guess this works by Stdio being a layer of abstraction layered over systems calls that allow for more funtionality such as portability due to it being high-level, and possibly improved security... And fopen() utilizes this stdio interface but still needs to call the open() system call before it can provide buffered I/O... The handle in this case is not a discriptor but it's FILE* which is a pointer to the C structure, which is stdin, sdout, and stderr. But the descriptor is wrapped in FILE*, which I guess gets used by fopen() when it does interact with open()...

Last edited by linux4evr5581; 01-02-2017 at 02:20 PM.
 
Old 01-02-2017, 01:43 PM   #7
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rknichols View Post
The file descriptor numbers (the value returned by an open() call) are per-process. A file descriptor number in one process has absolutly nothing to do with that same file descriptor number in a different process.
Yeah or socket() which I guess are the only two system calls that initiate a discriptor?

Last edited by linux4evr5581; 01-02-2017 at 01:46 PM.
 
Old 01-02-2017, 01:45 PM   #8
linux4evr5581
Member
 
Registered: Sep 2016
Location: USA
Posts: 275

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Keith Hedger View Post
the file descriptors are a system thing and for normal programming you dont need to worry about them, obviously checking for valid descriptors before using them, the only time i would imagine you needing to knownmore about them, how they are assigned etc is if you are doing kernel or init programming, i have been programming i linux for years and never had to worry about them or learn any more about where they come from how they are assigned etc etc, unless you intend to hack the kernel i wouldn't waste too much time on them
Thats funny because I found out about them from Julia Evans on youtube, a kernel hacker lol

Last edited by linux4evr5581; 01-02-2017 at 01:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unique combinations from range of integers astrogeek Programming 13 10-22-2015 04:17 PM
System wide .bashrc file - bad idea? ForumUser2 Linux - Newbie 3 08-17-2012 11:00 PM
A system wide .exrc file. swamprat Linux - Software 1 04-26-2012 08:53 PM
[SOLVED] Partition Proxy: System Wide File Read/Write Redirection steampunk Linux - Newbie 5 01-24-2012 06:19 PM
Problems with system-wide file permissions hexadevil Linux - Newbie 2 05-30-2004 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration