#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <WinBase.h>
int _tmain(int argc, _TCHAR* argv[]){
HANDLE hjob=CreateJobObject(NULL,NULL);//name can be null
JOBOBJECT_BASIC_LIMIT_INFORMATION jobin={0};
jobin.PriorityClass=IDLE_PRIORITY_CLASS;
LARGE_INTEGER li;
li.QuadPart=100000;
jobin.PerJobUserTimeLimit=li;//100ns as a "1"
jobin.LimitFlags=JOB_OBJECT_LIMIT_PRIORITY_CLASS|JOB_OBJECT_LIMIT_JOB_TIME;
SetInformationJobObject(hjob,JobObjectBasicLimitInformation,&jobin,sizeof(jobin));
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
CreateProcess(NULL,"..\\Debug\\client.exe",0,0,FALSE,CREATE_SUSPENDED,0,0,&si,&pi);
AssignProcessToJobObject(hjob,pi.hProcess);
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
HANDLE h[2]={pi.hProcess,hjob};
DWORD dw=WaitForMultipleObjects(2,h,FALSE,INFINITE);
switch(dw-WAIT_OBJECT_0){
case 0:
printf("进程自行中止\n");
break;
case 1:
printf("进程cpu时间用完,强行中止\n");
break;
}
CloseHandle(pi.hProcess);
CloseHandle(hjob);
TerminateJobObject(hjob,0);
return 0;
}
|