Use pid_t for PID, PGID, SID Values

The pid_t data type should be used in the declaration of all variables that hold PID, PGID, or SID values.3 It has sufficient precision to hold any PID value.

Use 32-bit int for Process Counters

Programs that maintain counts of the number of active processes in the system, or any subset thereof, should use the 32-bit int or equivalent data type to hold these counts. This provides more than adequate precision so that overflows are not a problem.

Comparision Checks on PIDs

PID values should be compared only for equality (or inequality). Never check for PID values greater than or less than other PID values. Never check for specific PID values because there is no association between specific values and specific processes.

One exception is the PID of the system initialization process (referred to as init). The PID value reported by interfaces such as getppid(2) is always 1 if the process is the init process. It is thus appropriate to check for a return value of 1 from this interface.

Validity checks on a PID value are usually unnecessary. When desired for program self-checking, the only validity check appropriate to make is that the PID is a positive value. (Note that the kill() function can be used to determine whether a process having that PID exists, and is accessible by the caller, by passing 0 for the signal.)

Use Data Structures Other Than Arrays for Tracking

PID values should not be used as array indexes. PID values can exceed 1 billion (1,000,000,000), but the number of valid PIDs at any point in time is relatively small (never greater than the value of nproc). When tracking PID values, it is better to use other data structures such as hash tables. Further, because the PID range is dynamically tunable, new PID values might exceed the size of a previously created array.

Print/Display Formats for Ten Decimal Digits

Any print or display formats should be able to handle ten (10) decimal digits, since PID values can range to greater than 1 billion.

Incorrect Handling

This section describes how some programs have built-in assumptions on prior PID or active process limits.

Using 16-Bit short or unsigned short for PIDs

A short data type variable has only 16 bits, and can represent values only up to 32,767 (signed) or 65,535 (unsigned). Some programs might be using short variables to hold PID values. Such programs fail for PID values that exceed the precision of such variables.

3The pid_t data type has been the specified type to use since HP-UX release 8.0. Programs that use a 32-bit int type support the expanded range of values properly. However, HP recommends that the pid_t data type be used as specified in the manpages. This will ensure source compatibility with any future expansion in the size of the data type.

6