I have a process that interfaces with a library that launches another process. Occasionally this process gets stuck and my program blocks in a call to the library. I would like to detect when this has happened (which I am doing currently), and send a kill signal to all these hung processes that are a child of me.
I know the commands to kill the processes, but I am having trouble getting the pids of my children. Does anyone know of a way to do this?
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp = popen("ps -C *YOUR PROGRAM NAME HERE* --format '%P %p'" , "r"); if (fp == NULL) { printf("ERROR!\n"); } char parentID[256]; char processID[256]; while (fscanf(fp, "%s %s", parentID, processID) != EOF) { printf("PID: %s Parent: %s\n", processID, parentID); // Check the parentID to see if it that of your process } pclose(fp); return 1; }