Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does a PID exist? 3

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
Given a PID I want to check to see if a process exists with this PID under a linux system. Does anyone know of a way to do this?


Smilee
 
C is definitely the harder way to do it since
ps and awk have already been written.

Something like:
Code:
function isPid() {
num=$1
ps -aux | awk -v id=$num ' {if ($2 == id) {print}}
}

 
It's pretty easy in C. Use the [tt]kill()[/tt] function. The syntax of [tt]kill()[/tt] is...
[tt]
#include <sys/types.h>
#include <signal.h>

int kill(pid_t pid, int sig);
[/tt]
If you call it with the process ID you're looking for and [tt]0[/tt] (zero) for the signal, it won't actually send the signal to the process, but will only see if the process ID exists on the system. [tt]kill()[/tt] will return a [tt]0[/tt] (zero) if the process ID exists, or a [tt]ESRCH[/tt] if not found.

Hope this helps.

 
Didn't know that..That's useful.
Thanks.
 
SamBones: That's EXACTLY what I was looking for. I just hope that the programmer that comes behind me reads the comment about the kill command not actually killing the process :p

That or I could wrap the function call.


Thanks!
 
Well, the kill command is a misnomer anyway. It only kills a program when you send a SIG_KILL. Otherwise, it simply delivers the given signal to the program, who can then react to it as it chooses (which may be to die).

A better name for kill woulda been &quot;send_signal&quot; or &quot;deliver,&quot; but it's not getting changed anytime soon.

So the comment is certainly a good idea, but just an FYI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top