zoukankan      html  css  js  c++  java
  • 《Windows核心编程》第七章——进程优先级实验

    其实就是做个实验,看看SetPriorityClass是否真的会生效。

    设计思路:主线程一直在进行某种操作(这里是写文件操作),以保证有一定的CPU占用率;生成的子线程来接收你的命令,决定将进程改变为什么级别。

    代码逻辑如下:

    #include <iostream>
    #include <windows.h>
    #include <tchar.h>
    #include <process.h>
    #include <fstream>
    
    using namespace std;
    unsigned int WINAPI ThreadFunc(PVOID pvParam){
        HANDLE hPThread = (HANDLE)pvParam;
        printf("Enter thread
    ");
        while (true){
            char cPriority = getchar();
            //SuspendThread(hPThread);
            if (cPriority == 'a')
            {
                if (!SetPriorityClass(GetCurrentProcess(), IDLE_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("IDLE_PRIORITY_CLASS
    ");
            }
            if (cPriority == 'b')
            {
                if (!SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("BELOW_NORMAL_PRIORITY_CLASS
    ");
            }
            if (cPriority == 'c')
            {
                if (!SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("NORMAL_PRIORITY_CLASS
    ");
            }
            if (cPriority == 'd')
            {
                if (!SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("ABOVE_NORMAL_PRIORITY_CLASS
    ");
            }
            if (cPriority == 'e')
            {
                if (!SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("HIGH_PRIORITY_CLASS
    ");
            }
            if (cPriority == 'f')
            {
                if (!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS))
                {
                    printf("SetPriorityClass failed:%d
    ", GetLastError());
                    return FALSE;
                }
                printf("REALTIME_PRIORITY_CLASS
    ");
            }
            /*if (cPriority == 's')
            {
                Sleep(30000);
                printf("Sleeping..
    ");
            }*/
            //ResumeThread(hPThread);
        }
        CloseHandle(hPThread);
        return TRUE;
    }
    
    BOOL main(){
        HANDLE hPThread;
        if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
        &hPThread, THREAD_SUSPEND_RESUME, FALSE, DUPLICATE_SAME_ACCESS))
        {
        printf("Dupilicate failed:%d", GetLastError());
        return FALSE;
        }
    
        _beginthreadex(NULL, 0, ThreadFunc, NULL, 0, NULL);
    
        int i = 0;
        printf("Enter while
    ");
        while (TRUE){
            i++;
            ofstream outfile;
            outfile.open("E:\Coding\test\test2.txt");
            outfile << "Run service success..
    " << endl;
            outfile.close();
        }
        return TRUE;
    }

    当启动这个线程和它的副本的时候,二者CPU占用率差不多:

    现在调低188.exe的优先级,调到IDLE优先级后,其CPU占用率立即下降:

    如果改成实时的优先级,其CUP占用率就会提升:

    确实会有变化,但是并不像书中说的那么明显。

  • 相关阅读:
    MyBatis热部署
    FreeMarker速查手册
    SpringBoot从Eclipse添加的Tomcat容器中启动
    Delphi中的GetEnumName和GetEnumValue的使用方法
    [数据库连接字符串] Access 连接字符串
    [数据库连接字符串] Access 连接字符串
    Ms SQLServer中的Union和Union All的使用方法和区别
    Ms SQLServer中的Union和Union All的使用方法和区别
    RasAPI函数实现PPPOE拨号
    RasAPI函数实现PPPOE拨号
  • 原文地址:https://www.cnblogs.com/predator-wang/p/8830234.html
Copyright © 2011-2022 走看看