Get child process id of already running process

I'm calling an external program (via pty.js in node) that when it runs it spawns a child process. When the parent process finishes it leaves the child running for quit some time (orphan). I can get the id of the parent process, but would like to get the id of the child that is spawned so I can kill it when the parent quits.

I don't believe there is any way in node to achieve this, so is there anything in C that allows you to get the id of a child, knowing only the parent id. Everything I have found so far relates to getting the child id from the fork() but the fork took place in the external program.

Alternatively I guess I could make a call to ps or pstree / something similar and parse the output but feels like a bit of a hack.

EDIT

It's not nice but I've come up with this so far:

#include <limits.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
  int id = atoi(argv[1]);
  int i;
  for(i = 0; i < INT_MAX; i++) {
    if(i != id && getpgid(i) == id) {
      printf("Process %d, is a child of %d", i, id);
      break;
    }
  }
}

In node.js you can do it via child_process library. There is a pid member function that returns the spawned child's pid. E.g.

var spawn = require('child_process').spawn,
    grep  = spawn('grep', ['ssh']);

console.log('Spawned child pid: ' + grep.pid);
grep.stdin.end();

You can also kill the child process with kill.

The pty.js does a fork with a pseudo terminal, which is useful if emulating terminal. Otherwise you should use child_process to spawn/fork new processes.

If you can add this code at the end of parent process :

int i ;
while(1)
{
    i = wait();//wait child exit
    if(i == -1)//if there is no child process 
       break;
}

If you can't . Then you need do nothing!

Here is the problem:

If the parent process finished but the children process didn't, those children process called :"orphan process" NOT "zombie". A "zombie process" means a child process finished while the patent process still runs, and the parent process didn't handle the child process's aftermath.

Since system will set "init" become the parent of all "orphan process".The init process will deal with everything . So you need do nothing.!!