改变由正派的所有进程的niceness [英] Change niceness of all processes by niceness

查看:106
本文介绍了改变由正派的所有进程的niceness的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,有没有办法改变所有正在运行的进程根据其当前的美好的事物美好的事物?例如更改为-20或-19到-10正派所有正在运行的进程。 renice只能可以改变的方法,和对某些用户进程。但据我可以告诉它不能做基于目前的文雅。

I am using Debian, is there a way to change the niceness of all running process based on their current niceness? For instance change all currently running processes that have a niceness of -20 or -19 to -10. Renice can change a process, and processes for certain users. But as far as I can tell it can't do it based on current niceness.

我试图运行一个程序-20美好的事物尽量和周围,似乎半经常发生一些时间尖峰得到。这些可能是由具有相同优先级取资源某些过程引起的。我希望与一些美好的事物摆弄进行检查。

I am trying to run a program with -20 niceness to try and get around some timing spikes that seem to occur semi-regularly. These might be caused by certain processes with the same priority taking resources. I was hoping to check this with some niceness fiddling.

推荐答案

有得开始与C:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

static char *prstatname(char *buf, char **endptr)
{
    /* parse process name */
    char *ptr = buf;
    while (*ptr && *ptr != '(') ++ptr;
    ++ptr;
    if (!ptr) return 0;

    char *name = ptr;
    while (*ptr)
    {
        if (*ptr == ')' && *(ptr+1) && *(ptr+2) && *(ptr+3)
                && *(ptr+1) == ' ' && *(ptr+3) == ' ')
        {
            *ptr = 0;
            *endptr = ptr + 1;
            return name;
        }
        ++ptr;
    }
    return 0;
}

int main(void)
{
    DIR *proc = opendir("/proc");
    if (!proc) return 1;

    struct dirent *ent;

    while ((ent = readdir(proc)))
    {
        /* check whether filename is all numeric, then it's a process id */
        char *endptr;
        int pid = strtol(ent->d_name, &endptr, 10);
        if (*endptr) continue;

        /* combine to '/proc/{pid}/stat' to get information about process */
        char statname[64] = {0,};       
        strcat(statname, "/proc/");
        strncat(statname, ent->d_name, 52);
        strcat(statname, "/stat");

        FILE *pstat = fopen(statname, "r");
        if (!pstat) continue;

        /* try to read process info */
        char buf[1024];
        if (!fgets(buf, 1024, pstat))
        {
            fclose(pstat);
            continue;
        }
        fclose(pstat);

        char *name = prstatname(buf, &endptr);
        if (!name) continue;

        /* nice value is in the 17th field after process name */
        int i;
        char *tok = strtok(endptr, " ");
        for (i = 0; tok && i < 16; ++i) tok = strtok(0, " ");
        if (!tok || i < 16) continue;

        int nice = strtol(tok, &endptr, 10);
        if (*endptr) continue;

        printf("[%d] %s -- nice: %d\n", pid, name, nice);
    }
}

如果你了解这个程序,你可以轻松地修改它做你想要的。

If you understand this program, you can easily modify it to do what you wanted.

这篇关于改变由正派的所有进程的niceness的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆