To get a (new) file descriptor to do
anything with you must first call open()
*, which takes a filename. So yes, to open a file, you need a filename; the act of opening a file creates a file descriptor to represent that, the same way that running a program creates a new process ID to represent that.
Files are rather different from processes though, in that the PID is fundamental, and systemwide. The filename a process was ran with really isn't relevant since
a) If you have 4 xterms doing different things, you can't tell them apart by what file you ran
b) Lots of processes clone themselves, instead of loading new programs
So the PID is really the
only way to identify processes. Commands like killall just trawl through the system's big list of PID's for you.
It's the files themselves that're unusual, and the ways you can use them, not the ways they're opened. There's several kinds of files:
- Ordinary files, just blocks of data like you'd expect.
- Symbolic links, a kind of special file that leads to a different file or directory sort of like a hyperlink leads to a website.
- Hard links, where the same file is literally in two entirely seperate places. They have to be on the same partition, and share the same inode.
- Named pipes aka FIFOs, where data written into the file by one process just gets pumped into whatever process is reading it.
- Domain sockets, a kind of beefed-up version of named pipes that adds things like the ability for one server to talk to many clients over it.
- Device files, which literally represent some hardware device or system-provided data source. 'dd if=/dev/random of=/dev/dsp' will read random data from the kernel and hurl it into your sound device on Linux systems with OSS compatibility...
You can also use FD's that have already been opened for you. The FD's 0, 1, and 2 are usually provided for you, where 0 represents the input stream(keyboard, for a console), 1 represents the output stream(the console display, for a console), and 2 represents error message output(also the console display). You can rearrange them how you please, having input coming from a file instead of console and having output going into a file or into another process etc etc etc.
* Okay, socket() and pipe() make FDs too, but have nothing to do with files on disk.