Search code examples
ckillpid

How to kill a process in C when pid is know


I have a cleanup process which needs to kill the process whose PID is passed as an argument to it.

I can very well use the kill(pid_t, SIGKILL) to kill it. However the PID which i get is an integer value. So i cant get a pid_t out of it. Is there any way to use and integer pid to kill the process?

Well. I can very well use system("kill -9 <pid>");

I am looking for a solution from C.


Solution

  • How did the PID get into an integer type in the first place? If your function takes a PID, it should take it as a pid_t. If it's a text argument, you should parse it into a pid_t, not an int.

    Unless you have some unusual situation, the only integer-like variable you should ever store a PID in is a pid_t.