C++ 如何在C++中设置进程优先级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5216347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:41:55  来源:igfitidea点击:

How to set the process priority in C++

c++windowsperformanceprocessprocess-elevation

提问by john

I am working on a program to sort data, and I need to to set the process to priority 31, which I believe is the highest process priority in Windows. I have done some research, but can't figure out how to do it in C++.

我正在开发一个程序来对数据进行排序,我需要将进程设置为优先级 31,我认为这是 Windows 中最高的进程优先级。我做了一些研究,但不知道如何在 C++ 中做到这一点。

回答by lunixbochs

The Windows API call SetPriorityClassallows you to change your process priority, see the example in the MSDN documentation, and use REALTIME_PRIORITY_CLASS to set the highest priority:

Windows API 调用SetPriorityClass允许您更改进程优先级,请参阅 MSDN 文档中的示例,并使用 REALTIME_PRIORITY_CLASS 设置最高优先级:

SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)

Caution: if you are asking for true realtime priority, you are going to get it. This is a nuke. The OS will mercilessly prioritize a realtime priority thread, well above even OS-level input processing, disk-cache flushing, and other high-priority time-critical tasks. You can easily lock up your entire system if your realtime thread(s) drain your CPU capacity. Be cautious when doing this, and unless absolutely necessary, consider using high-priority instead. More information

注意:如果您要求真正的实时优先级,您会得到它。这是核弹。操作系统会毫不留情地对实时优先级线程进行优先级排序,甚至远高于操作系统级输入处理、磁盘缓存刷新和其他高优先级时间关键任务。如果您的实时线程耗尽了您的 CPU 容量,您可以轻松锁定整个系统。这样做时要小心,除非绝对必要,否则请考虑使用高优先级。更多信息

回答by Michael Haephrati

The following function will do the job:

以下功能将完成这项工作:

void SetProcessPriority(LPWSTR ProcessName, int Priority)
{
    PROCESSENTRY32 proc32;
    HANDLE hSnap;
    if (hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
    if (hSnap == INVALID_HANDLE_VALUE)
    {

    }
    else
    {
        proc32.dwSize = sizeof(PROCESSENTRY32);
        while ((Process32Next(hSnap, &proc32)) == TRUE)
        {
            if (_wcsicmp(proc32.szExeFile, ProcessName) == 0)
            {
                HANDLE h = OpenProcess(PROCESS_SET_INFORMATION, TRUE, proc32.th32ProcessID);
                SetPriorityClass(h, Priority);
                CloseHandle(h);
            }
        }
        CloseHandle(hSnap);
    }
}

For example, to set the priority of the current process to below normal, use:

例如,要将当前进程的优先级设置为低于正常,请使用:

SetProcessPriority(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS)

回答by dyasta

After (or before) SetPriorityClass, you must set the individual thread priority to achieve the maximum possible. Additionally, another security token is required for realtime priority class, so be sure to grab it (if accessible). SetThreadPriority is the secondary API after SetPriorityClass.

在 SetPriorityClass 之后(或之前),您必须设置单个线程优先级以达到最大可能。此外,实时优先级需要另一个安全令牌,所以一定要抓住它(如果可以访问)。SetThreadPriority 是继 SetPriorityClass 之后的辅助 API。