Pkill guarantees

276 views Asked by At

Can pkill guarantee the following situation never happens:

  1. I use pkill -f "abc"

  2. pkill finds process by name and remembers pid

  3. process ends

  4. Linux starts a new process with the same pid

  5. pkill kills the process started at step 4

1

There are 1 answers

1
Sam Hartman On

Pids do wrap and do eventually get reused. However, pids assigned to recently running processes are not soon reused. so, in practice the problem you're worried about never happens. It is theoretically possible as far as I can tell. However, that would mean that

  • pkill was running slow enough that a whole bunch of new process IDs could get allocated between finding the process and killing it
  • the rest of the system was running fast enough to create all those processes and get to a point where the recently used pid was freed.
  • As pointed out in comments, either you are root or the process is running as the same user

It's possible there is some way of attacking pkill so it's that slow, but such an attack would almost certainly be a kernel bug. I've never been in a situation where worrying about this problem was the right design decision.