SUMMARY: C code to find all descendants of a given PID

From: lombardi emanuele <lele_at_mantegna.casaccia.enea.it>
Date: Fri, 04 Dec 1998 15:12:52 +0100 (MET)

Dear alphists,

you are the very best!

I received 6 answers to my question, thanks to

        Arrigo Triulzi <arrigo_at_albourne.com>
        Lucio Chiappetti <lucio_at_ifctr.mi.cnr.it>
        Kurt Carlson <snkac_at_java.sois.alaska.edu>
        "Moore, Chuck" <ChuckM_at_DIS.WA.GOV>
        ronny_eliahu_at_corp.disney.com
        Lars Bro <lbr_at_dksin.dk>

All the answers were usefull, but Lars sent me exactely what I wanted:
a C program which I insert as last (but not least!) after my signature.

Thank you everybody,
ciao from italy,
Emanuele

-- 
 Emanuele Lombardi
 mail:  AMB-GEM-CLIM ENEA Casaccia
        I-00060 S.M. di Galeria (RM)  ITALY
 mailto:lele_at_mantegna.casaccia.enea.it
 tel	+39 6 30483366 fax	+39 6 30483591
     This transmission was made possible by 100% recycled electrons.
-------------------------------------------------------------- 
Arrigo Triulzi <arrigo_at_albourne.com>
Why don't you have a look at Linux pstree and see if any of the code
there is portable? Available from all good Linux mirrors, e.g.
ftp://sunsite.doc.ic.ac.uk/Mirrors/sunsite.unc.edu/pub/Linux/
Alternatively, have a look at the source code for autonice which has
been posted to alpha-osf-managers, some time in 1995.
--------------------------------------------------------------------
Lucio Chiappetti <lucio_at_ifctr.mi.cnr.it>
He suggesetd to make a filter of ps output. 
--------------------------------------------------------------------
Kurt Carlson <snkac_at_java.sois.alaska.edu>
The program uaklogin within:
	ftp://raven.alaska.edu/pub/sois/README.uakpacct
	ftp://raven.alaska.edu/pub/sois/man.uaklogin 
 kit:	ftp://raven.alaska.edu/pub/sois/uakpacct-v2.7.tar.Z
does this (and a couple other things).  Does contain a callable routine
in C for gathering the pids and linking the ancestry.   k
---------------------------------------------------------------------
"Moore, Chuck" <ChuckM_at_DIS.WA.GOV>
	He has done something like what I need using REXX on AIX
	BUT I've not REXX
----------------------------------------------------------------------
ronny_eliahu_at_corp.disney.com
 While back, I wrote such a C code and later, after installing 
     Performance Manager (Digital UNIX Associated Products Volume 2), 
     I found out a script called process_tree in /var/opt/pm/SMscripts 
     which may be something you may want to check out. Basically this 
     script parses out the ps command to display a tree of all processes 
     with child, sons, nephew nicely tabbed --The only thing is that you 
     can not pass any arguments to process_tree script to report on 
     particular PID.
>>>>> from lele:
I tried to execute /var/opt/pm/SMscripts/process_tree but I get error
<<<<< 
--------------------------- here Lars's C program (perfectely running) 
------------------------------------------------------------------------
Lars Bro <lbr_at_dksin.dk>
Hi, Emanuele
I have put something together here that you can try. It is not tested
properly so please do that.
The function list_family(int ppid) lists all descendants of ppid into
the array ctab of integers. The array is terminated with -1 as processes
cannot have PID== -1. The integer nc tells how many were found. Please
remember to free() the array when finished. list_family(), ctab and nc
could
be global symbols whereas ptab, np and traverse should be private.
yours, Lars Bro
#include <limits.h>
#include <sys/table.h>
static struct tbl_procinfo *ptab;
static int np;
int *ctab;
int nc;
static int traverse(int ppid)
{
        int i;
        for(i = 0; i< np; i++)
                if ( ptab[i].pi_status != PI_EMPTY && ptab[i].pi_ppid ==
ppid) {
                        nc++;
                        ctab = (int *)realloc(ctab, sizeof(int)*(nc +
1));
                        ctab[nc]=-1;
                        ctab[nc-1]=(ptab[i].pi_pid);
                        traverse(ptab[i].pi_pid);
                }
}
list_family(int ppid)
{
        int i, cc;
        int *childlist;
        nc = 0;
        ctab = (int*)malloc(sizeof(int)*(nc + 1));
        ctab[nc]=-1;
        np = table(TBL_PROCINFO, 0, (void*)0, INT_MAX, 0);
       if (np < 0) {
                perror("table");
                return -1;
        }
	if (np == 0)
		return 0;
        ptab = (struct tbl_procinfo*) malloc(np*(sizeof(struct tbl_procinfo)));
        cc = table(TBL_PROCINFO, 0, ptab, np, sizeof(struct tbl_procinfo));
        if (cc < 0) {
                perror("table");
                free(ptab);
                return -1;
        }
        traverse(ppid);
	
	free(ptab);
	return 0;
}
main()
{
        int i;
        if (list_family(9539) == 0) {
        	printf("found %d members\n", nc);
        	for(i=0; ctab[i]!=-1; i++)
                	printf("in family %d\n", ctab[i]);
        	free(ctab);
	} else
		printf("went wrong\n");
}
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
On  2 Dec, To: alpha-osf-managers_at_ornl.gov wrote:
> 
> I'm here asking for some C help.
> 
> I'm not a C programmer: I'm only able to cut & paste others C programs.
>    BUT
> I need to make a C program which ,given a PID, returns  the PIDs of all
> its descendants (sons, nephews...) .
> 
> I tried to use table(2) but I'm not good enough to make a clean &
> working code.
> 
> Any help willbe greatly apreciated.
> 
> thanks,
> 
> Emanuele 
> 
Received on Fri Dec 04 1998 - 14:17:15 NZDT

This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:38 NZDT