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 09-14-2005, 03:16 PM   #1
$@ya
LQ Newbie
 
Registered: Jun 2005
Distribution: SUSE 9.2
Posts: 13

Rep: Reputation: 0
Process monitor


Hi,

I have a shell script which keeps track of the a process, by constant check whether the process id is in the PS(process listing) table or not, and if it is not in there it start this program again and grab its the new process id does this all the time. It works fine but, I want to write a C module now, which would do the same thing but I am not able to find the system call which does this (process listing or check in process table) . Is there a way to do this other than using a system call or if there is a system which i over looked or i dont know off. If someone can tell me which system call to use it would great.

thanks
neo
 
Old 09-14-2005, 05:00 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Heh, I had to do just this sort of thing. You could probably accomplish the same goal with a bash script running as a daemon, which *could* do a ps -aux | grep <what you're looking for> and if it's not there, restart it.

However, in C - unfortunately it's not just as simple as a system call, although
you can set up a basic process monitor by using a combination of fork(), a signal handler, and waitpid().

In main(), set up a signal handler (using sigaction), and tell it to listen for 'SIGCHLD'. That way, your program will be notified whenever the child process does something (such as exit )

Code:
#include <signal.h>
    ...
    int main( blah blah blah) {
        struct sigaction sa;

        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = handler;
        sigemptyset (&sa.sa_mask);
        sigaction(SIGCHLD, &sa, NULL);
What gets assigned to sa_sigaction is a function pointer to the function that you want to be called whenever a signal happens. My function here's called 'handler', so that's what gets assigned.

Use fork() to fork off a child process (also in main if you wish, or in a function. There's a good reason for the function, you'll see).
Tell the child process to run whatever it is that you want. The return value of fork() to the parent will be the process' id (pid).

Whenever handler is called, it gets passed a set of parameters
Code:
void handler(int signo, siginfo_t *info, void *context) {
    int code;
    sig_code = info->si_code;
    pid = (long)info->si_pid;

    if(signo == SIGCHLD) {
        if(retvalue = waitpid(pid, &code, WUNTRACED) > 0) {
           if(sig_code == CLD_EXITED) {  //child terminated normally
                //Do something *
           } else if(sig_code == CLD_DUMPED) { //child terminated abnormally, i.e. segfault
                 //do something *
           } else if (sig_code == CLD_KILLED) { //child was killed
                 // do something *
           } // and so on
    }
*Keep what goes on in your signal handler as limited as possible; it's not technically "safe" to call many things, such as i/o, during a signal handler, in part since that function may be needed again very soon. If you're going to restart the process which died, I'd suggest calling another function, as restarting will involve another fork() [this is why i suggested a function to do the forking - you'll have to do it again in order to restart, and you can't get back up to main() to use the code you wrote there anymore].

I'm not entirely sure how clear this is turning out to be :-[ The man pages will help you *a lot* here, and absolutely feel free to ask questions. I think the bash script way is going to be easier, but the C way is a great learning experience

Last edited by rose_bud4201; 09-14-2005 at 05:04 PM.
 
Old 09-14-2005, 11:26 PM   #3
mgatny
Member
 
Registered: Mar 2004
Posts: 41

Rep: Reputation: 15
Why not let init do it?

man 5 inittab

... see the respawn option.

Or do you need to do more than restart it if it dies?

Last edited by mgatny; 09-14-2005 at 11:27 PM.
 
Old 09-15-2005, 10:38 AM   #4
$@ya
LQ Newbie
 
Registered: Jun 2005
Distribution: SUSE 9.2
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks guys,

But this is not what i was looking for, may be I was not clear, I know by using waitpid I can track my child or parent process stats but here these two processes (or programs) are not related to each other at all one guy dumps its pid value in /tmp/mypid and other grabs it from there .The only solution that have is do something like this.

#include <unistd.h>

int main(...,...)
{
string pid,new_path;
..
..
..
// grab this pid from /tmp/pidofprocess
//strcat into new_path this "/proc" + pid;
while(chdir(new_path) >=0){
//I am happy process is running
}
printf("process disappeared");
// restart the process
return 0;
}

But this is a dirty way of I am looking at table and stuff, I want to know who (which system call) puts it there.

thanks
neo
 
Old 09-15-2005, 11:10 AM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
You're probably not going to get that in any sort of elegant form, since the kernel doesn't store the process name in any relation to its pid (it doens't care what the process is named, so no need to store that information in any usable form) - you'd have to know the pid before going to look for it.....why do you want this in C form, if the bash script is working?
 
Old 09-15-2005, 11:57 AM   #6
mgatny
Member
 
Registered: Mar 2004
Posts: 41

Rep: Reputation: 15
Quote:
I know by using waitpid I can track my child or parent process stats but here these two processes (or programs) are not related to each other
The proper way to monitor a process *is* from a parent process. Either from init, which is parent to all processes, or in a wrapper script/program that you write. Unless there are multiple, arbitrary, random processes that you want to inspect at any given time, there should be no reason why you cannot use a parent wrapper. Blocking until you get a signal from your child is exactly what you want to do.


Quote:
one guy dumps its pid value in /tmp/mypid and other grabs it from there
You should not be using regular files for interprocess communication. The system provides robust mechanisms for this, e.g. message queues, shared memory, and semaphore sets, as well as pipes, fifos, and sockets.

See also:
man 5 ipc
man 3 mkfifo
man 2 pipe
man 2 socket

Quote:
while(chdir(new_path) >=0){
//I am happy process is running
}
This is why you should create a parent wrapper if possible. You are busy waiting here when you could be blocking, waiting for your child to signal. If you are going to do a while-loop like this, you should at least call sleep() at the end of the loop.

Quote:
But this is a dirty way of I am looking at table and stuff, I want to know who (which system call) puts it there.
The kernel puts it there, and the /proc virtual filesystem is how you read it. Whether you open/examine the files in /proc yourself, or you use libproc (you will need the procps dev package), this *is* the interface to the information you are looking for.
 
  


Reply



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
can inetd monitor process? iclinux Linux - Newbie 0 01-27-2005 06:49 PM
monitor process iclinux Programming 3 01-16-2005 02:00 PM
process monitor iclinux Programming 6 01-14-2005 09:35 PM
Process monitor in program ... joda Linux - Networking 1 06-10-2004 01:22 PM
How do I monitor a process? mad_ady Linux - General 5 11-07-2003 09:15 AM

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

All times are GMT -5. The time now is 03:46 PM.

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