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:

#include <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("Current system time: %s", ctime(&now)); return 0; }

🧪 Step 2: Compile and Run the Program

gcc time_display.c -o time_display ./time_display

Sample Output:

Current system time: Mon Oct 28 11:37:21 2025

🧪 Step 3: Find the Process ID (PID)

You can run the program in the background to capture its PID:

./time_display &

Sample Output:

[1] 4512 Current system time: Mon Oct 28 11:37:25 2025

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:

cat /proc/4512/stat

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:

FieldDescription
14utime – Time spent in user mode (in clock ticks)
15stime – Time spent in kernel mode (in clock ticks)

To extract just these values:

awk '{print "utime (user mode): "$14, "\nstime (kernel mode): "$15}' /proc/4512/stat

Sample Output:

utime (user mode): 2 stime (kernel mode): 1

🧩 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:

getconf CLK_TCK

Typical output:

100

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:

cat /proc/4512/status | grep -E "utime|stime"

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

FieldMeaning
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 utime and stime change.


🧠 Extension Exercise

Modify the program to perform a large computation:

for (long i = 0; i < 100000000; i++);

Then compare utime and stime again — you’ll notice user time increases significantly because the CPU spends more time executing user instructions.

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command