Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过精确性更改所有过程的精确性_C_Linux_Debian_Nice - Fatal编程技术网

C 通过精确性更改所有过程的精确性

C 通过精确性更改所有过程的精确性,c,linux,debian,nice,C,Linux,Debian,Nice,我使用的是Debian,有没有一种方法可以根据当前进程的良好性来改变所有运行进程的良好性?例如,将所有当前运行的进程的准确度从-20或-19更改为-10。Renice可以更改进程,并为某些用户更改进程。但据我所知,基于目前的良好状况,它无法做到这一点 我正在尝试运行一个精确到-20的程序,试图绕过一些似乎是半定期出现的时间峰值。这些可能是由某些具有相同优先级的进程占用资源造成的。我希望通过一些精细的摆弄来检查这一点。要使用精细度19到10重新启动所有流程: ps-eo-nice,pid | se

我使用的是Debian,有没有一种方法可以根据当前进程的良好性来改变所有运行进程的良好性?例如,将所有当前运行的进程的准确度从-20或-19更改为-10。Renice可以更改进程,并为某些用户更改进程。但据我所知,基于目前的良好状况,它无法做到这一点


我正在尝试运行一个精确到-20的程序,试图绕过一些似乎是半定期出现的时间峰值。这些可能是由某些具有相同优先级的进程占用资源造成的。我希望通过一些精细的摆弄来检查这一点。

要使用精细度19到10重新启动所有流程:

ps-eo-nice,pid | sed-e's/^\+19/;tx;d、 :x'| xargs sudo renice 10


找出这一点的原因,或者将其扩展为同时使用多个优先级,留给读者作为练习。

在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);
    }
}
#包括
#包括
#包括
#包括
#包括
静态字符*prstatname(字符*buf,字符**endptr)
{
/*解析进程名*/
char*ptr=buf;
而(*ptr&&*ptr!='(')++ptr;
++ptr;
如果(!ptr)返回0;
char*name=ptr;
while(*ptr)
{
如果(*ptr==”)&&&*(ptr+1)&&*(ptr+2)&&*(ptr+3)
&&*(ptr+1)=''&*(ptr+3)='')
{
*ptr=0;
*endptr=ptr+1;
返回名称;
}
++ptr;
}
返回0;
}
内部主(空)
{
DIR*proc=opendir(“/proc”);
如果(!proc)返回1;
结构导向;
while((ent=readdir(proc)))
{
/*检查文件名是否都是数字,然后它是进程id*/
char*endptr;
intpid=strtol(ent->d_name,&endptr,10);
如果(*endptr)继续;
/*合并到“/proc/{pid}/stat”以获取有关进程的信息*/
char statname[64]={0,};
strcat(statname,“/proc/”);
strncat(statname,ent->d_name,52);
strcat(statname,“/stat”);
文件*pstat=fopen(statname,“r”);
如果(!pstat)继续;
/*尝试读取进程信息*/
char-buf[1024];
如果(!fgets(buf,1024,pstat))
{
fclose(pstat);
继续;
}
fclose(pstat);
char*name=prstatname(buf和endptr);
如果(!name)继续;
/*nice值位于进程名称后的第17个字段中*/
int i;
char*tok=strtok(endptr,“”);
对于(i=0;tok&&i<16;++i)tok=strtok(0,”;
如果(!tok | i<16)继续;
int nice=strtol(tok和endptr,10);
如果(*endptr)继续;
printf(“[%d]%s--nice:%d\n”,pid,名称,nice);
}
}

如果您了解此程序,您可以轻松地修改它以执行所需操作。

您使用什么语言编写此程序?到目前为止,你尝试了什么?你可以写一个<代码> BASH < /C++ >脚本,运行<代码> PS -O PID、NI<代码>,并且对于每个进程,其中<代码> NI<代码>列为代码> -20 < /代码>它使它重命名。程序是用C++编写的。我将尝试使用system()调用。我将更新这里的内容。您应该能够使用
/proc
在C程序中获取信息。这与编程无关,但与系统管理有关。