I'd like to introduce a solution for the problem originally described here:
pkill
is not an atomic operation, not by a long stretch. In the while since pkill -P 666 foo has determined that pid 667 is the child of 666 named foo (which on e.g. Linux it does by opening and reading multiple files in the /proc fs) until it actually calls the kill(2) system call, the process could've already terminated and its pid could've been already reused.
In order to make pkill -P XXX
operation safe (like transactions in SQL) I'd like to implement the Two Phase Commit Protocol:
- Check if the process is child of
THE_PARENT
process. - If "yes", mark the PID of the child process as "DO_NOT_REUSE".
- Check if the process is still the child of
THE_PARENT
process. - If "yes", kill the child process, if still possible.
- Remove the
DO_NOT_USE
flag for that PID number.
For that purpose, can I mark a PID number as "can not be used"?
DO_NOT_USE
removal?DO_NOT_USE
removal. OS will just use that PID as usual. What is the point I may be missing?