ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a supervisor process that creates child processes and monitors them with waitid() (this seems to be the most efficient means to monitor a child process to see if it crashes as far as I know).
However, if the supervisor process crashes or gets restarted/reloaded deliberately, it can no longer monitor the pid's of the "child" processes because they don't belong to the newly restarted supervisor process. btw, I'm keeping the pid's of the child process in a shared mem region so that they can be read by the restared supervisor process.
Is there efficient means of checking if a process is still running through its pid? Other than going through the /proc system which seems to me would be quite inefficient(?)
The easiest way is to create a file for each process, and have it contain only
the PID. The supervisor can then cat the contents of each file to find each
PID, and you can easily test whether or not that process is still alive.
However, I already have the PID's in a shared mem region, so I don't need to keep another file containing the PID.
What I think I'm looking for is either a way to "re-inherit" a child process back when the supervisor process restarts (in this case re-inheriting from init, since the supervisor is spawned by init), or find an efficient means of determining if a non-child process is still running (checking a list of processes once per second). There's got be a syscall somewhere that allows one process to efficiently check for the existence of a process by PID, I would hope!
One thought I had was to open a file descriptor for /proc/<pid> and do read's on the fd until there is an error on the read, but I'll have to try to see if this works on not. This can't be much worse than calling waitid() I would think.
A coworker sent me a link to some code that used the kill(pid, 0) to check for a process. If an error is returned and errno == ESRCH, then the process does not exist. So this seems to work great.
First, the supervisor has to be able to check the status of a process whether its a child or not. Using kill(pid, 0) will allow the supervisor to do this.
However, if the child process crashes and becomes a zombie, the kill() still returns success, but if the supervisor is not the parent (because it got restarted), it can't do a waidid() on the child of the previous supervisor process to "kill" the zombie because waitid() only works for child processes.
Therefore, the supervisor has to do a sigaction(), setting the sa_flags = SA_NOCLDWAIT, which means that a child process will not create a zombie if it crashes.
This way, the supervisor can call kill(pid, 0) and get the correct status of the process, whether its a child or not.
That's my solution and I'm sticking to it (for now).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.