2

我正在寻找一个关于如何在 VC++ 的进程中查找单个线程的 CPU 利用率的工具。

如果有人能给我提供一个工具,那就太好了。

如果你们提供如何以编程方式进行操作,那可能会更好。

先感谢您。

4

3 回答 3

3

也许使用GetThreadTimes会有所帮助?

详细说明线程是否属于另一个可执行文件,这将是以下行中的内容(未测试):

// Returns true if thread times could be queried and its results are usable,
// false otherwise. Error handling is minimal, considering throwing detailed
// exceptions instead of returning a simple boolean.
bool get_remote_thread_times(DWORD thread_id, FILETIME & kernel_time, FILETIME & user_time)
{
  FILETIME creation_time = { 0 };
  FILETIME exit_time = { 0 };
  HANDLE thread_handle = OpenThread(THREAD_QUERY_INFORMATION, FALSE, thread_id);
  if (thread_handle == INVALID_HANDLE) return false;

  bool success = GetThreadTimes(thread_handle, &creation_time, &exit_time, &kernel_time, &user_time) != 0;

  CloseHandle(thread_handle);
  return success;
}
于 2009-07-29T12:38:30.640 回答
3

尝试使用进程资源管理器..(工具)..非常有用..

http://download.cnet.com/Process-Explorer/3000-2094_4-10223605.html

于 2009-07-29T12:40:50.487 回答
2

我确定您在这里询问的是 Windows,但为了完整起见,我将描述一种可以在 Unix 系统上完成的方法。

/proc 文件系统包含有关您机器上所有正在运行的进程的信息。在此目录中,您将找到系统上每个进程的子目录(由 pid 命名),每个目录中都有一个名为 stat.h 的文件。查看“man proc”并搜索“stat”条目。该文件包含大量信息,但有几个字段可用于确定此进程消耗了多少用户和内核模式时间。

掌握了这些知识,寻找一个名为“task”的进程的子目录......在这里你会找到由外部进程产生的所有子进程......如果你 cd 进入那些,你会发现每个都有一个统计文件。

于 2009-07-29T13:02:45.683 回答