This application develops a program, written in C++ MFC, which displays all running processes with CPU usage, capable to run on a development board OMAP3530 EVM.
GetRunningProcesses() uses the ToolHelpAPI to enumerate all of the running processes, puts them in the process list with all of the information available about the process.
CreateToolhelp32Snapshot() is taking a snapshot of the running processes. The first parameter is a set of flags that request what we want the function to do for us. To get a snapshot of the running processes use TH32CS_SNAPPROCESS. The second parameter is ignored when using TH32CS_SNAPPROCESS, so set it to zero.
Then the pProcess array will be filled with information about the running processes. Start by clearing the first entry in the list and setting the size of the structure in the structure. Then call Process32First to get the first process information. If there is a first process get the rest of the processes. For this it will use Process32Next, which takes two parameters. The first is the snapshot; the second is the result of the previous call to Process32Next or Process32First. For that it is necessary to copy the result to the next entry in the array.
For calculating the CPU usage of processes we need a function called GetThreadTimes from which we will use only the last two parameters (KernelTime and UserTime). To use GetThreadTimes first we have to use the SetProcPermissions function to set the internal permissions bitmask for the thread.
To calculate the CPU usage of processes is necessary to get a value that indicates for how much time they have used the processor in a certain period of time, this value is equal to the sum of the time that the kernel and the user have spent on these processes. |