Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program under Linux: how to find out if another program is running

Tags:

c

linux

process

My C program running under Linux wants to find out, by name, if another program is running. How to do it?

like image 529
Pete Wilson Avatar asked Mar 19 '26 19:03

Pete Wilson


1 Answers

There are two ways basically:

  • Use popen("pgrep yourproc", "r"); and then fgets from it
  • Use opendir and readdir to parse /proc - this is basically what ps(1) does

Not the cleanest but I would go with the first of these.

like image 124
cnicutar Avatar answered Mar 21 '26 08:03

cnicutar