Why does getpid() return pid_t instead of int?

后端 未结 6 1079
太阳男子
太阳男子 2020-12-23 19:37

What\'s the logic behind calls like getpid() returning a value of type pid_t instead of an unsigned int? Or int? How does

6条回答
  •  心在旅途
    2020-12-23 20:04

    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.

提交回复
热议问题