Processes Have IDs

Every process running on your system has a unique process identifier, hereby referred to as ‘pid’.

The pid doesn’t say anything about the process itself, it’s simply a sequential numeric label. This is how the kernel sees your process: as a number.

Here’s how we can inspect the current pid in a ruby program. Fire up irb and try this:

# This line will print the pid of the current ruby process. This might be an 
# irb process, a rake process, a rails server, or just a plain ruby script.

A pid is a simple, generic representation of a process. Since it’s not tied to any aspect of the content of the process it can be understood from any programming language and with simple tools. We’ll see below how we can use the pid to trace the process details using different utilities.

Cross Referencing

To get a full picture, we can use ps(1) to cross-reference our pid with what the kernel is seeing. Leaving your irb session open run the following command at a terminal:

$ ps -p <pid-of-irb-process>

That command should show a process called ‘irb’ with a pid matching what was printed in the irb session.

In the Real World

Just knowing the pid isn’t all that useful in itself. So where is it used?

A common place you’ll find pids in the real world is in log files. When you have multiple processes logging to one file it’s imperative that you’re able to tell which log line comes from which process. Including the pid in each line solves that problem.

Including the pid also allows you to cross reference information with the OS, through the use of commands like top(1) or lsof(8). Here’s some sample output from the Spyglass server booting up. The first square brackets of each line denote the pid where the log line is coming from.

[58550] [Spyglass::Server] Listening on port 4545
[58550] [Spyglass::Lookout] Received incoming connection
[58557] [Spyglass::Master] Loaded the app
[58557] [Spyglass::Master] Spawned 4 workers. Babysitting now...
[58558] [Spyglass::Worker] Received connection

System Calls

Ruby’s maps to getpid(2).

There is also a global variable that holds the value of the current pid. You can access it with $$.

Ruby inherits this behaviour from other languages before it (both Perl and bash support $$), however I avoid it when possible. Typing out in full is much more expressive of your intent than the dollar-dollar variable, and less likely to confuse those who haven't seen the dollar-dollar before.