Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

282 Process Control Chapter 8


(tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);
printf(" child sys: %7.2f\n",
(tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);
}

Figure 8.31Time and execute all command-line arguments

If we run this program, we get
$./a.out "sleep 5" "date" "man bash >/dev/null"

command: sleep 5
real: 5.01
user: 0.00
sys: 0.00
child user: 0.00
child sys: 0.00
normal termination, exit status = 0
command: date
Sun Feb 26 18:39:23 EST 2012
real: 0.00
user: 0.00
sys: 0.00
child user: 0.00
child sys: 0.00
normal termination, exit status = 0
command: man bash >/dev/null
real: 1.46
user: 0.00
sys: 0.00
child user: 1.32
child sys: 0.07
normal termination, exit status = 0
In the first two commands, execution is fast enough to avoid registering any CPU time
at the reported resolution. In the thirdcommand, however, we run a command that
takes enough processing time to note that all the CPU time appears in the child process,
which is wherethe shell and the command execute.

8.18 Summary


Athorough understanding of the UNIX System’s process control is essential for
advanced programming. Thereare only a few functions to master:fork,theexec
family,_exit,wait,andwaitpid.These primitives areused in many applications.
Theforkfunction also gave us an opportunity to look at race conditions.
Our examination of thesystemfunction and process accounting gave us another
look at all these process control functions. We also looked at another variation of the
Free download pdf