tags:

views:

907

answers:

7

In a Linux-based project that I am working on, I need to be able to find all of my child processes. It is not feasible to record every time one is started -- they need to be found after the fact. This needs to be pure C, and I'd like to do it without reading /proc. Does anyone know how to do this?

A: 

You could parse a process list (ps -ax?) that included the parent process ID. This could probably be done with a simple shell script.

sangretu
He said pure c ;). I've done it that way with success though.
Byron Whitlock
Does it still count as pure C if you execute a shell command from it?
sangretu
@sangretu No - even if it is C shell. ;)
Duck
+2  A: 

You could use popen

Something like. (Hopefully the syntax is close enough)

#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;
}


RC
bleh, same answer as sangretu's.
Byron Whitlock
Byron, I voted you up because you probably have the best answer for what the question is now that ps is not an option. Though, I do hope you did not vote me down b/c my answer is the same as sangretu's with more detail. We were 2 minutes apart and it took me time to write the initial pseudo-code. I have since edited to provide more detailed code that works.
RC
Thanks, nice code.
Liran Orevi
down vote removed. :)
Byron Whitlock
+2  A: 

sangretu's answer got me thinking, The source code of the ps command would be a good place to start

http://procps.cvs.sourceforge.net/viewvc/procps/procps/ps/

Byron Whitlock
The poster doesn't want to read /proc but I'm willing to bet that's exactly what ps does.
Dave Webb
If I have to read /proc, I will, though I'd prefer not to. That's a good idea -- I'll check that.
c4757p
+4  A: 

It is usually entirely feasible to record child processes every time you start one. conveniently, the parent process is passed the pid value of the child process as the return value of the fork call which creates it.

As the man page says:

pid_t fork(void);

It would help if you could tell us why you think it isn't feasible.

Alex Brown
I assumed he had a good reason. But you're right. It's usually pretty trivial to get that information.
sangretu
A: 

If you want to trace fork events and extract child pids for debugging purposes, there are a number of ways to do that, including:

  • Using GDB
  • using strace
  • Using systemtap
  • Using kernel event connectors (not sure what these are exactly)
Alex Brown
A: 

I find your comment that it is not feasible to record the creation of processes to be odd, but if you really can't (possibly because you don't know how many will be created and don't want to have to keep reallocing memory), then I would probably open all of the files that match the glob /proc/[1-9]*/status and look for the line that says PPid: <num> where <num> was my process id.

Chas. Owens
He said he wanted to do it without reading /proc...
Polaris878
And then he/she accepted my answer, so I guess he/she changed his/her mind after seeing how easy it would be.
Chas. Owens
A: 

Does the original question state that they wrote the app that they want to track child processes for? Could they be asking to find all child processes for a given PID? That would be interesting.