日期:2014-05-17  浏览次数:20821 次

Win核心编程作业章中PerJobUserTimeLimit的一些疑问
问题1:JOBOBJECT_BASIC_LIMIT_INFORMATION结构体中的PerJobUserTimeLimit的单位是100-ns,这个100-ns与秒的换算关系?

书上(P122)写的是 1秒 == 10000 (100-ns)
百度一下好像是: 1秒 = 1000毫秒(ms) = 1000 * 1000 微秒(μs) = 1000 * 1000 * 1000 纳秒(ns)

这样两种是有冲突的,到底是哪一个对(书上应该不会出现这种错误)

问题2:设置分配给作业对象的最大用户模式时间为 10000 (单位),在进程结束后,
使用GetProcessTimes获得用户模式下的时间却不是 10000 (单位), 这是为什么?



附代码:基本与书上一致

C/C++ code

#include "stdafx.h"


void StartRestrictedProcess()
{
    //检查当前进程是否处于作业中
    BOOL bInJob = FALSE;
    IsProcessInJob(GetCurrentProcess(), NULL, &bInJob);
    if (bInJob)
    {
        MessageBox(NULL, _T("当前进程已经在作业中"), NULL,MB_ICONINFORMATION | MB_OK);
        return;
    }

    //创建作业对象
    HANDLE hJob = CreateJobObject(NULL, _T("RestrictedProcessJob"));

    //限制条件1
    JOBOBJECT_BASIC_LIMIT_INFORMATION jobli = { 0 };
    jobli.PriorityClass = IDLE_PRIORITY_CLASS;
    jobli.PerJobUserTimeLimit.QuadPart = 10000;
    jobli.LimitFlags = JOB_OBJECT_LIMIT_PRIORITY_CLASS | JOB_OBJECT_LIMIT_JOB_TIME;
    SetInformationJobObject(hJob, JobObjectBasicLimitInformation, &jobli, sizeof(jobli));

    //2
    JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir;            //dword
    jobuir.UIRestrictionsClass = JOB_OBJECT_UILIMIT_NONE;            //没有限制
    jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_EXITWINDOWS;
    jobuir.UIRestrictionsClass |= JOB_OBJECT_UILIMIT_HANDLES;

    SetInformationJobObject(hJob, JobObjectBasicUIRestrictions, &jobuir, sizeof(jobuir));

    //创建子进程
    STARTUPINFO si = {sizeof(si)};
    PROCESS_INFORMATION pi;
    TCHAR szExe[] = _T("D:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\Tools\\spyxx.exe");
    CreateProcess(NULL, szExe, NULL, NULL, FALSE, CREATE_SUSPENDED | CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);

    //与作业关联
    AssignProcessToJobObject(hJob, pi.hProcess);

    ResumeThread(pi.hThread);

    CloseHandle(pi.hThread);


    //等待子进程返回
    HANDLE h[2];
    h[0] = pi.hProcess;
    h[1] = hJob;

    DWORD dw = WaitForMultipleObjects(2, h, FALSE, INFINITE);
    switch (dw - WAIT_OBJECT_0)
    {
    case 0:
        //子进程结束返回
        break;
    case 1:
        //时间用完
        break;
    }

    //获取进程相关的一些时间信息
    FILETIME CreateTime;
    FILETIME ExitTime;
    FILETIME KernelTime;
    FILETIME UserTime;
    GetProcessTimes(pi.hProcess, &CreateTime, &ExitTime, &KernelTime, &UserTime);

    TCHAR szInfo[MAX_PATH];
    StringCchPrintf(szInfo, MAX_PATH, _T("Kernel = %u | User = %u\n"), KernelTime.dwLowDateTime / 10000, UserTime.dwLowDateTime / 10000);
    MessageBox(GetActiveWindow(), szInfo, NULL, MB_OK | MB_ICONINFORMATION);

    //关闭句柄
    CloseHandle(hJob);
    CloseHandle(pi.hProcess);

}



int _tmain(int argc, _TCHAR* argv[])
{
    StartRestrictedProcess();

    return 0;
}




运行结果:
+ KernelTime {dwLowDateTime=625000 dwHighDateTime=0 } _FILETIME
+ UserTime {dwLowDateTime=468750 dwHighDateTime=0 } _FILETIME

或者其它不同的结果
+ KernelTime {dwLowDateTime=1093750 dwHighDateTime=0 } _FILETIME
+ UserTime {dwLowDateTime=312500 dwHighDateTime=0 } _FILETIME



------解决方案--------------------
纳秒的确是千分之一微秒的。但是你这里的是:-ns 前面那个负号是啥意思啊?这本书我没看过。不清楚