我正在使用 Debian,有没有办法根据它们当前的友好度来改变所有正在运行的进程的友好度?例如,将所有当前运行的进程(niceness 为 -20 或 -19)更改为 -10。Renice 可以更改进程,并为某些用户更改进程。但据我所知,基于当前的友好度,它不能做到这一点。
我正在尝试以 -20 的精度运行一个程序,以尝试绕过一些似乎是半定期发生的时间尖峰。这些可能是由具有相同优先级的某些进程占用资源引起的。我希望通过一些漂亮的摆弄来检查这一点。
使用 niceness 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);
}
}
如果你理解这个程序,你可以很容易地修改它来做你想做的事。