[comp.unix.questions] "Monitoring" a process

rickc@telly.on.ca (Rick Copley) (01/22/91)

Can anyone out there tell me how to tell if a given process is actually
doing any thing (ie accumulating cpu time) from within a C program ??

On this NCR TOWER 32/800 running AT&T s5r3 (hacked by NCR, 2.00.02 is the NCR 
version number), I have found a file called /usr/include/sys/proc.h that seems 
to have all the stuff I need to "monitor" a process, but I can't figure out what
"function" to call to actually use it.

Reason I am looking is we have a problem with users NOT login off, and causing
swappage for the other users.  They are the kind that login, type for 20 seconds
and leave the terminal logged in for hours and hours.  I have taken a C 
program from UnixWorld Mag that looks at the users /dev/tty file to see when 
the last time they TYPED was. This is no go for when a user runs a very lengthy 
report that does no screen I/O.  The C program (called autologout.c) murders 
EVERYONE who does not access his/her tty port for more than a certain amount of 
time.

I realize I could muck around with the ps -t command and capture the cpu time
in a file and compare it to a few minutes ago and stuff like that, but it seems
like there should be a REALLY easy way to check if a process is "running" and 
accumulating cpu time.

Help, anyone ???

-- 

rickc@telly.on.can   "You can twist perception, but reality wont budge" - RUSH

uunet!telly.on.ca!rickc (Rick Copley)

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (01/22/91)

rickc@telly.on.ca (Rick Copley) wrote:
>Can anyone out there tell me how to tell if a given process is actually
>doing any thing (ie accumulating cpu time) from within a C program ??
>
>I realize I could muck around with the ps -t command and capture the cpu time
>in a file and compare it to a few minutes ago and stuff like that, but it seems
>like there should be a REALLY easy way to check if a process is "running" and 
>accumulating cpu time.

Yes, there should be, but there isn't.  You need to read /vmunix's name
list, open /dev/kmem, and traverse the kernal process table.  It's not
pretty at all.  Reading the output of some variant of ps is probably the
easiest solution in this case.

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
In the heat of composition I find that I have inadvertently allowed
myself to assume the form of a large centipede.  I am accordingly
dictating the rest to my secretary.

mike (Michael Stefanik) (01/23/91)

In article <279B5779.58AF@telly.on.ca> telly.on.ca!rickc (Rick Copley) writes:
>Can anyone out there tell me how to tell if a given process is actually
>doing any thing (ie accumulating cpu time) from within a C program ??
>[...]
>I realize I could muck around with the ps -t command and capture the cpu time
>in a file and compare it to a few minutes ago and stuff like that, but it seems
>like there should be a REALLY easy way to check if a process is "running" and 
>accumulating cpu time.

Unfortunately, this is one of the significant failings of UNIX ... it doesn't
provide any kernel function that reads the namelist on behalf of a process.
It isn't that reading the namelist is so tough either ... the problem is that
every flavor of UNIX loves to give it a different symbol name. :-(

IMHO, there should be a function called procstat() that returns a pointer to
an array of 'proc' structs.  Of course, it'll never happen.
-- 
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."

emv@ox.com (Ed Vielmetti) (01/23/91)

In article <279B5779.58AF@telly.on.ca> rickc@telly.on.ca (Rick Copley) writes:

   Can anyone out there tell me how to tell if a given process is actually
   doing any thing (ie accumulating cpu time) from within a C program ??

pmon does this.  it runs at least on decs, look on gatekeeper.  dunno
about system v.

--Ed

shore@mtxinu.COM (Melinda Shore) (01/24/91)

In article <382@bria> bria!mike (Michael Stefanik) writes:
>IMHO, there should be a function called procstat() that returns a pointer to
>an array of 'proc' structs.  Of course, it'll never happen.

That's not very general.  There are many kernel data structures that
are interesting/useful/whatever, and you shouldn't need a different
system call for each one.  Instead, you can use something like the
table system call (which Mach HAS) or the tabinfo/tabread system calls
(which Unicos HAS).  Also, from within the kernel you don't have to
read the namelist, because, of course, addresses are known at link
time.  And names of kernel data structures don't vary that much within
major Unix strains (or even among them - is there a Unix out there where
the name of the proc table isn't "proc"?).  You'll find sysinfo in just
about any sysV variant there is, for example.

[BTW, I would have sent you email about this but your "Reply-to:"
line totally loses and you have no From: line in your header.]
-- 
               Software longa, hardware brevis
Melinda Shore                                 shore@mtxinu.com
mt Xinu                              ..!uunet!mtxinu.com!shore