Getting Process Own Pid in C and C++
Every running process gets a unique identifier from the operating system called a PID (Process ID). You’ll need this when managing processes, logging, creating lock files, or coordinating between parent and child processes.
The POSIX standard approach
The simplest way is getpid() from unistd.h:
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = getpid();
printf("My PID is: %d\n", pid);
return 0;
}
Compile and run:
gcc -o get_pid get_pid.c
./get_pid
# Output: My PID is: 12345
The pid_t type is a signed integer (typically int on modern systems) that holds the process ID. getpid() is POSIX-compliant and works on Linux, BSD, macOS, and other Unix-like systems.
Getting the parent process ID
Use getppid() to retrieve your parent’s PID — useful when you need to signal or track the calling process:
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t my_pid = getpid();
pid_t parent_pid = getppid();
printf("My PID: %d, Parent PID: %d\n", my_pid, parent_pid);
return 0;
}
In C++
C++ doesn’t provide a standard library wrapper for getpid(), so you still use the C function directly:
#include <unistd.h>
#include <iostream>
int main() {
pid_t pid = getpid();
std::cout << "Process ID: " << pid << std::endl;
return 0;
}
For thread identification (different from process ID), use std::this_thread::get_id():
#include <iostream>
#include <thread>
int main() {
auto thread_id = std::this_thread::get_id();
std::cout << "Thread ID: " << thread_id << std::endl;
return 0;
}
Note that std::this_thread::get_id() returns a thread-local identifier, not a process-wide PID. If you need the actual process ID, stick with getpid().
Practical use cases
Creating unique lock files:
#include <unistd.h>
#include <stdio.h>
int main() {
pid_t pid = getpid();
char lock_file[64];
snprintf(lock_file, sizeof(lock_file), "/tmp/myapp.%d.lock", pid);
printf("Lock file: %s\n", lock_file);
return 0;
}
Logging with process context:
#include <unistd.h>
#include <stdio.h>
#include <time.h>
void log_message(const char *msg) {
time_t now = time(NULL);
printf("[%ld] [PID %d] %s\n", now, getpid(), msg);
}
int main() {
log_message("Application started");
return 0;
}
Parent-child coordination:
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
int main() {
pid_t child = fork();
if (child == 0) {
printf("Child process, PID: %d, Parent: %d\n", getpid(), getppid());
} else if (child > 0) {
printf("Parent process, PID: %d, Child: %d\n", getpid(), child);
wait(NULL);
}
return 0;
}
Cross-platform considerations
On Linux and Unix systems, getpid() is always available and reliable. Windows doesn’t have POSIX parity here — GetCurrentProcessId() from windows.h is the equivalent. For truly cross-platform code, consider wrapping the platform-specific calls or using a library like Boost.Process.
The PID value wraps around after reaching a system-defined maximum (typically 2^22 on Linux), so don’t rely on PIDs being globally unique over the lifetime of the system.
Please update the reuslt section with actual result.
$ gcc getpid.c -o s && ./s
ppid: 7108
But printf is
printf(“pid: %lun”, pid);
How come it prints “ppid” for print statement of “pid”.
Hello, thanks for the idea but getpid() returns an int so the format in printf is abnormal : %lun should be just %d (and the n in lun is probably a typo)
the error :
error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 2 has type ‘pid_t’ {aka ‘int’}
x86_64 architecture, 64 bits