Standard or best practise for unique process identifier (on Unix-like OS)

On Unix systems PID is unique only at any given point in time. It is not unique in a given time interval because PIDs used in the past can be re-used after the original process terminates. For storing of history data of all the processes which were running on the system since boot I need identifiers to be unique since the boot.

Is there any standard or best practise for that? Is there anything wrong or missing in my intended solution below?

Intended solution

It seems logical to use PID together with the process start time (as standard Unix time) as a unique process identifier. Such identifier would be even unique between reboots.

Advantages

  1. Both PID and process start time are clearly defined enough and make sense on all Unix-like systems.
  2. Both values are integers and relatively easy to process.
  3. Processes can be unambiguously sorted by start time followed by PID.
  4. Both values can serve other uses besides of just unique identification of processes.

Disadvantages

  1. There seems to be no portable way of getting the process start time except calling external utility ps. For example in shell: date -d "$(ps -p $PID -o lstart=)" +%s. In Linux it is possible to compute the process start time from /proc/$PID/stat but this method does not work on other systems.
  2. Because of the facts in the previous point the portable method can be unnecessarily slow and resource intensive.

Upvotes: 3

Views: 1103

Answers (1)

user2889076
user2889076

Reputation: 11

ntp service always adjusts system time, which influences start time read from "ps -p $pid -o lstart". As a result, you are able to got a different start time after system time adjusted.

Upvotes: 1

Related Questions