LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   file descriptors (https://www.linuxquestions.org/questions/linux-newbie-8/file-descriptors-4175731766/)

jt1122 12-13-2023 12:27 PM

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

frankbell 12-13-2023 08:05 PM

Because these are not files as such: https://www.baeldung.com/linux/dev-directory

lvm_ 12-14-2023 12:32 AM

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\]'

pan64 12-14-2023 01:35 AM

Quote:

Originally Posted by jt1122 (Post 6470212)
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.

MadeInGermany 12-14-2023 03:06 AM

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

pan64 12-14-2023 03:19 AM

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.

jt1122 12-14-2023 08:10 AM

Thanks for the replies.

But why is fd 1 (=standard output) symlinked to a process?.

pan64 12-14-2023 08:26 AM

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


All times are GMT -5. The time now is 03:18 PM.