linux c control cpu occupancy rate

Previously mentioned in <The Beauty of Programming> that controlling the usage rate of the cpu enables to draw a sine line on the task manager.
Now the following provides a realization on the Linux platform to control the cpu frequency at a certain value
cpu_load.c

#include <iostream>
#include <pthread.h>
#include <time.h>
#include <math.h>
#include <unistd.h>

using namespace std;

typedef long long int int64;
const int NUM_THREADS = 1; //CPU core nums
int INTERVAL = 100;
int cpuinfo = 15; //CPU utilization rate

// time unit is "ms"
int64 GetTickCount()
{
    timespec now;
    int64 sec, nsec;

    clock_gettime(CLOCK_MONOTONIC, &now);
    sec = now.tv_sec;
    nsec = now.tv_nsec;

    return sec * 1000 + nsec / 1000000;
}

void* CPUCost(void *args)
{
    int busyTime = INTERVAL * cpuinfo / 100;
    int idleTime = INTERVAL - busyTime;
    int64 startTime = 0;

    std::cout << "XXXX CPUCost" << std::endl;
    std::cout << "XXXX cpuinfo = " << cpuinfo << std::endl;

    /*
     * within INTERVAL ms, INTERVAL = busyTime + idleTime,
     * spend busyTime ms to let cpu busy,
     * spend idleTime ms top let cpu idle
     */
    while (true) {
        startTime = GetTickCount();
        while((GetTickCount() - startTime) <= busyTime);
        usleep(idleTime * 1000);
    }
}

int main(int argc, char **argv)
{
    pthread_t t[NUM_THREADS];
    int ret;

    std::cout << "please input cpu utilization rate" << std::endl;
    std::cin >> cpuinfo;
    for(int i = 0; i < NUM_THREADS; i++) {
        ret = pthread_create(&t[i], NULL, CPUCost, NULL);
        if(ret)
            std::cout << "XXXX create err" << std::endl;
    }

    pthread_exit(NULL);
    return 0;
}

Compile:  g++ cpu_load.c -lpthread -lrt -o cpu_load
1. Set the CPU occupancy rate to 10%
Write picture description here
Write picture description here

2. Set the CPU occupancy rate to 60%
Write picture description here
Write picture description here

3. Set the CPU occupancy rate to 100%. The
Write picture description here
Write picture description here
program needs to run on an idle machine to get the set CPU utilization rate more accurately.

 

 


 

 

Guess you like

Origin blog.csdn.net/whatday/article/details/114702884