Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 353
pabouk - Ukraine stay strong Avatar asked Nov 30 '25 21:11

pabouk - Ukraine stay strong


1 Answers

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.

like image 79
user2889076 Avatar answered Dec 02 '25 14:12

user2889076



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!