Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit windows process memory/execution time with job objects - what's wrong with my code?

I'm having an issue trying to make my application shut down after it has reached specified execution time. I created a job, set limit information, assigned current process to it and nothing happens. My code:

SECURITY_ATTRIBUTES sa;
myJob = CreateJobObject(&sa, TEXT("oko"));
LARGE_INTEGER lint; 
lint.LowPart = 1;

JOBOBJECT_BASIC_LIMIT_INFORMATION jbli;
jbli.PerProcessUserTimeLimit = lint;
jbli.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_TIME;
SetInformationJobObject(myJob, JobObjectBasicLimitInformation, &jbli, sizeof(jbli));

AssignProcessToJobObject(myJob, GetCurrentProcess());

I thought it would make my application shut down after 100 nanoseconds, but nothing happens. What is the problem here?


After hours of "fun" with winapi documentation I gave up on this approach and just created another process (which is actually even more convenient for my problem) with flags:

NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB

and such process can be assigned to the job. To succesfully create a job, I followed Roger Rowland's advice and explicitly set parameters of SECURITY_ATTRIBUTES structure. Default security attributes (NULL in CreateJobObject) work similiarly.

like image 320
Shocked Avatar asked Aug 10 '26 07:08

Shocked


1 Answers

One problem is possibly here

LARGE_INTEGER lint; 
lint.LowPart = 1;

You define a LARGE_INTEGER and initialise LowPart but do nothing with HighPart which may contain random data (in a Release build). Maybe try this

LARGE_INTEGER lint; 
lint.HighPart = 0;
lint.LowPart = 1;

Another possibility is here

SECURITY_ATTRIBUTES sa;
myJob = CreateJobObject(&sa, TEXT("oko"));

because you haven't initialised the SECURITY_ATTRIBUTES structure. So do something like this instead

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
myJob = CreateJobObject(&sa, TEXT("oko"));

If either or both of these have no effect, perhaps you can edit your question accordingly and I'll think again. It's quite likely that you'll need a more sophisticated initialisation of the SECURITY_DESCRIPTOR but start with the above and check all error returns.

like image 164
Roger Rowland Avatar answered Aug 12 '26 00:08

Roger Rowland