以低优先级启动进程(使用Runtime.exec/ProcessBuilder.start) [英] Start a process (using Runtime.exec / ProcessBuilder.start) with low priority

查看:46
本文介绍了以低优先级启动进程(使用Runtime.exec/ProcessBuilder.start)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在低优先级下启动CPU密集型系统进程,以免降低服务器速度.如何在Linux上执行此操作?

I need to start a CPU-intensive system process under low priority so that it doesn't slow down my server. How can I do this on Linux?

这类似于以下问题:

This is similar to this question: Start a Java process at low priority using Runtime.exec / ProcessBuilder.start? except on Linux instead of Windows. It's OK if the process's priority is changed after the process is started (as long as there isn't much delay).

推荐答案

使用

Run the command using /usr/bin/nice. For instance:

$ /usr/bin/nice -n 10 somecommand arg1 arg2

将以+10的精度运行 somecommand arg1 arg2 .(在Unix/Linux中,较大的niceness值将导致较低的调度程序优先级. nice 的范围通常从-19到+19.)

will run somecommand arg1 arg2 at a niceness of +10. (In Unix / Linux, a larger niceness value results in a lower scheduler priority. The range of nice is typically from -19 to +19.)

请注意,此解决方案是特定于平台的.它仅适用于Linux和Unix系统...

Note that this solution is platform specific. It will only work on Linux and Unix systems ...

跟进

应该像处理任何普通命令一样实例化ProcessBuilder.即

The ProcessBuilder should be instantiated as you would any normal command; i.e.

    new ProcessBuilder("nice", "-n", "10", "somecommand", "arg1", "arg2");

关于何时/如何拆分命令/参数没有任何黑魔法.该命令(例如 nice )语法确定其参数应为什么,并确定应如何在shell命令行上对其加引号,以及在使用 ProcessBuilder (或此问题的本机 exec * 系统调用).

There's no black magic about when / how to split commands / arguments. The command (e.g. nice) syntax determines what it arguments should be, and that determines both how they should quoted on the shell command line, and how they should be provided when using ProcessBuilder (or the native exec* syscalls of that matter).

我认为 waitFor()等不应该有问题,因为(AFAIK)/usr/bin/nice 命令使用 exec(不是 fork / exec )来运行提供的命令.试试吧...

I don't think that there should be problems with waitFor() etc, because (AFAIK) the /usr/bin/nice command uses exec (not fork / exec) to run the supplied command. Try it out ...

这篇关于以低优先级启动进程(使用Runtime.exec/ProcessBuilder.start)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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