Measuring Process Execution Time
Experiment: Measuring Process Execution Time Using /proc Filesystem
🎯 Aim:
To write a simple C program that prints the system time, execute it, and then use the /proc filesystem to determine how long the process ran in user mode and kernel mode.
🧩 Theory:
When a program runs on Linux, the CPU executes its instructions in two modes:
-
User mode: executing code in user space (e.g., arithmetic, loops)
-
Kernel mode: executing system calls (e.g., file I/O, printing)
Linux maintains process statistics in /proc/[pid]/stat, which includes:
-
utime – Time spent in user mode (in clock ticks)
-
stime – Time spent in kernel mode (in clock ticks)
By examining these fields after the program finishes, we can estimate how long it ran in each mode.
🧪 Step 1: Write the C Program
Create a file named time_display.c:
🧪 Step 2: Compile and Run the Program
Sample Output:
🧪 Step 3: Find the Process ID (PID)
You can run the program in the background to capture its PID:
Sample Output:
Here, the PID of the process is 4512.
🧩 Step 4: Check Process Statistics from /proc
While the process is still running (or immediately after starting it), view its status using:
This outputs a long line with many space-separated fields.
Example (shortened):
4512 (time_display) R 4498 4512 4498 0 -1 4194560 186 0 0 0 0 1 0 0 20 0 1 0 123456 123456 0 0 0 0 0 0 0 0 0 0 0 0🧩 Step 5: Identify the utime and stime Fields
According to the Linux documentation (man proc), the fields are:
| Field | Description |
|---|---|
| 14 | utime – Time spent in user mode (in clock ticks) |
| 15 | stime – Time spent in kernel mode (in clock ticks) |
To extract just these values:
Sample Output:
🧩 Step 6: Convert Clock Ticks to Seconds
Linux measures CPU time in clock ticks, which vary by system.
To find the number of clock ticks per second:
Typical output:
This means 100 ticks = 1 second.
Now convert manually:
-
User time = 2 / 100 = 0.02 seconds
-
Kernel time = 1 / 100 = 0.01 seconds
So the process ran for about 0.03 seconds total.
🧩 Step 7: Verify with /proc/[pid]/status (Alternative)
You can also read:
However, note that in many systems these fields are not shown directly here — /proc/[pid]/stat is more reliable for precise values.
🧩 Step 8: Explanation
| Field | Meaning |
|---|---|
utime | CPU time spent executing user instructions |
stime | CPU time spent in system calls or kernel code |
CLK_TCK | System constant defining ticks per second |
📘 Example Summary
| Parameter | Value |
|---|---|
| PID | 4512 |
| utime | 2 |
| stime | 1 |
| CLK_TCK | 100 |
| User time | 0.02 s |
| Kernel time | 0.01 s |
| Total CPU time | 0.03 s |
✅ Result:
The program executed for approximately:
-
0.02 seconds in user mode
-
0.01 seconds in kernel mode
Total execution time ≈ 0.03 seconds
The /proc filesystem provides a detailed breakdown of CPU usage for any process, helping students understand how user and kernel activities are measured separately in Linux.
🧩 Discussion Points for Students
-
User time increases with CPU-bound operations.
-
Kernel time increases with I/O-bound operations or system calls (e.g., printing, file read/write).
-
You can modify the program to include loops or file operations and observe how
utimeandstimechange.
🧠 Extension Exercise
Modify the program to perform a large computation:
Then compare utime and stime again — you’ll notice user time increases significantly because the CPU spends more time executing user instructions.
Comments
Post a Comment