gpt4 book ai didi

c - 进程的 PID 的最大可能长度是多少? (64 位)

转载 作者:太空狗 更新时间:2023-10-29 16:09:01 26 4
gpt4 key购买 nike

我一直在 AIX 上工作,看到一些正在运行的进程 ID 是 8 长....

我将 PID 用作与另一个 char* 值的串联,并且我正在尝试计算出如果您从“char 角度”来看 PID 的最大可能长度是多少?

我查看了 pid_t 结构(例如 t.html">http://dvbstreamer.sourceforge.net/api/structPID_t.html),它的 PID 是一个 INT...

在那张纸条上,我假设 PID 是一个“signed int”……在那种情况下,signed int 的最大长度是多少?

感谢帮助

林顿

最佳答案

以字节为单位的大小取决于系统,应该像这样简单地查询:

unsigned int pidLength = sizeof(pid_t);

编辑:如果您担心十进制表示的长度,如printf("%d", myPID);,我建议也查询一下。例如,snprintf 函数返回在有足够空间的情况下本应写入的字符数(无空终止符)。所以你可以这样做:

char tmpChar;
int representationLength = snprintf(&tmpChar, 1, "%d", myPID);
if (representationLength == -1) { handle_error(); }
char *finalString = malloc(representationLength + 1);
snprintf(finalString, representationLength + 1, "%d", myPID);

也许 snprintf(NULL, 0, "%d", myPID) 也可以查询长度,但是 Linux snprintf 手册页是这样说的:

Concerning the return value of snprintf(), SUSv2 and C99 contradict each other: when snprintf() is called with size=0 then SUSv2 stipulates an unspecified return value less than 1, while C99 allows str to be NULL in this case, and gives the return value (as always) as the number of characters that would have been written in case the output string has been large enough.

关于c - 进程的 PID 的最大可能长度是多少? (64 位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com