Skip to content
Advertisement

How to detect when a process is terminated in Linux using C++?

The purpose is I want my program running on Linux to be terminated when some random process is terminated. I can get PID or process handle of the process that my program is to monitor.

Are there any possible approaches that I could take?

Advertisement

Answer

Linux 5.3 introduced pidfd_open, which lets you get a file descriptor from a PID. The FD will become readable when the process dies, which you can detect with select/poll/epoll, like this:

JavaScript
Advertisement