简体   繁体   中英

What is the max possible length of a PID of a process? (64 bit)

I have been working on AIX and see some of the process ID's running are 8 long....

I am using the PID as a concatenation with another char* value and am trying to work out what that MAX possible length of a PID if you were looking at it from a "char perspective"?

I have looked at the pid_t structure (for example t.html">http://dvbstreamer.sourceforge.net/api/structPID_t.html) and it has the PID as an INT...

On that note I assume a PID is a "signed int"....in that case what is a signed int's max length?

Thanks for the help

Lynton

The size in bytes is system dependent and should simply be queried like this:

unsigned int pidLength = sizeof(pid_t);

Edit : If you're worried about the length decimal representation as in printf("%d", myPID); , I suggest querying that, too. For example, the snprintf function returns the number of characters (without null-termination) it would have written if there were enough space. So you can do:

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);

Maybe snprintf(NULL, 0, "%d", myPID) would work to query the length as well, but the Linux snprintf man page says this:

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.

另一种方法可能是使用(int)log10(pid)+ 1.当然,您可能需要为终止空字符包含空格,但上面应该为您提供将值表示为字符串所需的字符数。

The maximum number of digits in an 32-bit int is 10.

The maximum number of digits in a 64-bit signed int is 19.

The maximum number of digits in a 64-bit unsigned int is 20.

I don't know if AIX uses 32 or 64 bits, signed or unsigned, for its pid_t .

Edit: it's gotta be signed because fork can return -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM