Why does getpid() return pid_t instead of int?
Solution 1:
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).
Solution 2:
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.