Discovery: how to get process status using pid

From: Johan Hartzenberg (jhartzen@csc.com)
Date: Fri May 24 2002 - 03:39:40 EDT


Hi Gurus,

This question prompted me to try and find the answer myself, purely for
curiosity's sake. I have discovered something very very strange!

After scanning through a few man pages trying to look for a system
function, I did a truss -aef -o /tmp/ps.truss.out ps -o s -p $$

(using ksh, $$ gives 580 in the example below)

The way the execution seems to first check the platform specific stuff and
then execute the correct "ps" command.

Then the ps started to open the files under /proc/*/psinfo ... Every
single one of them! Only immediately after process 580 did it write a
single " S\n" to standard out. See the extract from the truss below.

My guess is that the odd getdents in between is to cater for processes that
got created or terminated during the time ps had been running. My surprise
is at the seeming inefficient coding! Surely when specifying a list of
process IDs, ps only need to open and actually read from the psinfo file
belonging to the relevant process(es)! Granted the /proc structure is in
memory, but does that justify going through 1000s of processes and actually
making three system calls per process, just to print the info for a single
process? Back in programming class I learned that calling a function is
CPU expensive, despite it's other advantages (Eg the cost of stack
operations vs simplified coding). To be sure I compared the results
between Solaris 7 and 8, and both acts the same. Also I compared the
results between an E250 and an E10K domain, and both were the same.

Any comments/thoughts?

> zactomud102 [/tmp] # more ps.truss.out
14693: execve("/usr/bin/ps", 0xFFBEFD44, 0xFFBEFD5C) argc = 5
14693: argv: ps -o s -p 580

[snip]

14693: open("/proc/12657/psinfo", O_RDONLY) = 4
14693: read(4, "\f\0 B\b\0\0\004\0\0 1 q".., 416) = 416
14693: close(4) = 0
14693: open("/proc/608/psinfo", O_RDONLY) = 4
14693: read(4, "\b01 B\b\0\0\001\0\002 `".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12862/psinfo", O_RDONLY) = 4
14693: read(4, "\f\0 B\b\0\0\004\0\0 2 >".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12516/psinfo", O_RDONLY) = 4
14693: read(4, "\f\002\b\0\0\003\0\0 0E4".., 416) = 416
14693: close(4) = 0
14693: getdents(3, 0x10010F378, 1048) = 1024
14693: open("/proc/11552/psinfo", O_RDONLY) = 4
14693: read(4, "\f\002\b\0\0\003\0\0 - ".., 416) = 416
14693: close(4) = 0
14693: open("/proc/13035/psinfo", O_RDONLY) = 4
14693: read(4, "\f\002\b\0\0\003\0\0 2EB".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12581/psinfo", O_RDONLY) = 4
14693: read(4, "\b02 B\b\0\0\001\0\0 1 %".., 416) = 416
14693: close(4) = 0
14693: open("/proc/5937/psinfo", O_RDONLY) = 4
14693: read(4, "\f\0 B\b\0\0\005\0\017 1".., 416) = 416
14693: close(4) = 0
14693: open("/proc/5446/psinfo", O_RDONLY) = 4
14693: read(4, "\b\002\b\0\0\001\0\015 F".., 416) = 416
14693: close(4) = 0
14693: open("/proc/29660/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\0 sDC".., 416) = 416
14693: close(4) = 0
14693: open("/proc/14691/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\0 9 c".., 416) = 416
14693: close(4) = 0
14693: open("/proc/580/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\002 D".., 416) = 416
14693: close(4) = 0
14693: write(1, " S\n", 2) = 2
14693: open("/proc/29964/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\0 u\f".., 416) = 416
14693: close(4) = 0
14693: open("/proc/14365/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\0 81D".., 416) = 416
14693: close(4) = 0
14693: open("/proc/7102/psinfo", O_RDONLY) = 4
14693: read(4, "\b\0 B\b\0\0\001\0\01BBE".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12633/psinfo", O_RDONLY) = 4
14693: read(4, "\b02 B\b\0\0\001\0\0 1 Y".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12609/psinfo", O_RDONLY) = 4
14693: read(4, "\f\002\b\0\0\003\0\0 1 A".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12582/psinfo", O_RDONLY) = 4
14693: read(4, "\b0202\b\0\0\001\0\0 1 &".., 416) = 416
14693: close(4) = 0
14693: open("/proc/12610/psinfo", O_RDONLY) = 4
14693: read(4, "\f01 B\b\0\0\0\n\0\0 1 B".., 416) = 416
14693: close(4) = 0
[snip]

                                                                                                                                
                    Vijay ch
                    <vijay_chint To: sunmanagers@sunmanagers.org
                    @yahoo.com> cc:
                    Sent by: Subject: how to get process status using pid
                    sunmanagers-a
                    dmin
                                                                                                                                
                                                                                                                                
                    24/05/2002
                    03:17 AM
                                                                                                                                
                                                                                                                                

Hi,
  How to get the process status (Running/Stopped)
using pid, using C in solaris.

Thanks in Advance
Vijay
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:24:22 EDT