Click here to Skip to main content
13,763,729 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 1 Feb 2005

Enum Processes with kernel and user times

, 7 Feb 2005
Rate this:
Please Sign up or sign in to vote.
An article on enumerating processes.

Sample Image -EnumProcesses.jpg


This article shows how to enumerate all the processes currently running on your system and display them with the process name, process ID tag, Kernel time and User time.


Process kernel mode and user mode times are amounts of time. For example, if a process has spent one second in kernel mode, this function will fill the FILETIME structure specified by lpKernelTime with a 64-bit value of ten million. That is the number of 100-nanosecond units in one second. To verify this, I used the following code:

DWORD dwLow, dwLow2, dwRes;
FILETIME ft, ft2;

    GetSystemTime(&st);              // gets current time
    SystemTimeToFileTime(&st, &ft);  // converts to file time format
    dwLow = ft.dwLowDateTime;
    st.wSecond += 1;                 // add 1 second
    SystemTimeToFileTime(&st, &ft2); // converts to file time format
    dwLow2 = ft2.dwLowDateTime;
    dwRes = dwLow2 - dwLow;
    printf("%lu\n", dwRes);
    result: 10,000,000

Using the code

I first saved all the kernel and user times in an array of a structure which saves the 64 bit values in an <CODE lang=mc++>__int64 variable. Then after five seconds, I sample them again and calculate the difference, the difference is then divided by 156250. The number 156250 is 1/64 of the 64-bit value of ten million, the smallest amount of time a process is running. Now the CPU usage can be calculated, the sum of the kernel and user times divided by five. The console functions defined in Console.h are useful in any Win32 console application. The function clrscr(int screenSize) can be called with MINIMUM or MAXIMUM, the first will set the screen buffer size to its normal size of 24 by 80, the second sets it to 60 by 80. The function gotoxy(x, y) sets the cursor at line y character position x. The function setrgb(int fgc, int bgc) is used to set the background and foreground colors. The colors are defined in an enum in the header file, for example, setrgb(BLACK, INT_WHITE) is the default.

<PRE lang=mc++>typedef struct tagPROCESSTIMES { __int64 m_kernel; __int64 m_user; } PROCESSTIMES[100];

Points of Interest

Being basically lazy, I don't like to click the maximize window button and then scroll to the top of the console window. See Console.cpp, I added code to set the console screen buffer to its maximum size when the second screen is called to display the results.


26, January 2005 - Version 1.0.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThanks, Good Job Pin
Member 62587418-Mar-09 16:54
memberMember 62587418-Mar-09 16:54 
GeneralFunction that I build for my application from this article. Pin
AndrewSmirnov30-May-06 6:44
memberAndrewSmirnov30-May-06 6:44 
GeneralConsole Commands Pin
naveenieus3-Apr-06 20:34
membernaveenieus3-Apr-06 20:34 
GeneralRe: Console Commands Pin
Roger654-Apr-06 3:48
memberRoger654-Apr-06 3:48 
GeneralRe: Console Commands Pin
naveenieus4-Apr-06 20:08
membernaveenieus4-Apr-06 20:08 
GeneralPsapi Errors Pin
CyKill22-Mar-05 7:25
memberCyKill22-Mar-05 7:25 
GeneralRe: Psapi Errors Pin
Roger6522-Mar-05 7:36
memberRoger6522-Mar-05 7:36 
GeneralRe: Psapi Errors Pin
k999930-Aug-06 6:50
memberk999930-Aug-06 6:50 
Generalcsrss CPU usage Pin
Nathan Evans8-Feb-05 6:55
memberNathan Evans8-Feb-05 6:55 
GeneralRe: csrss CPU usage Pin
Roger658-Feb-05 8:34
memberRoger658-Feb-05 8:34 
GeneralRe: csrss CPU usage Pin
Nathan Evans8-Feb-05 8:38
memberNathan Evans8-Feb-05 8:38 
GeneralRe: csrss CPU usage Pin
Roger658-Feb-05 10:48
memberRoger658-Feb-05 10:48 
GeneralGood! Pin
WREY3-Feb-05 12:51
memberWREY3-Feb-05 12:51 
GeneralRe: Good! Pin
Roger654-Feb-05 1:55
memberRoger654-Feb-05 1:55 
GeneralRe: Good! Pin
WREY4-Feb-05 3:19
memberWREY4-Feb-05 3:19 
GeneralRe: Good! Pin
Roger654-Feb-05 3:42
memberRoger654-Feb-05 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181113.2 | Last Updated 8 Feb 2005
Article Copyright 2005 by Roger65
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid