9

I would like to find out if a process is running. I DO NOT want to use any system("") commands. Is there any C based function that lets you know if a process is running?

I would like to provide the process name and want to know if it's running.

Thanks,

jww
  • 97,681
  • 90
  • 411
  • 885
Kitcha
  • 1,641
  • 6
  • 19
  • 20
  • I think you need to be more specific about what exactly you want to do. Do you know the pid or name of the process in question? – Gordon Bailey Aug 02 '12 at 21:19
  • You might want to look at this related question: http://stackoverflow.com/questions/3667486/display-all-process-using-a-posix-function – rayd09 Aug 02 '12 at 21:23
  • Also see [Determine programmatically if a program is running](https://stackoverflow.com/q/6898337/608639). – jww Jul 11 '17 at 14:38

4 Answers4

13

Sure, use kill(2):

 #include <sys/types.h>
 #include <signal.h>

 int kill(pid_t pid, int sig);

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

So just call kill(pid, 0) on the process ID of the process that you want to check for and see if you get an error (ESRCH).

yaobin
  • 2,436
  • 5
  • 33
  • 54
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • This is a good way to check if you have the process id, but he only has the process name. – cegfault Aug 02 '12 at 21:24
  • @cegfault: That's true. A name doesn't really tell you "the process", though, since there may be many processes with the same name. I think the OP's question is lacking several important details. – Kerrek SB Aug 02 '12 at 21:35
  • There is no "process name" : `execve(argv[0], NULL, NULL);` – wildplasser Aug 02 '12 at 21:38
  • yeah, agreed. It's hard to give a good answer without more details – cegfault Aug 02 '12 at 23:19
  • I was able to follow your approach with fetching the pid of my process. It really helped. – Kitcha Aug 03 '12 at 20:47
  • @Kitcha: great, I'm glad to hear :-) – Kerrek SB Aug 03 '12 at 21:43
  • @KerrekSBThis works well if I want to check the existence of a local process. But how can I check the process existence on another machine? I can rsh to that machine. – werk Sep 11 '18 at 06:21
  • @werk: wow, that's a whole different question, and any answer will probably not be suitable for your problem. Yes, in principle you'd want to log into the machine somehow and then perform the local steps, but it sounds like you probably want some existing distributed job management system, rather than roll your own! – Kerrek SB Sep 11 '18 at 20:17
  • @KerrekSBThanks! This is what I thought. Meanwhile I will try Boost MPI. – werk Sep 16 '18 at 06:18
  • And remember you have no way to know that pid's still refer to the same process they did a minute ago. It's one of the few serious design flaws in UNIX. Try to never ever use pid, asynchron signals or the library calls of system and select if you have any quality development in mind. – Lothar Feb 17 '23 at 12:55
2

On Linux, another way to do this might include examining the contents of the /proc directory. Numbered directories are process IDs, while subdirectories containing the cmdline file show the name of the command.

For example, if /proc/1234/cmdline contains the value foo, then process foo has an ID of 1234. You could map names to PIDs this way, using standard file access functions in C. See proc(5) for more information.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
1

You may find this interesting: http://programming-in-linux.blogspot.com/2008/03/get-process-id-by-name-in-c.html

The "conventional and best way" to do this is read the /proc folder. You can see this question for more, which references http://procps.sourceforge.net/, which may be of interest to you

Community
  • 1
  • 1
cegfault
  • 6,442
  • 3
  • 27
  • 49
0

You can scan the /proc filesystem for all currently running processes, and see if the cmdline entry matches what you want for that particular process.

However, there is a race condition. The process may die after you have decided it was running.

The sure fire way to know if your process is still running is to be the one that launched the process. Then, when a child dies, you will get SIGCHLD and you can use waitpid(-1,..) to find out which child died.

jxh
  • 69,070
  • 8
  • 110
  • 193