Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Check if process is running C

  • 22-12-2010 12:27PM
    #1
    Registered Users, Registered Users 2 Posts: 2,353 ✭✭✭


    Hi Guys,

    From a C program how could I check if a specified process is running....to break it down, get pid from process name and check if pid is valid.....


    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 24 xonet


    Have a look at the source code for the pgrep utility, i.e. pgrep.c, and it will show you how to do it. Go to http://www.koders.com and search for pgrep.c.


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    I don't know the proper way, this should work though.
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
            FILE *fd = popen("pgrep rpn", "r");
            char pid[10];
            while (fgets(pid, 10, fd)) {
                    printf("PID: %s", pid);
            }
            return 0;
    }
    


Advertisement