What is a process ID (PID)
What you'll learn: How the operating system assigns a unique numerical identity to every running program.
The Social Security Number for programs
Imagine you are at a busy airport. Every passenger has a unique passport number. Even if two people share the same name—like "John Smith"—their passport numbers ensure the authorities don't mix them up.
In Linux and Unix-based systems, your programs work the same way. When you run a C program, the operating system loads it into memory and turns it into a process. Since you might be running five different instances of the same text editor or web browser simultaneously, the system cannot rely on the program's name to manage them. Instead, it assigns every process a Process ID, or PID.
A PID is simply a unique, non-negative integer. It remains constant for the entire life of that process. Once the process finishes, the OS eventually retires that number and may give it to a new program later.
Checking your own ID
In C, your program can "ask" the operating system what its own ID is by using a function called getpid(). This function is found in the <unistd.h> header.
#include <stdio.h>
#include <unistd.h>
int main() {
// pid_t is a special integer type for process IDs
pid_t my_pid = getpid();
printf("Hello! I am a running process.\n");
printf("My unique Process ID (PID) is: %d\n", my_pid);
return 0;
}
If you run this program twice, you will see two different numbers. Even though the code is identical, each execution creates a brand-new process with its own unique identity in the system's eyes.
Why PIDs matter
PIDs are the "handles" you use to communicate with the operating system. If you want to force a stuck program to close (using the kill command) or check how much memory a specific program is using, you need to know its PID. Without these IDs, the operating system would be like a teacher in a classroom where every student is named "Student"—it would be impossible to give a specific instruction to just one person.
Key takeaway
A PID is a unique integer assigned by the operating system to identify a specific instance of a running program.