0

I want to know if a process on my computer exists. There are three ways to test this through code in C: kill, getpid (pid) ,stat (path, & stat)

I want to know what are the pros and cons of each method?

  • By stat do you mean checking for `/proc/`? – Barmar Aug 03 '20 at 15:49
  • Uhm... `getpid()` doesn't take a parameter – Marco Aug 03 '20 at 15:49
  • 1
    Does this answer your question? [Check if process exists given its pid](https://stackoverflow.com/questions/9152979/check-if-process-exists-given-its-pid) – Marco Aug 03 '20 at 15:51
  • The `getpid()` function tells you the PID of the current process. You know the current process exists – if it didn't, it couldn't execute `getpid()` (or any other function). – Jonathan Leffler Aug 04 '20 at 03:48

1 Answers1

4

kill(pid, 0) is POSIX compliant whereas stat("/proc/<pid>", ...) is not.

I don't know what you mean with getpid() as it doesn't take any parameters.


Update:

getpgid(pid) is POSIX compliant as well, so I don't think there's a difference between using kill and getpgid for your purpose. I would choose kill because it's more widely used.

Marco
  • 7,007
  • 2
  • 19
  • 49