首页 > 解决方案 > 我想让我的 c 程序调度具有特定 CPU 使用率的其他程序

问题描述

我的程序现在只执行其他 c 程序。但我希望这个进程占用 CPU 的(输入值)%。到目前为止,新进程(分叉进程)只占用 CPU 的 0%..有时 0.1%.I希望它采用输入的% ex..25%

#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>


  printf("u_interval=%d\n", u_interval);
  for (size_t i = 0; i < schedulees_count; ++i) {
    schedulee_t *schedulee = schedulees[i];
    printf("schedulee %zu: %d%%\n  argv: [", i, schedulee->proportion);
    for (size_t j = 0; schedulee->argv[j] != NULL; ++j) {
      printf("\"%s\", ", schedulee->argv[j]);
    }
    printf("\b\b]\n");
    int pid = fork(); //The parent(>0) and child(0) is formed
    if (pid == 0) {
        printf("Hello from Child!\n"); 
        execv(schedulee->argv[0], schedulee->argv);
      }

    // parent process because return value non-zero. 
    else{
      wait(NULL);
        printf("Hello from Parent!\n"); 
              schedulee->pid = pid;
              kill(pid,SIGSTOP);

      }    
  }





这是程序的一部分,它通过 execv 命令执行其他程序(任务)。但是新进程只占用 0.0% 的 CPU。例如,我希望它有 25% 的 CPU 使用率。

标签: coperating-systemcpu-usage

解决方案


推荐阅读