3,881
社区成员
发帖
与我相关
我的任务
分享
#define _WIN32_WINNT 0x0500
#include "ReturnInts.h"
#include <Windows.h>
#include <string.h>
#include <Psapi.h>
#include <Winbase.h>
#define KBITS 1024
#define MSEC 10000
void setLimits(HANDLE hJob,int s,int k)//set limits on job
{
//JOBOBJECT_BASIC_LIMIT_INFORMATION basic_limit = {0};
JOBOBJECT_EXTENDED_LIMIT_INFORMATION extended_limit = {0};
//basic_limit.PerProcessUserTimeLimit.QuadPart = 5*sec;
extended_limit.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart = s*MSEC;//set time limit
extended_limit.ProcessMemoryLimit = k*KBITS;//set memory linmit
extended_limit.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_TIME | JOB_OBJECT_LIMIT_PROCESS_MEMORY ;
if(!SetInformationJobObject(
hJob, // handle to job
JobObjectExtendedLimitInformation, // information class
&extended_limit, // limit information
sizeof(extended_limit) // size of limit information
))
{
printf("SetInformationJobObject fail!\n");
}
}
void queryLimits(HANDLE hJob)//query limist set on job
{
JOBOBJECT_EXTENDED_LIMIT_INFORMATION extended_limit = {0};
DWORD cb = 0;
QueryInformationJobObject(
hJob, // handle to job
JobObjectExtendedLimitInformation, // information class
&extended_limit, // limit information
sizeof(extended_limit), // limit information size
&cb // data written
);
if(cb == 0)
printf("no date!\n");
else{
printf("\ntime limit = %d\n",extended_limit.BasicLimitInformation.PerProcessUserTimeLimit.QuadPart);
printf("memory limit = %d\n",extended_limit.ProcessMemoryLimit);
}
}
JNIEXPORT jintArray JNICALL Java_ReturnInts_retInts
(JNIEnv *env, jclass cl, jstring str)
{
jint ntvRet[3] = {1,2,3};
jintArray ret = env->NewIntArray(3);
env->SetIntArrayRegion(ret, 0, 3, ntvRet);
return ret;
}
JNIEXPORT jint JNICALL Java_ReturnInts_runApp
(JNIEnv *env, jclass cl, jstring file)
{
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
char szCmdLine[128]={0};
const char* CmdLine = env->GetStringUTFChars(file,0);
strcpy(szCmdLine,CmdLine);
env->ReleaseStringUTFChars(file,CmdLine);
if( !CreateProcess( NULL, // No module name (use command line).
szCmdLine,
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_SUSPENDED, //creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed." );
return 0;
}
printf("run the process...\n");
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
WaitForSingleObject( pi.hProcess, INFINITE );
PROCESS_MEMORY_COUNTERS pmc = {0};
GetProcessMemoryInfo(pi.hProcess,&pmc,sizeof(pmc));
printf("\nPeakWorkingSetSize=%dk\n",pmc.PeakWorkingSetSize/1024);
FILETIME CreateTime,ExitTime,KernelTime,UserTime;
GetProcessTimes(pi.hProcess,&CreateTime,&ExitTime,&KernelTime,&UserTime);
printf("UserTimeH=%ums,UserTimeL=%ums\n",UserTime.dwHighDateTime,UserTime.dwLowDateTime);
printf("KernelTime=%ums\n",KernelTime.dwLowDateTime);
printf("diffTime=%u\n",ExitTime.dwLowDateTime-CreateTime.dwLowDateTime);
DWORD cb;
GetExitCodeProcess(pi.hProcess,&cb);
CloseHandle(pi.hProcess);
return cb;
}
JNIEXPORT jintArray JNICALL Java_ReturnInts_RunAppInJob
(JNIEnv *env, jclass cl, jint tLimit, jint mLimit, jstring file)
{
jint ntvRet[3] = {-1,-2,-3};
jintArray ret = env->NewIntArray(3);
env->SetIntArrayRegion(ret, 0, 3, ntvRet);
HANDLE hjob = CreateJobObject(NULL,NULL);//创建JOB对象
setLimits(hjob,tLimit,mLimit);//设置时间和内存的限制
queryLimits(hjob);//调试用的,看看是否添加限制成功
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
char szCmdLine[128]={0};
const char* CmdLine = env->GetStringUTFChars(file,0);
strcpy(szCmdLine,CmdLine);
env->ReleaseStringUTFChars(file,CmdLine);
//创建进程
if( !CreateProcess( NULL, // No module name (use command line).
szCmdLine,
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_SUSPENDED, //creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
// printf( "CreateProcess failed." );
return ret;
}
// printf("run the process...\n");
ResumeThread(pi.hThread);
CloseHandle(pi.hThread);
WaitForSingleObject( pi.hProcess, INFINITE );
//下面部分是当进程结束后查询进程消耗的资源 PROCESS_MEMORY_COUNTERS pmc = {0};
GetProcessMemoryInfo(pi.hProcess,&pmc,sizeof(pmc));
printf("\nPeakWorkingSetSize=%dk\n",pmc.PeakWorkingSetSize/1024);
ntvRet[1] = pmc.PeakWorkingSetSize/1024;
FILETIME CreateTime,ExitTime,KernelTime,UserTime;
GetProcessTimes(pi.hProcess,&CreateTime,&ExitTime,&KernelTime,&UserTime);
printf("UserTimeH=%ums,UserTimeL=%ums\n",UserTime.dwHighDateTime,UserTime.dwLowDateTime);
printf("KernelTime=%ums\n",KernelTime.dwLowDateTime);
printf("diffTime=%u\n",ExitTime.dwLowDateTime-CreateTime.dwLowDateTime);
ntvRet[2] = ExitTime.dwLowDateTime-CreateTime.dwLowDateTime;
DWORD cb;
GetExitCodeProcess(pi.hProcess,&cb);
ntvRet[0] = cb;
env->SetIntArrayRegion(ret, 0, 3, ntvRet);
CloseHandle(pi.hProcess);
CloseHandle(hjob);
return ret;
}