Experiment 03 — Process Time Analysis Using /proc

← Back to Index
## Process Time Analysis Using /proc in Linux

----
-> /proc directory acts as a virtual file system that provides detailed information about system resources and active processes.

->  /proc/self refers to the currently executing process.

-> /proc/self/stat contains
	user-mode CPU time (field 14, utime)
	kernel-mode CPU time (field 15, stime)
	(both measured in clock ticks)
-> Clock ticks are converted into seconds using the system-defined value obtained through sysconf(SC_CLK_TCK).

->  User mode time represents the CPU time spent executing program instructions
-> kernel mode time represents time spent executing system-level operations.

->  system time is obtained using the time() and ctime() functions, which provide the current date and time in a human-readable format.

-> dummy process used for calculation 
'''

long long sum = 0;
for (long long i = 0; i < 500000000; i++) {
sum += i;
}

'''
----

----
algorithm

(a) Start the Program

(b) Get the current system time
	i. Use time(now) to read the current time.
	ii. Print it using ctime().

(c) Perform dummy computation
	i. Initialize sum = 0.
	ii. Run a loop from 0 to 500000000 and accumulate the value in sum.
	iii. Print the final value of sum.

(d) Open the /proc/self/stat le
	i. Use fopen("/proc/self/stat", 'r') to access process statistics.
	ii. If the file cannot be opened, display an error and stop the program.

(e) Read the process statistics
	i. Use fgets() to read the complete line into a buffer.
	ii. Close the file.

(f) Extract user-mode and kernel-mode CPU times
	i. Tokenize the buer with strtok() using space as the delimiter.
	ii. Traverse tokens until eld 14 and eld 15 are reached.
	iii. Field 14 → store as utime ticks.
	iv. Field 15 → store as stime ticks.

(g) Convert CPU ticks into seconds
	i. Get the number of ticks per second using sysconf(SC_CLK_TCK)
	ii. Convert:
		long ticks_per_sec = sysconf(_SC_CLK_TCK);

		double user_time_sec = (double) utime_ticks / ticks_per_sec;
		double kernel_time_sec = (double) stime_ticks / ticks_per_sec;
	
(h) Display the results.
	printf("\n=== Process CPU Usage ===\n");
	printf("User mode time : %.6f seconds\n", user_time_sec);
	printf("Kernel mode time : %.6f seconds\n", kernel_time_sec);

(i) End of Program.

----
----
code (in c)

------
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int main() {

// 1. Print current system time
	time_t now;
	time(&now);
	printf("Current System Time: %s", ctime(&now));


// 2. Do some dummy computation
	long long sum = 0;
	for (long long i = 0; i < 500000000; i++) {
		sum += i;
	}
	printf("Dummy computation result: %lld\n", sum);


// 3. Read /proc/self/stat to get CPU times
	FILE *fp = fopen("/proc/self/stat", "r");
	if (fp == NULL) {
		perror("Error opening /proc/self/stat");
		return 1;
	}

	int i;
	char buffer[4096];
	long utime_ticks, stime_ticks;


// Read entire line
	fgets(buffer, sizeof(buffer), fp);
	fclose(fp);

// Tokenize
	char *token = strtok(buffer, " ");
	for (i = 1; i <= 15; i++) {
		if (i == 14) utime_ticks = atol(token);
		if (i == 15) stime_ticks = atol(token);
		token = strtok(NULL, " ");
	}


// Get system clock ticks per second
	long ticks_per_sec = sysconf(_SC_CLK_TCK);
	double user_time_sec = (double) utime_ticks / ticks_per_sec;
	double kernel_time_sec = (double) stime_ticks / ticks_per_sec;

// Print results
	printf("\n=== Process CPU Usage ===\n");
	printf("User mode time : %.6f seconds\n", user_time_sec);
	printf("Kernel mode time : %.6f seconds\n", kernel_time_sec);
	
	return 0;
}
------
-------
output 

┌──(akhil㉿akhil)-[~/os-lab]
└─$ nano exp-3.c

┌──(akhil㉿akhil)-[~/os-lab]
└─$ gcc exp-3.c

┌──(akhil㉿akhil)-[~/os-lab]
└─$ ./a.out
Current System Time: Tue May 19 15:21:13 2026
Dummy computation result: 124999999750000000

=== Process CPU Usage ===
User mode time : 0.340000 seconds
Kernel mode time : 0.000000 seconds
-------