C语言 为什么 getpid() 返回 pid_t 而不是 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7378854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does getpid() return pid_t instead of int?
提问by ntl0ve
What's the logic behind calls like getpid()returning a value of typepid_tinstead of an unsigned int? Or int? How does this help?
诸如getpid()返回类型值pid_t而不是类型值之类的调用背后的逻辑是unsigned int什么?或者int?这有什么帮助?
I'm guessing this has to do with portability? Guaranteeing that pid_tis the same size across different platforms that may have different sizes of ints etc.?
我猜这与便携性有关?保证pid_t不同平台的大小相同,可能有不同大小的ints 等?
采纳答案by zvrba
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).
我认为这是相反的:使程序可跨平台移植,无论例如 PID 是 16 位还是 32 位(甚至更长)。
回答by R.. GitHub STOP HELPING ICE
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:
当然,现代系统希望 pids 至少为 32 位,但如果标准要求:
int getpid(void);
then all historical implementations that had used shortwould become non-conformant. This was deemed unacceptable, so pid_twas created and the implementation was allowed to define pid_twhichever way it prefers.
那么所有使用过的历史实现short都会变得不一致。这被认为是不可接受的,因此pid_t创建并允许实现定义pid_t它喜欢的任何方式。
Note that you are by no means obligated to use pid_tin your own code as long as you use a type that's large enough to store any pid (intmax_tfor example would work just fine). The only reason pid_tneeds toexist is for the standard to define getpid, waitpid, etc. in terms of it.
请注意,pid_t只要您使用的类型足够大以存储任何 pid(intmax_t例如可以正常工作),您就没有义务在自己的代码中使用。唯一的原因,pid_t需要存在是一个标准的定义getpid,waitpid等在它的条款。
回答by Maz
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.
在不同的平台和操作系统上,不同的类型(例如 pid_t)在 32 位机器上可能是 32 位(unsigned int),在 64 位机器上可能是 64 位(unsigned long)。或者,出于某些其他原因,操作系统可能会选择不同的大小。此外,在阅读代码时,它清楚地表明该变量代表一个“对象”,而不仅仅是一个任意数字。
回答by Justin Time - Reinstate Monica
The purpose of it is to make pid_t, or any other type of the sort, platform-independent, such that it works properly regardless of how it's actually implemented. This practice is used for any type that needs to be platform-independent, such as:
它的目的是使pid_t或任何其他类型的类型与平台无关,这样无论它是如何实际实现的,它都能正常工作。这种做法适用于任何需要平台无关的类型,例如:
pid_t: Has to be large enough to store a PID on the system you're coding for. Maps tointas far as I'm aware, although I'm not the most familiar with the GNU C library.size_t: Anunsignedvariable able to store the result of thesizeofoperator. Generally equal in size to the word size of the system you're coding for.int16_t(intX_t): Has to be exactly 16 bits, regardless of platform, and won't be defined on platforms that don't use 2n-bit bytes (typically 8- or 16-bit) or, much less frequently, provide a means of accessing exactly 16 bits out of a larger type (e.g., the PDP-10's "bytes", which could be any number of contiguous bits out of a 36-bit word, and thus could be exactly 16 bits), and thus don't support 16-bit two's complement integer types (such as a 36-bit system). Generally maps toshorton modern computers, although it may be aninton older ones.int_least32_t(int_leastX_t): Has to be the smallest size possible that can store at least 32 bits, such as 36 bits on a 36-bit or 72-bit system. Generally maps tointon modern computers, although it may be alongon older ones.int_fastX_t: Has to be the fastest type possible that can store at least X bits. Generally, it's the system's word size if(X <= word_size)(or sometimescharforint_fast8_t), or acts likeint_leastX_tif(X > word_size))intmax_t: Has to be the maximum integer width supported by the system. Generally, it'll be at least 64 bits on modern systems, although some systems may support extended types larger thanlong long(and if so,intmax_tis required to be the largest of those types).- And more...
pid_t:必须足够大才能在您编码的系统上存储 PID。int就我所知映射到,尽管我不是最熟悉 GNU C 库的人。size_t: 一个unsigned能够存储sizeof运算符结果的变量。通常大小等于您正在编码的系统的字大小。int16_t(intX_t): 必须恰好是 16 位,无论平台如何,并且不会在不使用 2 n位字节(通常为 8 位或 16 位)的平台上定义,或者在不太频繁的情况下提供一种方法访问较大类型中的 16 位(例如,PDP-10 的“字节”,可以是 36 位字中的任意数量的连续位,因此可以是 16 位),因此不t 支持 16 位二进制补码整数类型(如 36 位系统)。通常映射到short现代计算机上,尽管它可能是int旧计算机上的映射。int_least32_t(int_leastX_t):必须是可以存储至少 32 位的最小大小,例如 36 位或 72 位系统上的 36 位。通常映射到int现代计算机上,尽管它可能是long旧计算机上的。int_fastX_t: 必须是最快的类型,可以存储至少 X 位。通常,它是系统的字长 if(X <= word_size)(或有时char为int_fast8_t),或行为类似于int_leastX_tif(X > word_size))intmax_t: 必须是系统支持的最大整数宽度。通常,它在现代系统上至少为 64 位,尽管某些系统可能支持大于long long(如果是,intmax_t则要求是这些类型中最大的)的扩展类型。- 和更多...
Mechanically, it allows the compiler's installer to typedefthe appropriate type to the identifier (whether a standard type or an awkwardly-named internal type) behind the scenes, whether by creating appropriate header files, coding it into the compiler's executable, or some other method. For example, on a 32-bit system, Microsoft Visual Studio will implement the intX_tand similar types as follows (note: comments added by me):
从机制上讲,它允许编译器的安装程序typedef在幕后为标识符(无论是标准类型还是命名笨拙的内部类型)指定适当的类型,无论是通过创建适当的头文件、将其编码到编译器的可执行文件中,还是通过其他方法。例如,在32位系统上,Microsoft Visual Studio 将实现intX_t和类似的类型如下(注:我添加的注释):
// Signed ints of exactly X bits.
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
// Unsigned ints of exactly X bits.
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
// Signed ints of at least X bits.
typedef signed char int_least8_t;
typedef short int_least16_t;
typedef int int_least32_t;
// Unsigned ints of at least X bits.
typedef unsigned char uint_least8_t;
typedef unsigned short uint_least16_t;
typedef unsigned int uint_least32_t;
// Speed-optimised signed ints of at least X bits.
// Note that int_fast16_t and int_fast32_t are both 32 bits, as a 32-bit processor will generally operate on a full word faster than a half-word.
typedef char int_fast8_t;
typedef int int_fast16_t;
typedef int int_fast32_t;
// Speed-optimised unsigned ints of at least X bits.
typedef unsigned char uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
typedef _Longlong int64_t;
typedef _ULonglong uint64_t;
typedef _Longlong int_least64_t;
typedef _ULonglong uint_least64_t;
typedef _Longlong int_fast64_t;
typedef _ULonglong uint_fast64_t;
On a 64-bit system, however, they may not necessarily be implemented the same way, and I can guarantee that they won't be implemented the same way on an archaic 16-bit system, assuming you can find a version of MSVS compatible with one.
但是,在 64 位系统上,它们可能不一定以相同的方式实现,并且我可以保证它们不会在过时的 16 位系统上以相同的方式实现,假设您可以找到与 MSVS 兼容的版本与一个。
Overall, it allows code to work properly regardless of the specifics of your implementation, and to meet the same requirements on any standards-compatible system (e.g. pid_tcan be guaranteed to be large enough to hold any valid PID on the system in question, no matter what system you're coding for). It also prevents you from having to know the nitty-gritty, and from having to look up internal names you may not be familiar with. In short, it makes sure your code works the same regardless of whether pid_t(or any other similar typedef) is implemented as an int, a short, a long, a long long, or even a __Did_you_really_just_dare_me_to_eat_my_left_shoe__, so you don't have to.
总体而言,它允许代码正常工作,而不管您的实现细节如何,并满足任何标准兼容系统的相同要求(例如,pid_t可以保证足够大以容纳相关系统上的任何有效 PID,无论您正在为什么系统编码)。它还使您不必了解细节,也不必查找您可能不熟悉的内部名称。简而言之,无论pid_t(或任何其他类似的 typedef)是否实现为 an int、a short、a long、along long甚至 a __Did_you_really_just_dare_me_to_eat_my_left_shoe__,它都能确保您的代码工作相同,因此您不必这样做。
Additionally, it serves as a form of documentation, allowing you to tell what a given variable is for at a glance. Consider the following:
此外,它作为一种文档形式,使您可以一目了然地了解给定变量的用途。考虑以下:
int a, b;
....
if (a > b) {
// Nothing wrong here, right? They're both ints.
}
Now, let's try that again:
现在,让我们再试一次:
size_t a;
pid_t b;
...
if (a > b) {
// Why are we comparing sizes to PIDs? We probably messed up somewhere.
}
If used as such, it can help you locate potentially problematic segments of code before anything breaks, and can make troubleshooting much easier than it would otherwise be.
如果这样使用,它可以帮助您在任何问题发生之前定位潜在的有问题的代码段,并且可以比其他方式更容易地进行故障排除。
回答by Justin Time - Reinstate Monica
Each process in the program has a specific process ID. By calling pid, we know the assigned ID of the current process.Knowing the pid is exceptionally important when we use fork(), because it returns 0and !=0values for child and parent copies receptively.These two videos have clear explanations: video#1Video#2
程序中的每个进程都有一个特定的进程 ID。通过调用PID,我们知道当前process.Knowing当我们使用pid是非常重要的分配ID fork(),因为它返回0,并!=0为孩子和家长的值复制receptively.These两个视频有明确的解释:视频#1视频#2
An example: Suppose we have the following c program:
一个例子:假设我们有以下 c 程序:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
printf("I am %d\n", (int) getpid());
pid_t pid = fork();
printf("fork returned: %d\n", (int) pid);
if(pid<0){
perror("fork failed");
}
if (pid==0){
printf("This is a child with pid %d\n",(int) getpid());
}else if(pid >0){
printf("This is a parent with pid %d\n",(int)getpid());
}
return 0;
}
If you run it, you will get 0for child and non zero/greater than zerofor the parent.
如果你运行它,你会得到0孩子,而不是zero/greater than zero父母。
回答by 0xcurb
One thing to point out, in most answers I saw something along the lines of "using pid_t makes the code work on different systems", which is not necessarily true.
需要指出的一件事,在大多数答案中,我看到了 “使用 pid_t 使代码在不同系统上工作”的内容,这不一定是正确的。
I believe the precise wording should be: it makes the code 'compile' on different systems.
我相信确切的措辞应该是:它使代码在不同的系统上“编译”。
As, for instance, compiling the code on a system that uses 32-bit pid_t will produce a binary that will probablybreak if run on another system that uses 64-bit pid_t.
例如,在使用 32 位 pid_t 的系统上编译代码将生成一个二进制文件,如果在使用 64 位 pid_t 的另一个系统上运行,该二进制文件可能会中断。

