I'm having trouble finding them, and how to use them in general. for example i see in x86 functions, the output may be a file descriptor. i cant seem to find much about them on the web so im trying it here. and yes im very new to linux
CodePudding user response:
A file descriptor is basically just an integer. It gets returned by certain functions, e.g. open().
It is used very similar to a pointer in C, you don't do much with it except pass it to other functions, e.g. read(). I don't think doing pointer arithmetic on file handles makes that much sense, though. The fstat() system call will return you a bunch of information about the file. See the manpage:
man 2 fstat
Each process has its own list of file handles, the numbering always starts at 0 for STDIN, 1 for STDOUT and 2 for STDERR, then continues simply counting up as you open and close files.
Except for the first three, that number has no deeper meaning, it just tells the kernel which one from its list of open files it should operate on. The concept seems to be so simple that nobody has bothered to document it... ;-)
CodePudding user response:
The list of file descriptors is:
ls -l /proc/self/fd
Every process has its own list.
The following script will print all file descriptors of all processes.
#! /bin/bash
find /proc -maxdepth 1 -type d -regex /proc/[0-9] -printf '%P\n' |
{
while read -r pid; do
if [[ -d /proc/$pid ]]; then
printf '%d:' "$pid"
find /proc/$pid/fd -type l -printf ' %P' 2>/dev/null
printf '\n'
fi
done
}
You need root permissions to execute it.
