[comp.unix.wizards] timing child processes

vas1@homxc.ATT.COM (S.PUTCHA) (04/08/89)

Is there an easy way to kill a process after it uses up a certain
amount of CPU time (Note: NOT REAL time)? The times() system command
does return CPU times of child processes. Is there a way to monitor
this time ? The problem with time is that it doesnt give you the
CPU process time unless you wait() on it.

Thanks !

kucharsk@uts.amdahl.com (William Kucharski) (04/08/89)

In article <6250@homxc.ATT.COM> vas1@homxc.ATT.COM (S.PUTCHA) writes:
 >Is there an easy way to kill a process after it uses up a certain
 >amount of CPU time (Note: NOT REAL time)? The times() system command
 >does return CPU times of child processes. Is there a way to monitor
 >this time ? The problem with time is that it doesnt give you the
 >CPU process time unless you wait() on it.
 >
 >Thanks !

Since you're mailing from an AT&T site, I'm not sure how much this will help.

If you're running on a BSD or any one of several "SYSV w/BSD extras" systems,
you can use the setitimer(2) call and set the ITIMER_VIRTUAL timer for the
amount of time you need to pass before timeout.  Setitimer(2) will send you
a SIGVTALRM when your time is up.

If you're running straight SYSV, the times(2) call returns the amount of time
the calling process has been running as well, so you could probably hack
together some type of pseudo-setitimer that might kill off the process
after (time >= timeoutvalue), depending on how accurate you need it.
Other than that, perhaps you could (ugh! are you ready?) fork off a process
to continue with your main program, and have the original process loop,
checking the CPU time used by its child process (your original program), 
and have it fire off a SIGALRM when the time is >= the amount of time you want 
the child to run...

Of course, remember that the times returned by times(2) are in "ticks," and
some conversion will be necessary if you want the value to represent a 
"real" value by using the HZ variable defined in param.h.

-- 
					William Kucharski

ARPA: kucharsk@uts.amdahl.com
UUCP: ...!{ames,decwrl,sun,uunet}!amdahl!kucharsk

Disclaimer:  The opinions expressed above are my own, and may not agree with
	     those of any other sentient being, not to mention those of my 
	     employer.  So there.

arosen@hawk.ulowell.edu (MFHorn) (04/09/89)

From article <6250@homxc.ATT.COM>, by vas1@homxc.ATT.COM (S.PUTCHA):
> Is there an easy way to kill a process after it uses up a certain
> amount of CPU time (Note: NOT REAL time)?

You could use the setrlimit system call.  Setrlimit is passed a pointer
to an 'rlimit' struct as defined in /usr/include/sys/resource.h .  When
the process uses whatever CPU time you specify, it gets sent a SIGXCPU.

This applies to BSD.  I'm not sure about SysV.

--
Andy Rosen           | arosen@hawk.ulowell.edu | "I got this guitar and I
ULowell, Box #3031   | ulowell!arosen          |  learned how to make it
Lowell, Ma 01854     |                         |  talk" -Thunder Road
		RD in '88 - The way it should've been

vas1@homxc.ATT.COM (S.PUTCHA) (04/10/89)

In article <6250@homxc.ATT.COM>, vas1@homxc.ATT.COM (S.PUTCHA) writes:
> Is there an easy way to kill a process after it uses up a certain
> amount of CPU time (Note: NOT REAL time)? The times() system command
> does return CPU times of child processes. Is there a way to monitor
> this time ? The problem with time is that it doesnt give you the
> CPU process time unless you wait() on it.
> 
> Thanks !

I failed to mention that I am running sys V. Sorry about that.    
Vas Putcha