NOTE:This blog had a good run, but is now in retirement.
If you enjoy the content here, please support Gregory's ongoing work on the Practicing Ruby journal.

USP: The Unix object model (as seen in procfs)

2011-09-17 03:32, written by Eric Wong

Eric Wong wrote:

If you’re a Rubyist, you may already be familiar with object-oriented designs. A running Unix/Unix-like kernel exposes two primary types of objects to userspace:

  1. Processes
  2. Open files

Now that we know what filesystems and virtual filesystems are, several systems have a procfs virtual filesystem mounted under /proc. I don’t believe it’s standardized anywhere between different systems, but your system probably supports it.

Among other things, procfs allows you to see the relationship between processes and their file descriptors on the filesystem.

You can inspect which file descriptors a particular process is using by looking in the “fd” subdirectory belonging to the PID belonging to the process. For example, my current mutt process has PID=19245 and file descriptors 0, 1, 2, and 4 open:

       /proc/19245/fd
       /proc/19245/fd/0
       /proc/19245/fd/1
       /proc/19245/fd/2
       /proc/19245/fd/4

Each PID has a directory in /proc/, and for each directory belonging to a particular PID, there is an “fd”

Utilities like ps(1) (are likely to) use information found in procfs to generate its output. Other information in procfs is less-consistent across different implementations, so consult your OS documentation.

License: GPLv3 (or later, at the discretion of Eric Wong)

blog comments powered by Disqus