Windows内核之作业
1 定义:
作业是一种内核对象,它相当于一个容器,里面可以加入很多进程,通过配置作业,可以让作业内的进程具有相同的配置,例如进程的最大执行时间,cpu片占用时间等。
2 作业的创建相关函数
HANDLE CreateJobObject(LPSECURITY_ATTRIBUTESlpJobAttributes,LPCTSTR lpName); HANDLE OpenJobObject(DWORD dwDesiredAccessBOOL bInheritHandles,LPCTSTR lpName);
3 对作业中进程中的限制
BOOL SetInformationObject(HANDLEhJob,JOBOBJECTINFOCLASS JobObjectInfoClass LPVOID lpJobInfo DWORDcbJobObjectInfoLength );
通过对作业设置限制条件,便可以做到对作业中进程的统一限制。作业中进程的限制有四种,如下所示:
3.1 JOBOBJECT_BASIC_LIMIT_INFORMATION
typedef struct _JOBOBJECT_BASIC_LIMIT_INFORMATION { LARGE_INTEGER PerProcessUserTimeLimit; LARGE_INTEGER PerJobUserTimeLimit; DWORD LimitFlags; SIZE_T MinimumWorkingSetSize; SIZE_T MaximumWorkingSetSize; DWORD ActiveProcessLimit; ULONG_PTR Affinity; DWORD PriorityClass; DWORD SchedulingClass; }JOBOBJECT_BASIC_LIMIT_INFORMATION,*PJOBOBJECT_BASIC_LIMIT_INFORMATION;
3.2 JOBOBJECT_EXTENDED_LIMIT_INFORMATIO
typedef struct _JOBOBJECT_EXTENDED_LIMIT_INFORMATION { JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; IO_COUNTERS IoInfo; SIZE_T ProcessMemoryLimit; SIZE_T JobMemoryLimit; SIZE_T PeakProcessMemoryUsed; SIZE_T PeakJobMemoryUsed; } JOBOBJECT_EXTENDED_LIMIT_INFORMATION, *PJOBOBJECT_EXTENDED_LIMIT_INFORMATION;
<1> 包含基本的限制信息
<2> 保留
<3> 设置每个内存的内存限制
<4> 设置每个工作的内存限制
<5> 指明所有进程中已经使用内存的最大的
<6> 指明所有进程已经是用的内存的最大值
3.3 JOBOBJECT_BASIC_UI_RESTRICTIONS
typedef struct _JOBOBJECT_BASIC_UI_RESTRICTIONS { DWORD UIRestrictionsClass; } JOBOBJECT_BASIC_UI_RESTRICTIONS, *PJOBOBJECT_BASIC_UI_RESTRICTIONS;
typedef struct _JOBOBJECT_SECURITY_LIMIT_INFORMATION { DWORD SecurityLimitFlags ; HANDLE JobToken ; PTOKEN_GROUPS SidsToDisable ; PTOKEN_PRIVILEGES PrivilegesToDelete ; PTOKEN_GROUPS RestrictedSids ; } JOBOBJECT_SECURITY_LIMIT_INFORMATION, *PJOBOBJECT_SECURITY_LIMIT_INFORMATION ;<1> 指明是否不允许管理员访问、不允许无限制的标记访问、强制使用特定的访问标记,或者停用某些安全性标识符和优先权
BOOL QueryInformationJobObject( HANDLE hJob, // handle to job JOBOBJECTINFOCLASS JobObjectInfoClass, // information class LPVOID lpJobObjectInfo,// limit information DWORD cbJobObjectInfoLength,// limit information size LPDWORD lpReturnLength / /data written);
BOOL AssignProcessToJobObject( HANDLE hJob, // handle to job HANDLE hProcess // handle to process);当进程产生新的进程的时候,子进程会自动成为父进程作业的一部分
BOOL TerminateJobObject( HANDLE hJob, // handle to job UINT uExitCode // exit code);
通过此函数来使作业中的所有进程停止运行。
原文地址:http://blog.csdn.net/ddupd/article/details/26962039