Description:
box uses user time
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r130:db5312fad511 - - 1 file changed: 26 inserted, 4 deleted
@@ -298,100 +298,122 | |||||
|
298 |
|
298 | ||
|
299 | int execute(char *exname, char *inname, char *outname, double t, int max_mem) |
|
299 | int execute(char *exname, char *inname, char *outname, double t, int max_mem) |
|
300 | { |
|
300 | { |
|
301 | STARTUPINFO si; |
|
301 | STARTUPINFO si; |
|
302 | PROCESS_INFORMATION pi; |
|
302 | PROCESS_INFORMATION pi; |
|
303 | int ifsuccess = EXE_RESULT_OK; |
|
303 | int ifsuccess = EXE_RESULT_OK; |
|
304 |
|
304 | ||
|
305 | ZeroMemory(&si, sizeof(si)); |
|
305 | ZeroMemory(&si, sizeof(si)); |
|
306 | si.cb = sizeof(si); |
|
306 | si.cb = sizeof(si); |
|
307 | ZeroMemory(&pi, sizeof(pi)); |
|
307 | ZeroMemory(&pi, sizeof(pi)); |
|
308 |
|
308 | ||
|
309 | setstartupinfo(&si, inname, outname); |
|
309 | setstartupinfo(&si, inname, outname); |
|
310 |
|
310 | ||
|
311 | if(!CreateProcess( NULL, // No module name (use command line). |
|
311 | if(!CreateProcess( NULL, // No module name (use command line). |
|
312 | TEXT(exname), // Command line. |
|
312 | TEXT(exname), // Command line. |
|
313 | NULL, // Process handle not inheritable. |
|
313 | NULL, // Process handle not inheritable. |
|
314 | NULL, // Thread handle not inheritable. |
|
314 | NULL, // Thread handle not inheritable. |
|
315 | TRUE, // Set handle inheritance to FALSE. |
|
315 | TRUE, // Set handle inheritance to FALSE. |
|
316 | 0, // No creation flags. |
|
316 | 0, // No creation flags. |
|
317 | NULL, // Use parent's environment block. |
|
317 | NULL, // Use parent's environment block. |
|
318 | NULL, // Use parent's starting directory. |
|
318 | NULL, // Use parent's starting directory. |
|
319 | &si, // Pointer to STARTUPINFO structure. |
|
319 | &si, // Pointer to STARTUPINFO structure. |
|
320 | &pi)) // Pointer to PROCESS_INFORMATION structure. |
|
320 | &pi)) // Pointer to PROCESS_INFORMATION structure. |
|
321 | { |
|
321 | { |
|
322 | //printf( "CreateProcess failed (%d).\n", GetLastError() ); |
|
322 | //printf( "CreateProcess failed (%d).\n", GetLastError() ); |
|
323 | fprintf(stderr, "Process creation error.\n"); |
|
323 | fprintf(stderr, "Process creation error.\n"); |
|
324 | report_stat(0,0); |
|
324 | report_stat(0,0); |
|
325 | return EXE_RESULT_ERROR; |
|
325 | return EXE_RESULT_ERROR; |
|
326 | } |
|
326 | } |
|
327 | //fprintf(stderr,"Process ID: %ld\n",pi.dwProcessId); |
|
327 | //fprintf(stderr,"Process ID: %ld\n",pi.dwProcessId); |
|
328 | //fprintf(stderr,"time limit = %d\n",t); |
|
328 | //fprintf(stderr,"time limit = %d\n",t); |
|
329 |
|
329 | ||
|
330 | // checking memory usage |
|
330 | // checking memory usage |
|
331 | // wait 0.1 sec before checking mem usage |
|
331 | // wait 0.1 sec before checking mem usage |
|
332 |
|
332 | ||
|
333 | SetProcessWorkingSetSize(pi.hProcess, |
|
333 | SetProcessWorkingSetSize(pi.hProcess, |
|
334 | 1, |
|
334 | 1, |
|
335 | max_mem); |
|
335 | max_mem); |
|
336 | int actual_memory_usage = 0; |
|
336 | int actual_memory_usage = 0; |
|
337 |
|
337 | ||
|
338 | Sleep(INITIAL_WAIT_FOR_MEM_CHECK); |
|
338 | Sleep(INITIAL_WAIT_FOR_MEM_CHECK); |
|
339 | if(!check_memory_usage(pi.dwProcessId,max_mem,&actual_memory_usage)) { |
|
339 | if(!check_memory_usage(pi.dwProcessId,max_mem,&actual_memory_usage)) { |
|
340 | // using too much memory |
|
340 | // using too much memory |
|
341 | fprintf(stderr,"Memory limit exceeded.\n"); |
|
341 | fprintf(stderr,"Memory limit exceeded.\n"); |
|
342 | //PrintMemoryInfo(pi.dwProcessId); |
|
342 | //PrintMemoryInfo(pi.dwProcessId); |
|
343 | ifsuccess = EXE_RESULT_MEMORY; |
|
343 | ifsuccess = EXE_RESULT_MEMORY; |
|
344 | } |
|
344 | } |
|
345 |
|
345 | ||
|
346 | - if((ifsuccess == EXE_RESULT_MEMORY) || |
|
346 | + //printf("PID: %d\n", pi.dwProcessId); |
|
347 | - (WaitForSingleObject(pi.hProcess, |
|
347 | + |
|
348 | - (int)(t*1000) + 1 |
|
348 | + if(ifsuccess != EXE_RESULT_MEMORY) { |
|
349 | - - INITIAL_WAIT_FOR_MEM_CHECK)==WAIT_TIMEOUT)) { |
|
349 | + int based_time = (int)(t*1000) + 1 - INITIAL_WAIT_FOR_MEM_CHECK; |
|
|
350 | + bool major_timed_out = (WaitForSingleObject(pi.hProcess, | ||
|
|
351 | + based_time)==WAIT_TIMEOUT); | ||
|
|
352 | + if(major_timed_out) { | ||
|
|
353 | + // wait some more for user time. | ||
|
|
354 | + double time_used = get_process_time_usage(pi.hProcess); | ||
|
|
355 | + while(time_used <= t) { | ||
|
|
356 | + int iter_time = 100; | ||
|
|
357 | + if(t - time_used < 200) | ||
|
|
358 | + iter_time = 20; | ||
|
|
359 | + bool iter_timed_out = (WaitForSingleObject(pi.hProcess, | ||
|
|
360 | + iter_time)==WAIT_TIMEOUT); | ||
|
|
361 | + if(!iter_timed_out) | ||
|
|
362 | + break; | ||
|
|
363 | + | ||
|
|
364 | + time_used = get_process_time_usage(pi.hProcess); | ||
|
|
365 | + //printf("%lf\n",time_used); | ||
|
|
366 | + } | ||
|
|
367 | + ifsuccess = EXE_RESULT_TIMEOUT; | ||
|
|
368 | + } | ||
|
|
369 | + } | ||
|
|
370 | + | ||
|
|
371 | + if((ifsuccess == EXE_RESULT_MEMORY) || (ifsuccess == EXE_RESULT_TIMEOUT)) { | ||
|
350 | // Kill process, because (1) it used too much memory, or (2) time limit |
|
372 | // Kill process, because (1) it used too much memory, or (2) time limit |
|
351 | HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId); |
|
373 | HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pi.dwProcessId); |
|
352 |
|
374 | ||
|
353 | if(ifsuccess != EXE_RESULT_MEMORY) |
|
375 | if(ifsuccess != EXE_RESULT_MEMORY) |
|
354 | fprintf(stderr,"Time limit exceeded.\n"); |
|
376 | fprintf(stderr,"Time limit exceeded.\n"); |
|
355 | if(hProcess != NULL) { |
|
377 | if(hProcess != NULL) { |
|
356 | fprintf(stderr,"killing pid: %ld\n",pi.dwProcessId); |
|
378 | fprintf(stderr,"killing pid: %ld\n",pi.dwProcessId); |
|
357 | TerminateProcess(hProcess, 0); |
|
379 | TerminateProcess(hProcess, 0); |
|
358 | wait_dialog(); |
|
380 | wait_dialog(); |
|
359 | } else { |
|
381 | } else { |
|
360 | DWORD dwNtvdmId = get_ntvdm_pid(); |
|
382 | DWORD dwNtvdmId = get_ntvdm_pid(); |
|
361 | fprintf(stderr,"killing (ntvdm) pid: %ld\n",dwNtvdmId); |
|
383 | fprintf(stderr,"killing (ntvdm) pid: %ld\n",dwNtvdmId); |
|
362 | if(dwNtvdmId!=0) { |
|
384 | if(dwNtvdmId!=0) { |
|
363 | hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwNtvdmId); |
|
385 | hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwNtvdmId); |
|
364 | TerminateProcess(hProcess, 0); |
|
386 | TerminateProcess(hProcess, 0); |
|
365 | } else { |
|
387 | } else { |
|
366 | fprintf(stderr,"killing process error\n"); |
|
388 | fprintf(stderr,"killing process error\n"); |
|
367 | } |
|
389 | } |
|
368 |
|
390 | ||
|
369 | if(get_ntvdm_pid()!=0) { |
|
391 | if(get_ntvdm_pid()!=0) { |
|
370 | fprintf(stderr,"killing error, ntvdm.exe still remains;"); |
|
392 | fprintf(stderr,"killing error, ntvdm.exe still remains;"); |
|
371 | fprintf(stderr,"please MANUALLY kill it."); |
|
393 | fprintf(stderr,"please MANUALLY kill it."); |
|
372 | fflush(stderr); |
|
394 | fflush(stderr); |
|
373 | do { |
|
395 | do { |
|
374 | Sleep(1000); |
|
396 | Sleep(1000); |
|
375 | } while(get_ntvdm_pid()!=0); |
|
397 | } while(get_ntvdm_pid()!=0); |
|
376 | fprintf(stderr,"... done\n"); |
|
398 | fprintf(stderr,"... done\n"); |
|
377 | wait_dialog(); |
|
399 | wait_dialog(); |
|
378 | } |
|
400 | } |
|
379 | } |
|
401 | } |
|
380 | if(ifsuccess != EXE_RESULT_MEMORY) |
|
402 | if(ifsuccess != EXE_RESULT_MEMORY) |
|
381 | ifsuccess = EXE_RESULT_TIMEOUT; |
|
403 | ifsuccess = EXE_RESULT_TIMEOUT; |
|
382 | } |
|
404 | } |
|
383 |
|
405 | ||
|
384 | // check memory after terminated |
|
406 | // check memory after terminated |
|
385 | if((ifsuccess==EXE_RESULT_OK) && |
|
407 | if((ifsuccess==EXE_RESULT_OK) && |
|
386 | (!check_memory_usage(pi.dwProcessId,max_mem, &actual_memory_usage))) { |
|
408 | (!check_memory_usage(pi.dwProcessId,max_mem, &actual_memory_usage))) { |
|
387 | // using too much memory |
|
409 | // using too much memory |
|
388 | ifsuccess = EXE_RESULT_MEMORY; |
|
410 | ifsuccess = EXE_RESULT_MEMORY; |
|
389 | } |
|
411 | } |
|
390 |
|
412 | ||
|
391 | // check return code |
|
413 | // check return code |
|
392 | if(ifsuccess==EXE_RESULT_OK) { |
|
414 | if(ifsuccess==EXE_RESULT_OK) { |
|
393 | DWORD exitcode; |
|
415 | DWORD exitcode; |
|
394 | GetExitCodeProcess(pi.hProcess, &exitcode); |
|
416 | GetExitCodeProcess(pi.hProcess, &exitcode); |
|
395 | if(exitcode!=0) { |
|
417 | if(exitcode!=0) { |
|
396 | fprintf(stderr,"Exit status %d.\n", (int)exitcode); |
|
418 | fprintf(stderr,"Exit status %d.\n", (int)exitcode); |
|
397 | ifsuccess = EXE_RESULT_ERROR; |
|
419 | ifsuccess = EXE_RESULT_ERROR; |
You need to be logged in to leave comments.
Login now