
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
Programs that maintain counts of the number of active processes in the system, or any subset thereof, should use the
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
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
Using
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
6