0

Hi this is my code that prints the physical memory used by some process displayed in bytes , when i am converting bytes into kb by google converter. The value shown in task manager for memory usage is less than output given by my code. Also i want to know the Cpu used by the same process ? I found this question on Stack overflow,CPU USAGE that provides guidance in knowing cpu usage,but i want to know CPU usage for some Particular process id ,instead of current process as mentioned in my code, Can i achieve the same with the code provided.

Any guidance would be appreciated.

int main( void )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    DWORD processID = 4696;


    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return 1;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {

        printf( "\tWorkingSetSize: %u\n", pmc.WorkingSetSize );
    }

    CloseHandle( hProcess );
    return 0;
}
Community
  • 1
  • 1
user3410246
  • 65
  • 1
  • 9

1 Answers1

1

CPU

From the answer linked, you want to use your 'hProcess' handle instead of the 'self' handle from the sample.

Turn this:

self = GetCurrentProcess();
GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
...

Into this:

GetProcessTimes(hProcess, &ftime, &ftime, &fsys, &fuser);
...

Memory

The Working Set is comprised of Private (heap, stack, etc) + Shared (typically dll/exe code pages). What specific column in Task Manager (and what OS) are you referencing?

josh poley
  • 7,236
  • 1
  • 25
  • 25