LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-13-2023, 12:27 PM   #1
jt1122
Member
 
Registered: Apr 2021
Posts: 140

Rep: Reputation: Disabled
file descriptors


Hi

I'm using debian with xfce4-terminal.

Code:
ls /dev/fd
lr-x------ 1 pc pc 64 2023-12-13 10:21 3 -> /proc/94877/fd                                                                         
lrwx------ 1 pc pc 64 2023-12-13 10:21 0 -> /dev/pts/11                                                                            
l-wx------ 1 pc pc 64 2023-12-13 10:21 1 -> pipe:[19194926]                                                                        
lrwx------ 1 pc pc 64 2023-12-13 10:21 2 -> /dev/pts/11
1. why isn't fd 1 pointing to /dev/pts/11 and instead pointing to a pipe?.
2. if I close fd 3 the terminal closes - why?.

I didn't "open" fd 3 manually for reading/writing so it's not clear why is it even enabled.

Thanks
 
Old 12-13-2023, 08:05 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,329
Blog Entries: 28

Rep: Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142Reputation: 6142
Because these are not files as such: https://www.baeldung.com/linux/dev-directory
 
Old 12-14-2023, 12:32 AM   #3
lvm_
Member
 
Registered: Jul 2020
Posts: 933

Rep: Reputation: 337Reputation: 337Reputation: 337Reputation: 337
The answer which is perfectly correct and yet totally irrelevant... On my system (kubuntu) there is no such nonsense:

Code:
lrwx------ 1 x x 64 Dec 14 08:54 0 -> /dev/pts/12
lrwx------ 1 x x 64 Dec 14 08:54 1 -> /dev/pts/12
lrwx------ 1 x x 64 Dec 14 08:54 2 -> /dev/pts/12
lrwx------ 1 x x 64 Dec 14 08:54 255 -> /dev/pts/12
You may find processes linked by this pipe in /proc e.g. like this: find /proc -lname 'pipe:\[12345\]'
 
Old 12-14-2023, 01:35 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by jt1122 View Post
Hi

I'm using debian with xfce4-terminal.

Code:
ls /dev/fd
lr-x------ 1 pc pc 64 2023-12-13 10:21 3 -> /proc/94877/fd                                                                         
lrwx------ 1 pc pc 64 2023-12-13 10:21 0 -> /dev/pts/11                                                                            
l-wx------ 1 pc pc 64 2023-12-13 10:21 1 -> pipe:[19194926]                                                                        
lrwx------ 1 pc pc 64 2023-12-13 10:21 2 -> /dev/pts/11
1. why isn't fd 1 pointing to /dev/pts/11 and instead pointing to a pipe?.
2. if I close fd 3 the terminal closes - why?.

I didn't "open" fd 3 manually for reading/writing so it's not clear why is it even enabled.

Thanks
the first 3 file descriptors are by default inherited from the parent process (recursively), in your cases it is most probably your terminal, which is connected to /dev/pts/11.
Based on your post fd=0 is still inherited, fd=1 is redirected into a pipe, fd=2 is still the original one.
The last one, fd=3 is usually belongs to the "current process". In your case that is the command ls. It opened /dev/fd (as you specified), which is a symbolic link to /proc/self/fd, which is in turn a hard link to /proc/<pid>/fd. That is what you see as 3 -> /proc/94877/fd, ls opened that dir to list its content.
Additionally fd=3 belongs to the process ls, not to the shell. The current shell may have also a fd=3 opened, but we don't know (depends on how did you start/use it). Closing it probably terminates your shell and at the end that will also force close its parent process, the terminal itself.
 
Old 12-14-2023, 03:06 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The pathname /dev/fd appears to be global, in common to all processes.
If you find things in it that are specific to a certain process then examine the parent directory
Code:
ls -ld /dev/fd
Certainly it is a link to /proc/self/fd, as was stated in the previous post.

"self" points to the current process.
Code:
ls -ld /proc/self
ls -ld /proc/self
Likely the "self" points to the ls process, but might also point to the current(invoking) shell. (It depends on the implementation in the kernel.)
Use the shell-internal cd command to safely examine the shell itself:
Code:
echo "My pid is $$"
cd /proc/self
/bin/pwd
ls -l fd
The "cd" has fixed the "self". Compare with
Code:
ls -l /proc/self/fd
 
Old 12-14-2023, 03:19 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Code:
$ echo $$
17324
$ cd /proc/self
$ pwd
/proc/self
$ /bin/pwd
/proc/17324
$ readlink -f .
/proc/17324
$ file /proc/self
/proc/self: symbolic link to 39367
$ file /proc/self
/proc/self: symbolic link to 49918
$ file /proc/self
/proc/self: symbolic link to 50956
You might say it is interesting, file itself (and ls) see a different symlink every time, their own /proc/self symlink. /proc is a virtual filesystem and also dynamically managed by kernel.
 
Old 12-14-2023, 08:10 AM   #7
jt1122
Member
 
Registered: Apr 2021
Posts: 140

Original Poster
Rep: Reputation: Disabled
Thanks for the replies.

But why is fd 1 (=standard output) symlinked to a process?.
 
Old 12-14-2023, 08:26 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
that is not symlinked, but piped to somewhere. It depends on how did you start your terminal and shell, what is running inside.
here is a link to check what is using that pipe: https://stackoverflow.com/questions/...a-bash-command
 
  


Reply



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
reading and writing to pipes, file descriptors, and file streams cmartin0 Programming 1 02-13-2012 03:03 AM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 07:02 AM
File Descriptors - How to raise the limit? xmdms Linux - General 3 07-15-2004 12:57 AM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM
File descriptors odd question ferreter Linux - General 2 02-13-2003 10:21 AM

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

All times are GMT -5. The time now is 02:00 AM.

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
Open Source Consulting | Domain Registration