|
Guest
|
I am using the code below to set the job memory limit to 400MB, but it
seems not working. I also tried to set process memory limit. Not work
either. Did I miss anything here? Thanks very much!
Leo
void StartRestrictedProcess(LPSTR lpCmdLine)
{
// If we are already associated with a job, no way to switch to
another
BOOL bInJob = FALSE;
IsProcessInJob(GetCurrentProcess(), NULL, &bInJob);
if (bInJob)
return;
// Create a job kernel object
HANDLE hjob = CreateJobObject(NULL, _T("My_Job"));
// Set memory limit to 400MB
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
jeli.BasicLimitInformation.LimitFlags =
JOB_OBJECT_LIMIT_JOB_MEMORY;
jeli.JobMemoryLimit = 400;
SetInformationJobObject(hjob, JobObjectExtendedLimitInformation,
&jeli, sizeof(jeli));
// Create Process
STARTUPINFO si;
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi) );
BOOL bResult = CreateProcess(L"C:\\Test\\MyApp.exe", 0, NULL, NULL,
FALSE,
CREATE_SUSPENDED, NULL, NULL, &si, &pi);
if (bResult)
{
// Place the process in the job.
AssignProcessToJobObject(hjob, pi.hProcess);
// Now we can allow the child process' thread to execute code.
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
// Wait for the process to terminate or for all the job's
allocated memory to be used.
HANDLE h[2];
h[0] = pi.hProcess;
h[1] = hjob;
DWORD dw = WaitForMultipleObjects(2, h, FALSE, INFINITE);
switch (dw - WAIT_OBJECT_0)
{
case 0:
// The process has terminated...
break;
case 1:
// All of the job's allocated memory was used...
break;
}
}
// Clean up properly.
CloseHandle(pi.hProcess);
CloseHandle(hjob);
} |
|
|