How to find process name in c

Blair P. Houghton bhoughto at cmdnfs.intel.com
Mon Oct 15 04:49:59 AEST 1990


In article <2206 at megadon.UUCP> you write:
>In article <2190 at megadon.UUCP> you write:
>>Has anyone got a method for finding out if a process is running short of
>>"ps -ef | grep processname" in a pipe.  There must be a better way from
>>within a c program.
>
>Well, you can perform the above task in C by, doing a
>popen("ps -ef", "r");

Here's the funny part.

Using this program:

    /* popenpsef.c -- demo popen() */
    #include <stdio.h>
    main()
    {
	FILE *pp;
	char line[BUFSIZ];

	pp = popen("ps -ef","r");

	while ( fgets(line, sizeof line, pp) != NULL )
	    fputs(line,stdout);
    }

and doing 'time ps -ef' vs. 'time popenpsef' a few times,
I get these results:

    time ps -ef
    0.2u 0.3s 0:00 206% 70+427k 0+0io 0pf+0w
    0.2u 0.3s 0:00 70% 69+435k 0+0io 0pf+0w
    0.2u 0.3s 0:00 206% 71+444k 0+0io 0pf+0w

    time popenpsef
    0.0u 0.0s 0:00 8% 6+15k 0+0io 0pf+0w
    0.0u 0.0s 0:00 9% 7+18k 0+0io 0pf+0w
    0.0u 0.0s 0:00 5% 6+14k 0+0io 0pf+0w

Clearly, ps(1) is doing something slow and large when
connected to a tty that it doesn't do when connected to a
pipe this way.  Despite the "0:00" in the real-time field,
the response is noticeably faster with the popen()'ed version.

				--Blair
				  "Counter-intuition:  when you
				   know that if you want mayo
				   you can't have mustard also,
				   even though you've never
				   tried it."



More information about the Comp.unix mailing list