1822000 Members
3696 Online
109639 Solutions
New Discussion юеВ

Killing process using C

 
Henry Chua
Super Advisor

Killing process using C

Hi Guys,

I would like to kill a process using C, I know the PID so I use:

kill(PID, SIGTSP);

But is it possible to see if this process is valid using C as well? What is the common practise?

Thank u.
Henry
5 REPLIES 5
Rajeev  Shukla
Honored Contributor

Re: Killing process using C

Hi Henry,

By you asking
But is it possible to see if this process is valid using C as well?
Do you mean about the PID? you want to check if its a valid PID? You can do that using the raise() function which actally is used to send signal to a running process and looking at the man pages you see
"If sig is 0 (the null signal), error checking is performed but no
signal is actually sent. This can be used to check the validity of
pid." So before you use the kill function you can call the raise function.

Also why do you want to re-invent a wheel until its a real requirement in your program or a module you are trying to add.

Cheers
Rajeev
Henry Chua
Super Advisor

Re: Killing process using C

Hi Rajeev,

Thanks for the info, raise() sends signal to itself to its validity, however is there any way i can test process running in the background.
Say i ps -ef and know process A with PID 9081 is running. But after say an hour i want to see if it is still residing in the spool. Is there anyway I can write a procedure in C to test whether this PID is still active... even better if I can see the actual program using this PID...

Best regards
Henry
Ralph Grothe
Honored Contributor

Re: Killing process using C

I'm not C conversant,
so probably not entitled to advise you.
But the common practise (that I also use in Perl scrcipts) is to send the PID as signal 0.
See also "man 2 kill" where they mention the action on receipt of 0.
Madness, thy name is system administration
Renda Skandier
Frequent Advisor

Re: Killing process using C

I'm pretty sure the easiest way is to send a system command.
your system command can either call a script or send the command
ps -ef | grep pid | kill -9 `awk '{print $2}'`
A. Clay Stephenson
Acclaimed Contributor

Re: Killing process using C

I'm positive that since you are already using C is to the easist way is
cc = kill(pid,0);
if (cc == 0)
{
/* valid process */
}
else
{
/* invalid process */
}
If it ain't broke, I can fix that.