2009-07-29 79 views

回答

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; 
} 
1

我敢肯定,你問有關Windows這裏,但爲了完整起見,我將描述一種方式這可以在Unix系統來完成。

/proc文件系統包含有關計算機上所有正在運行的進程的信息。在這個目錄中,你會發現系統中每個進程的子目錄(由pid命名),這些目錄中的每個進程都是一個名爲stat的文件。看看'man proc'並搜索「stat」條目。該文件包含大量信息,但可以使用幾個字段來確定該進程消耗了多少用戶和內核模式時間。

掌握了這些知識之後,找一個名爲「task」的進程的子目錄...在這裏你會發現外部進程產生的所有子進程..如果你進入這些進程,你會發現每個人都有一個stat文件。

相關問題