Link to home
Start Free TrialLog in
Avatar of lwinkenb
lwinkenb

asked on

Catch the kill command?

If my process is running with a PID of 1000, is there a way to catch in my code when someone at the terminal types in:  kill 1000 ?

I want to know this so I can free up used resources, and close up log files.
Avatar of Robson
Robson
Flag of Poland image

It's easy to cach a signal:

void sighandler(int number)
{
    printf ("Process is killed!\n");
}

int main()
{
    signal(SIGKILL, sighandler);
    /* rest of your program */
}

But what is exactly that you want? Do you mean that program with pid 1000 is already running and you want to protect it from killing? I don't suppose its possible...
Avatar of lwinkenb
lwinkenb

ASKER

That didn't work.  I tried the above code, and it just said "Terminated" when I killed it.  It didnt say "Process is killed!".

>> Do you mean that program with pid 1000 is already running and you want to protect it from killing?
The program with pid of 1000 is the small program I am writing here.  This is the one that is catching the signal.
ASKER CERTIFIED SOLUTION
Avatar of Robson
Robson
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks =)