第五章 作业

(1)作业是什么?

  你可以把作业想象成一个进程的容器,作用是限制一组进程的属性。读懂下面的示例函数,就差不多理解了。

void StartRestrictedProcess()
{
    bool bInJob =FALSE;   
    IsProcessInJob(GetCurrentProcess(),NULL,&bInJob);   
    if(bInJob)
    {
        Messagebox(TEXT("进程已经存在作业中"),TEXT(""),MB_OK|MB_ICONINFORMATION);
        return;
    }
    
    HANDLE hjob = CreateJobObject(NULL,TEXT("作业"));
    JOBOBJECT_BASIC_LIMIT_INFORMATION jobli ={ 0 };
    jobli.PriorityClass = IDLE_PRIORITYCLASS;//CPU 级别
    jobli.PerJobUserTimeLimit.QuadPart = 10000000;//进程每个时间不能超过1s
    jobli.LimitFlags = JOB_OBJECT_LIMIT_PRIORITY_CLASS | OB_OBJECT_LIMIT_JOB_TIME;
    
    SetInformationJobObject(hjob, JobObjectBasicLimitInformation,&jobli, sizeof(jobli));
   
    JOBOBJECT_BASIC_UI_RESTRICTIONS jobuir;
    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; CreateProcess(NULL, "CMD", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, π); 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; } CloseHandle(pi.hProcess); CloseHandle(hjob); }

 

转载于:https://www.cnblogs.com/WillingCPP/archive/2013/04/05/3001124.html

你可能感兴趣的:(第五章 作业)