#include "stdio.h"
#include "windows.h"
#define MEM_LIMIT 4096*1000
int main(int argc, char* argv[])
{
HANDLE hjob;
char *job_name="J1",*pro_name="notepad";
int err;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION joeli;
JOBOBJECT_BASIC_LIMIT_INFORMATION jbii={0};
STARTUPINFOA si ={sizeof(si)};
PROCESS_INFORMATION pi;
if((hjob=CreateJobObjectA(NULL,job_name))==NULL)
{
printf("Cannot create job!\n");
return 1;
}
joeli.BasicLimitInformation.LimitFlags=JOB_OBJECT_LIMIT_PROCESS_MEMORY;
joeli.ProcessMemoryLimit=MEM_LIMIT;
if(SetInformationJobObject(hjob,JobObjectExtendedLimitInformation,&joeli,sizeof(joeli))==0)
{
err=GetLastError();
printf("Fail to set job attributes, Error Code:%d\n",err);
return 1;
}
if(CreateProcessA(NULL,pro_name,/*JOB_OBJECT_ALL_ACCESS*/NULL,/*JOB_OBJECT_ALL_ACCESS*/NULL,FALSE,CREATE_SUSPENDED,NULL,NULL,&si,&pi)==0)
{
err=GetLastError();
printf("Cannot create Process %s, Error Code:%d\n",pro_name,err);
return 1;
}
if(!AssignProcessToJobObject(hjob,pi.hProcess))
{
err=GetLastError();
printf("Fail to assign process to job Eror Code:%d\n",err);
}
ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess,INFINITE);
return 0;
}