timing child processes

William Kucharski kucharsk at uts.amdahl.com
Sat Apr 8 17:26:21 AEST 1989


In article <6250 at homxc.ATT.COM> vas1 at 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 at 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.



More information about the Comp.unix.wizards mailing list