Process Times
Every process consumes processor time while executing. UNIX provides standardized interfaces to measure how much CPU time has been used by the current process and its child processes. Process timing information is useful for performance analysis, benchmarking, debugging, profiling, and system monitoring.
Learning Objectives
- Understand process timing.
- Learn the
times()function. - Study the
struct tmsstructure. - Understand CPU time categories.
- Learn about clock ticks.
1. What are Process Times?
Process times represent the amount of CPU time consumed while a process executes. Unlike wall-clock time, which measures elapsed real time, process times measure only the time during which the CPU executes instructions on behalf of the process.
2. Why Measure Process Time?
- Measure application performance.
- Identify CPU-intensive code.
- Benchmark algorithms.
- Profile software execution.
- Compare optimization results.
- Monitor resource usage.
3. times()
The times() function obtains CPU time information for the
calling process and for its terminated child processes.
4. struct tms
The times() function stores timing information in a
struct tms structure.
5. Members of struct tms
| Member | Description |
|---|---|
tms_utime |
User CPU time consumed by the calling process. |
tms_stime |
System CPU time consumed by the calling process. |
tms_cutime |
User CPU time consumed by terminated child processes. |
tms_cstime |
System CPU time consumed by terminated child processes. |
6. User Time vs System Time
| User Time | System Time |
|---|---|
| CPU executes application code. | CPU executes kernel code. |
| Application instructions. | Operating system services. |
| Algorithm execution. | System calls and kernel operations. |
7. Clock Ticks
The values returned by times() are measured in clock ticks,
not seconds.
Applications can determine the number of clock ticks per second by
calling:
8. Process Timing Workflow
9. Practical Applications
| Application | Purpose |
|---|---|
| Performance Profilers | Measure CPU utilization. |
| Benchmark Programs | Compare execution speed. |
| Monitoring Tools | Display process CPU usage. |
| Testing Utilities | Measure algorithm efficiency. |
| Diagnostic Software | Analyze resource consumption. |
10. Interview Questions
- What information does
times()provide? - Which header file declares
times()? - What is stored in
struct tms? - What does
tms_utimerepresent? - What is the difference between user CPU time and system CPU time?
- What information is stored in
tms_cutime? - Why are process times useful?
- Why are process times measured in clock ticks?
- How can clock ticks be converted to seconds?
- Does process time represent elapsed wall-clock time?
11. Return Value of times()
The times() function returns the elapsed real time
expressed in clock ticks.
The CPU usage information is stored separately in the
struct tms structure supplied by the caller.
| Return Value | Description |
|---|---|
| Non-negative | Elapsed real time in clock ticks. |
| -1 | An error occurred. |
12. clock_t Data Type
The clock_t data type represents processor time values
returned by timing functions.
It is defined by the implementation and is used by
times() and other timing-related interfaces.
13. Converting Clock Ticks to Seconds
To obtain CPU time in seconds, divide the number of clock ticks by the
number of clock ticks per second returned by
sysconf(_SC_CLK_TCK).
14. Measuring CPU Time
15. Example Using times()
16. Error Handling
Applications should always verify that
times() does not return
(clock_t)-1 before using the timing information.
17. CPU Time Categories
| Category | Meaning |
|---|---|
| User Time | Time executing application instructions. |
| System Time | Time executing kernel services. |
| Child User Time | User CPU time used by terminated child processes. |
| Child System Time | Kernel CPU time used by terminated child processes. |
18. CPU Time vs Wall Clock Time
| CPU Time | Wall Clock Time |
|---|---|
| Measures processor usage. | Measures elapsed real time. |
| Excludes waiting time. | Includes waiting and sleep time. |
| Used for profiling. | Used for execution duration. |
19. Timing APIs Overview
| API | Purpose |
|---|---|
times() |
Retrieve process CPU usage. |
sysconf() |
Retrieve clock ticks per second. |
clock_t |
Represent processor time values. |
20. Interview Questions
- What does the return value of
times()represent? - What information is stored in
struct tms? - What is the purpose of the
clock_ttype? - How are clock ticks converted into seconds?
- Which function returns the number of clock ticks per second?
- Why should
times()return values always be checked? - What is the difference between CPU time and wall-clock time?
- Which member stores kernel CPU time?
- Which member stores child process user CPU time?
- Why are CPU times expressed in clock ticks instead of seconds?
21. Complete CPU Timing Example
22. Parent and Child Process Times
The times() function records CPU usage for both the
calling process and its terminated child processes.
This enables a parent process to determine how much processor time was
consumed by child processes after they have terminated.
| Member | Represents |
|---|---|
tms_utime |
Current process user CPU time. |
tms_stime |
Current process system CPU time. |
tms_cutime |
Terminated child user CPU time. |
tms_cstime |
Terminated child system CPU time. |
23. Parent-Child Timing Workflow
24. Linux time Command
The time utility measures the execution time of a command.
It reports elapsed time, user CPU time, and system CPU time.
25. Typical Output
| Field | Description |
|---|---|
real |
Elapsed wall-clock time. |
user |
User CPU time. |
sys |
Kernel CPU time. |
26. Interpreting Timing Results
| Observation | Possible Interpretation |
|---|---|
| High user time | CPU-intensive computations. |
| High system time | Frequent system calls or kernel activity. |
| Large real time | Waiting for I/O, scheduling, or other processes. |
| Low CPU time but high real time | Process spent significant time blocked or sleeping. |
27. Best Practices
- Measure user and system CPU time separately.
- Convert clock ticks to seconds before displaying results.
- Use repeated measurements when benchmarking.
- Avoid drawing conclusions from a single execution.
- Measure representative workloads.
28. Common Programming Mistakes
- Confusing elapsed time with CPU time.
- Ignoring child process CPU usage.
- Displaying raw clock ticks without conversion.
- Hardcoding clock tick values.
- Ignoring the return value of
times().
29. Advanced Interview Questions
- What information is returned through the
times()return value? - Why are CPU times separated into user and system categories?
- When are
tms_cutimeandtms_cstimeupdated? - What is the difference between the
timecommand and thetimes()function? - Why should benchmarking use multiple executions?
- What causes high system CPU time?
- Can elapsed time be smaller than CPU time?
- Why should clock tick values never be hardcoded?
- What information does the
realfield represent? - How can blocked I/O affect timing results?
30. CPU Timing Overview
31. Process Timing API Comparison
| API | Purpose |
|---|---|
times() |
Retrieve process CPU usage information. |
sysconf() |
Retrieve clock ticks per second. |
clock_t |
Represent processor time values. |
struct tms |
Store CPU timing information. |
32. Timing Information Summary
| Information | Obtained From |
|---|---|
| User CPU Time | tms_utime |
| System CPU Time | tms_stime |
| Child User CPU Time | tms_cutime |
| Child System CPU Time | tms_cstime |
| Elapsed Clock Ticks | Return value of times() |
33. Complete Timing Lifecycle
34. Best Practices
- Always obtain the clock tick frequency using
sysconf(_SC_CLK_TCK). - Convert timing values to seconds before displaying them.
- Check the return value of
times()for errors. - Measure both user and system CPU time during performance analysis.
- Repeat measurements to reduce the effect of temporary system load.
- Benchmark under consistent test conditions.
35. Common Programming Mistakes
- Using elapsed wall-clock time as CPU usage.
- Ignoring child process timing information.
- Hardcoding clock tick values.
- Forgetting to convert clock ticks into seconds.
- Ignoring errors returned by
times(). - Comparing benchmark results obtained under different workloads.
36. Real-World Applications
| Application | Purpose |
|---|---|
| Performance Profilers | Measure CPU consumption. |
| Benchmark Suites | Compare algorithm efficiency. |
| Monitoring Utilities | Display processor usage. |
| Build Systems | Analyze compilation time. |
| Scientific Computing | Evaluate computation cost. |
37. Advanced Interview Questions
- Why does
times()return elapsed clock ticks in addition to fillingstruct tms? - Explain the purpose of each member of
struct tms. - Why are processor times measured in clock ticks instead of seconds?
- How do you convert clock ticks into seconds?
- What is the difference between elapsed time and CPU time?
- When are child process timing values updated?
- Why should benchmarking be repeated multiple times?
- Why is
sysconf(_SC_CLK_TCK)preferred over hardcoded values? - What happens if
times()fails? - Which timing information is most useful when optimizing CPU-bound applications?
38. Module Summary
This module introduced the UNIX process timing interfaces used to
measure CPU usage. It explained the times() function,
the struct tms structure, user and system CPU time,
child process timing information, clock tick conversion,
performance measurement techniques, practical examples, and
recommended programming practices.
Understanding these interfaces enables developers to evaluate
application performance using standardized POSIX timing facilities.
Concept Mastery Assessment — Module 33
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.