Check usage of CPU time by each Process

Chris Kelleher

Administrator
Staff member
Hi

2 questions to ask regarding WIN32 API on NT:

(1) How can we find out the CPU usage and CPU time for
each process?

(2) This is a common problem here in peg list where we
want to kill a process using WIN32 API and end up not
able to open process. Is it really no other way to
kill a process given the former statement is true??

Pls advise. Thanks
 

Chris Kelleher

Administrator
Staff member
> 2 questions to ask regarding WIN32 API on NT:
>
> (1) How can we find out the CPU usage and CPU time for
> each process?

Performance data can be found in the registry, in key HKEY_PERFORMANCE_DATA
especially in the "Process" object. It is hard to understand but there are
helper functions and there is a lot of documentation in MSDN. I did not read
it
close enough to understand it.

function GetProcessTimes in kernel32.dll returns the elapsed time, the
kernel time and the user time.
I don't know how this relates to CPU usage and CPU time, but it may be
usefull.

> (2) This is a common problem here in peg list where we
> want to kill a process using WIN32 API and end up not
> able to open process. Is it really no other way to
> kill a process given the former statement is true??

I suppose not, if you can't open the process with PROCESS_TERMINATE rights
using your current login. The process was probably created by someone else.

Perhaps you can use function CreateProcessAsUser or CreateProcessWithLogon
to launch an application on someone else's account, and pass this
application
the PID of the process you want killed. Just a thought, I have not tried it.


Merry Christmas,
Jurjen.


---------------
Search-engine for Progress related websites:
http://home.wxs.nl/~jurjen.dijkstra/prodevring/ringsearch.html

Windows API examples for Progress 4GL:
http://home.wxs.nl/~jurjen.dijkstra/api/index.html
 

Chris Kelleher

Administrator
Staff member
For Win95 & 98 there was a freeware monitor from Microsoft called WinTop.
It's not very robust, but will give you basic CPU% and such.
With NT press CTRL-ALT-DELTE to pull up the Sys interface and select the
task list option. The screen that appears reminds me up some heart monitor,
but it also has a tab for a Process list which can even be sorted by each
column, GASP!, unlike wintop. The NT version also has a nice Memory column
as well. Wintop has a right-click option on process' in it's list to
terminate, which doesn't alsways work, and to see some memory used.

However netiher of those two things are a programatic solution...

Scott.
 

Chris Kelleher

Administrator
Staff member
The following procedure works under Windows NT. I have not
tested this under Windows 9x. You can retrieve the process ID
by calling the GetWindowThreadProcessId() function. I've
left this as an exercise to the reader.

If the process was created under another account, then you
may need to modify the DACL and/or ACE for the object you
are trying to terminate.

Oh, and please forgive the hungarian notation.

Glenn


DWORD GetWindowThreadProcessId(
HWND hWnd, // handle to window
LPDWORD lpdwProcessId // process identifier
);

BEGIN CODE BLOCK
-----------------

<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
&scop PROCESS_ALL_ACCESS 2035711

procedure KillProc:
def input param nProcId as int no-undo.

/* This procedure attempts to kill a process by its process
id.

!! NOTE !!: The call to TerminateProcess() does not
notify any DLLs the process opened that the process is
exiting. This can result in messy DLL cleanup.

If possible, the process should be terminated by sending
all of it's top level windows the WM_CLOSE message.
*/

def var nProcess as int no-undo.
def var bTermSuccess as int no-undo.

run OpenProcess({&PROCESS_ALL_ACCESS}, 1, nProcId, output nProcess).

if not(nProcess > 0) then do:
message "Error opening handle to process.".
return.
end.

run TerminateProcess(nProcess, 0, output bTermSuccess).

if bTermSuccess <> 0 then
message "Process terminated successfully.".
end procedure.

procedure OpenProcess external "kernel32.dll":
def input param dwDesiredAccess as long.
def input param bInheritHandle as long.
def input param dwProcessId as long.
def return param hProcess as long.
end procedure.

procedure TerminateProcess external "kernel32.dll":
def input param hProcess as long.
def input param uExitCode as long.
def return param bSuccess as long.
end procedure.

[/code]
 
Top