Results 1 to 5 of 5
I'm writing a C program. This program requires that a certain process be running in order to function properly. So the first thing I want to do is try and ...
- 01-18-2008 #1Just Joined!
- Join Date
- Dec 2003
- Posts
- 41
How to check running processes?
I'm writing a C program. This program requires that a certain process be running in order to function properly. So the first thing I want to do is try and see whether the required process is running. Whats the best way to do that?
I know that something like "ps -e | grep name" would work. But I don't want to use system() and the exec() family can't handle pipes (|) for obvious reasons. If it matters, this section of code will run from child process as a result of fork().
Thanks.
- 01-18-2008 #2
Nice question.I got some hint for you.
Under /proc/[pid-number]/cmdline we will have the name of running process.
But the hardpart is, you have to loop through all various directories to grep all running process name.
you can try this, if you don't have any other solution
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 01-18-2008 #3
Lakshmipathi is correct.
You don't want to use system(), and I guess that's for security reasons, and I can't blame you.
And exec*() won't do pipes. Not directly.
If I were writing in C, my inclination would be to go with Lakshmipathi's solution.
But if pipes themselves don't turn you off, why not carefully write a helper script, one containing all the necessary pipes, one in which all the executables are specified with fully qualified path names, beginning with "/"? Then name that script in your exec*().--
Bill
Old age and treachery will overcome youth and skill.
- 01-18-2008 #4Just Joined!
- Join Date
- Dec 2003
- Posts
- 41
Thanks guys.
I think the solution by Lakshmipathi is good. But it seems to require quite a bit a programming
I would first like to try the helper script idea proposed by wje_lf. But how can I pass the results of the script back to the main program? Does the script utilize errno? That would be convenient because that would mean I can call something like "$?" from within the main program via exec*() or even popen() and test the return value. What do you think?
- 01-18-2008 #5
If the helper bash script does something like this:
then the C program, having called the bash script via fork() and exec(), does a waitpid(). The returned status (whatever the second parameter to waitpid() points to), divided by 256, yields 5.Code:exit 5
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
