21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

cause any process exceeding any of the imposed limits to be terminated by the operating<br />

system.<br />

The first step in using job objects on Windows is to create a job control object. This<br />

is done by calling CreateJobObject( ), which requires a set of security attributes in a<br />

SECURITY_ATTRIBUTES structure and a name for the job object. The job object may be<br />

created without a name, in which case other processes cannot open it, making the<br />

job object private to the process that creates it and its children. If the job object is<br />

created successfully, CreateJobObject( ) returns a handle to the object; otherwise, it<br />

returns NULL, and GetLastError( ) can be used to determine what caused the failure.<br />

With a handle to a job object in hand, restrictions can be placed on the processes<br />

that run within the job using the SetInformationJobObject( ) function, which has the<br />

following signature:<br />

BOOL SetInformationJobObject(HANDLE hJob, JOBOBJECTINFOCLASS JobObjectInfoClass,<br />

LPVOID lpJobObjectInfo, DWORD cbJobObjectInfoLength);<br />

This function has the following arguments:<br />

hJob<br />

Handle to a job object created with CreateJobObject( ), or opened by name with<br />

OpenJobObject( ).<br />

JobObjectInfoClass<br />

Predefined constant value used to specify the type of restriction to place on the<br />

job object. Several constants are defined, but we are only interested in two of<br />

them: JobObjectBasicLimitInformation and JobObjectExtendedLimitInformation.<br />

lpJobObjectInfo<br />

Pointer to a filled-in structure that is either a JOBOBJECT_BASIC_LIMIT_INFORMATION<br />

or a JOBOBJECT_EXTENDED_LIMIT_INFORMATION, depending on the value specified<br />

for JobObjectInfoClass.<br />

cbJobObjectInfoLength<br />

Length of the structure pointed to by lpJobObjectInfo in bytes.<br />

For the two job object information classes that we are interested in, two data structures<br />

are defined. The interesting fields in each structure are:<br />

typedef struct _JOBOBJECT_BASIC_LIMIT_INFORMATION {<br />

LARGE_INTEGER PerProcessUserTimeLimit;<br />

LARGE_INTEGER PerJobUserTimeLimit;<br />

DWORD LimitFlags;<br />

DWORD ActiveProcessLimit;<br />

} JOBOBJECT_BASIC_LIMIT_INFORMATION;<br />

typedef struct _JOBOBJECT_EXTENDED_LIMIT_INFORMATION {<br />

JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation;<br />

SIZE_T ProcessMemoryLimit;<br />

SIZE_T JobMemoryLimit;<br />

} JOBOBJECT_EXTENDED_LIMIT_INFORMATION;<br />

732 | Chapter 13: Other Topics<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!