如何改变低进程优先级?

如何改变低进程优先级?

我刚刚在配备 IvyBridge i7 的新笔记本电脑上设置了 Ubuntu 12.04 64 位、Cinnamon 桌面和 3.5.0-030500 内核。

我决定通过运行一个我经常用于相机校准的单线程 CPU 密集型程序来测试它的性能。不幸的是,它最终运行得比我预想的要慢得多。经过一番调查,我发现程序优先级会自动从正常变为低,这会使程序运行得更慢。

我还注意到,所有用户程序(如 Skype 和 Firefox)都设置为低优先级。我尝试使用renice命令,它会暂时起作用,直到内核调度程序(我猜)将优先级重置为低。

这是一个正常现象吗?我该如何克服执行缓慢的问题?

PS 我也尝试了 3.2 内核,但问题仍然存在。

答案1

您可以在文件中设置特定用户或组的默认 nice 值/etc/security/limits.conf

/etc/security/limits.conf

它使用以下语法:[username] [hard|soft] priority [nice value]

您尝试将您的用户放入此文件中并设置为您希望的优先级。

答案2

这是在 Ubuntu 中永久提高 Firefox 优先级的一种方法。您也可以将其用于 Skype 或其他进程。

步骤 1:使用以下 C 代码(来源:http://and.sourceforge.net/highpriostart.c

如果将文件命名为increase_ff_priority.c,那么

编译安装如下:

# gcc increase_ff_priority.c -o increase_ff_priority
# sudo cp increase_ff_priority /usr/local/bin
# sudo chmod +s /usr/local/bin/increase_ff_priority

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>


/*********************************************************************

   High priority task starter.

   USE AT OWN RISK! THIS PROGRAM COMES WITHOUT *ANY*
   WARRANTY WHATSOEVER!

   This program executes a program (as defined in PROGRAM)
   at a given (usually higher than normal) priority (as
   defined in PRIORITY). Edit PROGRAM and PRIORITY to meet
   your needs. Sensible programs may be: cdrecord, MP3 or
   DVD players, etc. Sensible priorities range from -1 to -5.

   The program itself is *not* started SUID root but as the
   user invoking the task starter. Root privilegues are used
   *only* to set the desired priority (aka. nice level).

    Compile and install as follows:
   # gcc increase_ff_priority.c -o increase_ff_priority
   # sudo cp increase_ff_priority /usr/local/bin
   # sudo chmod +s /usr/local/bin/increase_ff_priority

   2002, Patrick Schemitz <[email protected]>
   This program is released under the GNU Public License (GPL).

*********************************************************************/


/* The program to start with higher priority, with full path. */
#define PROGRAM "/usr/bin/firefox"


/* The desired priority, ranging from 19 (very low) to -20 (too
   high). Sensible values from 0 (normal) to maybe -5 (pretty
   high).

   WARNING:

   DO NOT SET THIS TO -20, OR YOU WILL SEVERELY AND PERMANENTLY
   DAMAGE YOUR SYSTEM! Setting the priority too high causes your
   program to interfere with very important system tasks (like
   the swapper or the RAID daemons). Messing with the RAID
   daemons is a *VERY* bad idea!
*/
#define PRIORITY -20


/* You can increase this if your program can sensibly handle a
   very large number of parameters (like tar, ls, etc); normally
   the default (1024) should be plenty.
*/
#define MAX_ARGS 1024


int main (int argc, char **argv)
{
    int i;
    pid_t pid;
    char* args [MAX_ARGS+1] = { PROGRAM, NULL };
    /* first thing to do: drop privilegues */
    seteuid(getuid());
    /* copy over arguments */
    for (i=1; i<argc && i<MAX_ARGS; i++)
    args[i] = argv[i];
    args[i] = NULL;
    /* fork process for the program to be started */
    pid = fork();
    if (pid == 0) {
    /* new process starts actual program */
    execvp(PROGRAM,args);
    } else {
    /* original process regains privilegues */
    seteuid(0);
    /* set desired priority */
    setpriority(PRIO_PROCESS,pid,PRIORITY);
    }
    return 0;
}

第 2 步:创建以下名为 increase_ff_priority.desktop 的文件并复制到 ~/.config/autostart(使用 sudo cp)

[Desktop Entry]
Type=Application
Exec="/usr/local/bin/increase_ff_priority"
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_IN]=increase_ff_priority
Name=increase_ff_priority
Comment[en_IN]=AnyComment
Comment=AnyComment

步骤 3:重新启动

步骤 4:使用 Top 命令或系统监视器检查优先级。

相关内容