0

When a process is started, Linux picks a PID from a pool.

I wonder whether it is possible to tell Linux not to pick certain PID without rebooting the machine as I want to disable certain PID from being picked for probably 2 seconds, and then I will lift that restriction.

Use case

I have like 20 C++ applications running in parallel and each of them will create a named shared memory segment called process.[pid]. And since each application could crash (in case there is a bug), then I don't get a chance to clean up (removing process.[pid] or to be more precise, shm_unlink() the segement). Due to limited memory, I would like to reclaim the resource if I find the process associated with process.[pid] has died already. And so I was planning to write a script (with cron to kick it in periodically) to parse the named shared memory segment files and then clean up. But I am afraid when my script is about to delete process.1234 and OS assigns PID 1234 to my newly started C++ application, which will attempt to create process.1234, and then right after it is created, OS context switches to my script to delete process.1234.

Thanks.

8
  • 4
    What's your use case? Commented Jun 5, 2019 at 13:47
  • Periodically, I want my script to check whether the processes using the named shared memory segments are still alive and if not, it removes those SHM segments, during this check and removal period, I don't want the OS to reuse a PID that is dead and was previously allocated and attached to a SHM segment when my script is removing, race condition.
    – HCSF
    Commented Jun 6, 2019 at 2:04
  • 1
    At least it'll fail safely – it won't cause the script to remove 'live' SHM segments, rather it'll just cause the script to leave stale segments. And chances are the condition is no longer there next time the script is run so the segments will get cleaned up anyway... But have you considered using systemd's RemoveIPC= to handle this automatically? Commented Jun 6, 2019 at 4:57
  • @grawity sorry, I think I missed your point. What will fail safely? The script? My application will create some named segment like process.[pid]. Hence, if my script can parse the name and does something like if pid is not running; then delete the segmenet, and the OS picks the PID for the next process (my application again) to use between the if and then statement, then my application will attempt to create, and context switch back to the script, the script deletes the segment just created, and then context switch back to my application. Then, the whole thing is wrong.
    – HCSF
    Commented Jun 6, 2019 at 6:30
  • RemoveIPC= might not work as my application will create multiple named segments but some should stay forever. Some should be deleted after the process crashes, or properly exits.
    – HCSF
    Commented Jun 6, 2019 at 6:31

0

You must log in to answer this question.

Browse other questions tagged .