LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2009, 05:16 PM   #1
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Rep: Reputation: 0
monitoring child processes


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(?)

thank you
 
Old 12-03-2009, 05:37 PM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
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.
 
1 members found this post helpful.
Old 12-04-2009, 11:18 AM   #3
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thank you for the reply.

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.

Thank you!
 
Old 12-04-2009, 12:49 PM   #4
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Lightbulb

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.

Thanks for your help.
 
Old 12-05-2009, 12:37 PM   #5
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
I use [ `ps -uww <PID> | grep <cmdname> >/dev/null` ] to test if a given
PID is running the expected command.

I'm curious about your coworker's solution.

Last edited by carltm; 12-05-2009 at 12:38 PM. Reason: typo
 
Old 12-10-2009, 04:22 PM   #6
caverdave
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Smile

There were two parts to the problem.

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).

Thanks.
 
  


Reply

Tags
monitoring, process



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
su processes child of pid 1? acid_kewpie Linux - Software 5 07-29-2009 01:22 PM
forking 7 child processes ianomc Programming 5 11-07-2004 12:33 PM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Rotatelogs - Do I have too many child processes? fireman949 Linux - Software 2 06-08-2004 02:04 PM
parent and child processes skora Programming 5 11-02-2003 10:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration