The fd entry is a subdirectory that contains entries for the file descriptors opened by a process. Each entry is a symbolic link to the file or device opened on that file descriptor.
You can write to or read from these symbolic links; this writes to or reads from the
corresponding file or device opened in the target process. The entries in the fd subdirectory are named by the file descriptor numbers.
Open a new window, and find the process ID of the shell process by running ps.
$ ps
Now open a second window, and look at the contents of the fd subdirectory for that process.
$ ls -l /proc/PID/fd
File descriptors 0, 1, and 2 are initialized to standard input, output, and error, respectively.
Thus, by writing to /proc/PID/fd/1, you can write to the device attached to stdout for the shell process.
In the second window, try writing a message to that file:
$echo "Hello, world." >> /proc/PID/fd/1
The http://siber.cankaya.edu.tr/SystemsProgramming/cfiles/open-and-spin.cprogram in Fig. 10.6 presents a program that simply opens a file descriptor to a file specified on the command line and then loops forever.
Figure 10.6:
Open a File for Reading.
$ ./open-and-spin open-and-spin.c (in one window)
$ ls -l /proc/PID/fd (in other window)