Home

getpid

Getpid is a POSIX-compliant library function that returns the process identifier (PID) of the calling process. It is commonly used in C and other languages that provide POSIX interfaces. The function is declared in unistd.h and returns a value of type pid_t, a type defined in sys/types.h.

The PID is a unique identifier assigned by the kernel to each active process within the system.

Threads and PID semantics: All threads within a single process share the same PID, since they belong

Usage example in C: pid_t pid = getpid(); printf("PID: %d\n", (int)pid);

Portability and related functions: getpid is part of the POSIX standard, and equivalents or wrappers exist

See also: getppid (parent process ID), fork (process creation), pid_t, unistd.h, sys/types.h.

The
value
is
valid
for
the
lifetime
of
the
process.
When
a
new
process
is
created
via
fork,
the
child
receives
a
new,
distinct
PID,
while
the
parent
retains
its
original
PID.
After
a
process
terminates,
its
PID
may
be
reused
by
the
system
for
new
processes.
to
the
same
process.
If
a
program
needs
to
distinguish
individual
threads,
other
identifiers
such
as
thread
IDs
can
be
used
(for
example,
gettid
on
Linux
or
pthread
identifiers
through
the
pthread
API).
in
many
languages
(for
example,
Python’s
os.getpid()).
In
contrast,
Windows
uses
a
different
API
(for
example,
GetCurrentProcessId)
to
obtain
the
process
ID.