能力值:
( LV2,RANK:10 )
2 楼
占个沙发同求...
能力值:
( LV2,RANK:10 )
3 楼
占位,学习
能力值:
(RANK:50 )
4 楼
CPU占用率这个似乎很难直接控制。
思路:
1.降低优先级,减小资源耗费。
2.指定其可以使用的CPU,比如指定只允许使用CPU7,这样在四核8线程的i7上总占用率不会超过12.5%
能力值:
( LV4,RANK:50 )
5 楼
直接控制的结果就是对方的CPU下去了 你上去了
建议控制优先级,目前大多数资源控制软件都这么干的
能力值:
( LV2,RANK:10 )
6 楼
奥,谢谢大家的回答,我大概的总结一下
1.通过设置处理器关系,让你指定的进程只在一个核心(或者线程)上运行--->SetProcessAffinityMask来实现。
2.设置进程的优先级,把它调到最低---->SetPriorityClass
3.通过DeadLoop中配合Sleep、ResumeThread和SuspendThread,对进程的执行时间进行限制
4.CreateJobObject创建作业对象
AssignProcessToJobObject 将其绑定到作业中
SetInformationJobObject设置作业信息
JOBOBJECTINFOCLASS传入JOB_OBJECT_MSG_END_OF_JOB_TIME
作业分配的CPU时间到期时,就投递通知。但其中的进程不会自动终止。我们可以允许进程继续运行,可以设置一个新的时间限额,还可以自己调用TerminateJobObject
能力值:
( LV2,RANK:10 )
7 楼
谁在帮我把第四种方案实现一下啊,我的一直抱SetInformationJobObject2 Fail!,err code 为24,代码相当不完整,帮我搞搞,最好能实现对UserTime的Limit #include "stdafx.h"
#include "windows.h"
#include "stdlib.h"
#include <process.h>
HANDLE g_hIOCP;
HANDLE g_hThreadIOCP;
HANDLE hProcess;
HANDLE hJob;
///////////////////////////// chBEGINTHREADEX Macro /////////////////////////// // This macro function calls the C runtime's _beginthreadex function.
// The C runtime library doesn't want to have any reliance on Windows' data
// types such as HANDLE. This means that a Windows programmer needs to cast
// values when using _beginthreadex. Since this is terribly inconvenient,
// I created this macro to perform the casting.
typedef unsigned (__stdcall *PTHREAD_START) (void *);
#define chBEGINTHREADEX(psa, cbStackSize, pfnStartAddr, \
pvParam, dwCreateFlags, pdwThreadId) \
((HANDLE)_beginthreadex( \
(void *) (psa), \
(unsigned) (cbStackSize), \
(PTHREAD_START) (pfnStartAddr), \
(void *) (pvParam), \
(unsigned) (dwCreateFlags), \
(unsigned *) (pdwThreadId))) DWORD WINAPI JobNotify(PVOID) {
while(true)
{
DWORD lpNumberOfBytes;
ULONG_PTR lpCompletionKey;
LPOVERLAPPED lpOverlapped;
GetQueuedCompletionStatus(g_hIOCP,&lpNumberOfBytes,&lpCompletionKey,&lpOverlapped,INFINITE);
printf("i am running!\n");
switch(lpNumberOfBytes)
{
case JOB_OBJECT_MSG_END_OF_PROCESS_TIME:
{
MessageBox(0,"Time Up","JJ",0);
TerminateJobObject(hJob,0);
}
}
} //return 0;
} int main(int argc, char* argv[])
{
//LARGE_INTEGER a;
int Pid=0;
const int nNanosecondsPerSecond = 1000000000;
const int nMillisecondsPerSecond = 1000;
const int nNanosecondsPerMillisecond =
nNanosecondsPerSecond / nMillisecondsPerSecond;
JOBOBJECT_END_OF_JOB_TIME_INFORMATION Jeojti={0};
JOBOBJECT_ASSOCIATE_COMPLETION_PORT Jacp={0};
JOBOBJECT_BASIC_LIMIT_INFORMATION Jbli={0};
Jeojti.EndOfJobTimeAction=JOB_OBJECT_POST_AT_END_OF_JOB;
Jbli.LimitFlags|=JOB_OBJECT_LIMIT_PROCESS_TIME;
Jbli.PerProcessUserTimeLimit.QuadPart=10000;
if(argc!=2)
{
printf("You Have to Input one Argument!\n");
printf("Usage:xxx.exe PID\n");
return 0;
}
Pid=atoi(argv[1]); //将传入的参数转换成整形
// Create the completion port that receives job notifications
g_hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); //Jacp.CompletionKey=(PVOID)COMP_KEY;
Jacp.CompletionPort=g_hIOCP;
// Create a thread that waits on the completion port
hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Pid);
if(!hProcess)
{
printf("OpenProcess Fail!\n");
return 0;
}
if(!(hJob=CreateJobObject(NULL,"MySpecialJob"))) //在使用这个函数的时候,一定要在Stdafx.h中添加#define _WIN32_WINNT 0x0500,否则编译报错,未定义的字符
{
printf("CreateJobObject Fail!\n");
return 0;
}
if(!AssignProcessToJobObject(hJob,hProcess))//进程和JOB相关联
{
printf("AssignProcessToJobObject Fail!\n");
return 0;
}
if(!SetInformationJobObject(hJob,JobObjectEndOfJobTimeInformation,&Jeojti,sizeof(Jeojti)))
{
printf("SetInformationJobObject0 Fail!\n");
CloseHandle(hJob);
CloseHandle(hProcess);
return 0;
}
if(!SetInformationJobObject(hJob,JobObjectAssociateCompletionPortInformation,&Jacp,sizeof(Jacp)))
{
printf("SetInformationJobObject1 Fail!\n");
CloseHandle(hJob);
CloseHandle(hProcess);
return 0;
}
if(!SetInformationJobObject(hJob,JobObjectBasicLimitInformation,&Jbli,sizeof(Jbli)))
{
printf("SetInformationJobObject2 Fail!\n");
printf("Err code is %d\n",GetLastError());
CloseHandle(hJob);
CloseHandle(hProcess);
return 0;
}
g_hThreadIOCP = chBEGINTHREADEX(NULL, 0, JobNotify, NULL, 0, NULL);
WaitForSingleObject(g_hThreadIOCP, INFINITE);
CloseHandle(hJob);
CloseHandle(hProcess);
return 0;
}
能力值:
( LV2,RANK:10 )
8 楼
真正的程序员才考虑这些,我这样的三流从来没想过!