How to Calculate CPU Usage of a Process by PID in Linux Using C

Dec 04, 2025 · Programming · 11 views · 7.8

Keywords: C programming | Linux | CPU usage | /proc file system | process monitoring

Abstract: This article explains how to programmatically calculate the CPU usage percentage for a given process ID in Linux using the C programming language. It covers reading data from the /proc file system, sampling CPU times, and applying the calculation formula, with code examples and best practices for system monitoring.

Introduction

In Linux, monitoring CPU usage for specific processes is essential for performance analysis and system management. This article details how to programmatically calculate the CPU usage percentage for a given process ID using the C programming language.

Understanding the /proc File System

The /proc file system in Linux provides a virtual interface to kernel data structures. To access process-specific CPU time, we read from /proc/<PID>/stat, where <PID> is the process ID. The relevant fields are utime and stime, representing time spent in user mode and kernel mode, respectively, measured in jiffies.

Reading System CPU Time

To calculate usage percentage, the total CPU time is needed. This is obtained from /proc/stat, specifically the cpu line, which lists cumulative CPU times in jiffies. The sum of these values gives time_total.

Calculation Method

CPU usage over a sampling period is calculated by taking two snapshots: at time T1 and after a short interval (e.g., 1 second) at T2. Use the formula:

user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before);
sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before);
total_util = user_util + sys_util;

This yields the CPU usage percentage for the process.

Code Example in C

Here is a simplified C code snippet to demonstrate the approach:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// Function prototypes
long read_utime(pid_t pid);
long read_stime(pid_t pid);
long read_total_cpu_time();

int main() {
    pid_t pid = 1234; // Example PID
    long utime1, stime1, total1;
    long utime2, stime2, total2;
    
    // First reading
    utime1 = read_utime(pid);
    stime1 = read_stime(pid);
    total1 = read_total_cpu_time();
    
    sleep(1); // Sleep for 1 second
    
    // Second reading
    utime2 = read_utime(pid);
    stime2 = read_stime(pid);
    total2 = read_total_cpu_time();
    
    // Calculate usage
    double user_util = 100.0 * (utime2 - utime1) / (total2 - total1);
    double sys_util = 100.0 * (stime2 - stime1) / (total2 - total1);
    double total_util = user_util + sys_util;
    
    printf("CPU Usage: User: %.2f%%, System: %.2f%%, Total: %.2f%%\n", user_util, sys_util, total_util);
    
    return 0;
}

// Implement read functions by parsing /proc files

Considerations and Best Practices

Ensure error handling when reading files, as processes may terminate. The unit jiffies may vary per system; convert to seconds if needed by dividing by sysconf(_SC_CLK_TCK). Choose an appropriate sampling interval to balance accuracy and overhead.

Conclusion

This method provides a reliable way to monitor process CPU usage in Linux using C, leveraging the /proc file system for real-time data access.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.