Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pidfd to track processes on Linux >= 5.4 #190

Open
nh2 opened this issue Aug 12, 2020 · 2 comments
Open

Use pidfd to track processes on Linux >= 5.4 #190

nh2 opened this issue Aug 12, 2020 · 2 comments

Comments

@nh2
Copy link
Member

nh2 commented Aug 12, 2020

Background:

The problem Pids in ProcessHandles is that on most system they are limited to 2^16 many by default. If you spawn many short-lived processes quickly, the pid namespace can wrap around and you will accidentally waitForProcess (wait()) on the wrong process, or terminateProcess the wrong one.

Linux 5.4 solves this with pidfds (which are per-process, and 32-bit many). They can point to zombie processes so they will never accidentally point to a different process.

The process library could use them on newer Linux by simply tracking the pidfd in a Maybe field inside ProcessHandle.

  • After being spawned, a pid can be converted to a pidfd using pidfd_open() -- but this is still slightly racy, and better is to get it atomically directly from clone(). But it's an easy migration path that's a strict improvement already.
  • pidfds can be waited on with select(), epoll() and so on, which means we can use the GHC IO manager to wait for them more efficiently than with the usual waitForProcess.

This Rust library https://github.com/pop-os/pidfd shows how you can wait for a program to finish using waitid().

@nh2
Copy link
Member Author

nh2 commented Dec 15, 2020

Linux 5.10 made pidfds nonblocking, thus making them much easier to use without additional threads, and probably direct integration into the IO manager:

https://news.ycombinator.com/item?id=25413266

@nh2
Copy link
Member Author

nh2 commented Aug 2, 2022

glibc 2.36 now contains wrappers for the pidfd_* functions: https://www.phoronix.com/news/GNU-C-Library-Glibc-2.36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant