Core\Os::pid
Returns the number the operating system gives this program's process.
The number does not change while the process runs. It belongs to the whole process, not to one request. A server process that handles many requests returns the same number for all of them. Two processes that run at the same time on one machine never have the same number.
Use it to tell processes apart: in a log line, in the name of a worker, or in the name of a file that only this process writes. The number is different on every run, so do not write it into a test.
The examples show how to read the number, that it stays the same, and how to put it in a file name.
Return value
Returns uint. The process number, which is larger than 0.
Examples
This process number
<?nvs// Shows how to read the number the operating system gives this program's process.
uint $pid = Core\Os::pid();
// The number is different on every run, so this example does not print it.// Every process number is larger than 0, so this prints `yes`.echo $pid > 0 ? 'yes' : 'no', "\n";yesThe same number every time
<?nvs// Shows that the process number does not change while the program runs.
uint $first = Core\Os::pid();
// This loop builds a text, so some time passes between the two readings.string $text = '';uint $i = 0;while ($i < 1000) { $text = $text . 'x'; $i = $i + 1;}
// The second reading is the same number. This prints `yes`.echo Core\Os::pid() == $first ? 'yes' : 'no', "\n";yesA file name for each process
<?nvs// Shows how to give a temporary file a name that no other running process uses.// Two processes that write a report at the same time then do not overwrite each other.
// The process number goes in the middle of the name.string $name = 'report-' . Core\Os::pid() . '.tmp';
// The name starts with `report-` and ends with `.tmp`. This prints `yes`.echo Core\Str::startsWith($name, 'report-') && Core\Str::endsWith($name, '.tmp') ? 'yes' : 'no', "\n";
// There is a number between the two parts, so the name is longer than// `report-.tmp`. This prints `yes`.echo Core\Str::length($name) > Core\Str::length('report-.tmp') ? 'yes' : 'no', "\n";yesyes