How to calculate the CPU usage of a process by PID in Linux from C?

You need to parse out the data from /proc/<PID>/stat. These are the first few fields (from Documentation/filesystems/proc.txt in your kernel source): Table 1-3: Contents of the stat files (as of 2.6.22-rc3) …………………………………………………………………… Field Content pid process id tcomm filename of the executable state state (R is running, S is sleeping, D is sleeping in an … Read more

How to get the CPU Usage in C#?

You can use the PerformanceCounter class from System.Diagnostics. Initialize like this: PerformanceCounter cpuCounter; PerformanceCounter ramCounter; cpuCounter = new PerformanceCounter(“Processor”, “% Processor Time”, “_Total”); ramCounter = new PerformanceCounter(“Memory”, “Available MBytes”); Consume like this: public string getCurrentCpuUsage(){ return cpuCounter.NextValue()+”%”; } public string getAvailableRAM(){ return ramCounter.NextValue()+”MB”; }