getpid() — Linux system interface
Return the calling process identifier as visible in its PID namespace. Threads in the same process share this ID, which differs from a kernel thread ID (gettid). PID values can be reused after a process exits; they are not durable identities or security credentials. There is no error return. The example compares the number with Linux procfs rather than printing a machine-specific PID. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. POSIX getpid; the demonstration additionally requires procfs mounted for the caller’s PID namespace. These are libc-facing signatures, not raw architecture-specific syscall ABIs.
Formal Syntax & Structure
#include <unistd.h>pid_t getpid(void);Return Value
Current process PID; always succeeds.
Working Example
#define _DEFAULT_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <inttypes.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \ fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \ return EXIT_FAILURE; \} } while (0)
int main(void){ pid_t pid = getpid(); char expected[64], actual[64]; int n = snprintf(expected, sizeof expected, "%jd", (intmax_t)pid); CHECK(n > 0 && (size_t)n < sizeof expected); ssize_t got = readlink("/proc/self", actual, sizeof actual); CHECK(got == n && memcmp(expected, actual, (size_t)n) == 0); puts("PID matches /proc/self"); return EXIT_SUCCESS;}