Path: utzoo!attcan!uunet!samsung!usc!apple!altos!megadon!clp From: bhoughto@cmdnfs.intel.com (Blair P. Houghton) Newsgroups: comp.unix Subject: Re: How to find process name in c Message-ID: <2207@megadon.UUCP> Date: 14 Oct 90 18:49:59 GMT Sender: clp@megadon.UUCP Organization: Intel Corp, Chandler, AZ Lines: 50 Approved: clp@megadon.UUCP In-Reply-To: <2206@megadon.UUCP> In article <2206@megadon.UUCP> you write: >In article <2190@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 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."