Why does getpid() return pid_t instead of int?
I think it's the opposite: making the program portable across platforms, regardless of whether, e.g., a PID is 16 or 32 bits (or even longer).
The reason is to allow nasty historical implementations to still be conformant. Suppose your historical implementation had (rather common):
short getpid(void);
Of course modern systems want pids to be at least 32-bit, but if the standard mandated:
int getpid(void);
then all historical implementations that had used short
would become non-conformant. This was deemed unacceptable, so pid_t
was created and the implementation was allowed to define pid_t
whichever way it prefers.
Note that you are by no means obligated to use pid_t
in your own code as long as you use a type that's large enough to store any pid (intmax_t
for example would work just fine). The only reason pid_t
needs to exist is for the standard to define getpid
, waitpid
, etc. in terms of it.
On different platforms and operating systems, different types (pid_t for example) might be 32 bits (unsigned int) on a 32-bit machine or 64 bits (unsigned long) on a 64-bit machine. Or, for some other reason, an operating system might choose to have a different size. Additionally, it makes it clear when reading the code that this variable represents an "object", rather than just an arbitrary number.