在C中更改当前进程的优先级

发布于 2024-07-04 14:29:01 字数 191 浏览 6 评论 0原文

在 Windows 上我可以这样做:

HANDLE hCurrentProcess = GetCurrentProcess();

SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS);

我怎样才能在 *nix 上做同样的事情?

On Windows I can do:

HANDLE hCurrentProcess = GetCurrentProcess();

SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS);

How can I do the same thing on *nix?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转瞬即逝 2024-07-11 14:29:03

尝试:

#include <sys/time.h>
#include <sys/resource.h>

int main(){
    setpriority(PRIO_PROCESS, 0, -20);
}

请注意,您必须以超级用户身份运行才能正常工作。

(有关详细信息,请在提示时键入“man setpriority”。)

Try:

#include <sys/time.h>
#include <sys/resource.h>

int main(){
    setpriority(PRIO_PROCESS, 0, -20);
}

Note that you must be running as superuser for this to work.

(for more info, type 'man setpriority' at a prompt.)

千秋岁 2024-07-11 14:29:03

@ allin

你能在不成为超级用户的情况下降低你自己的进程的优先级吗?

当然。 但请注意,这是一条单行道。 你甚至无法回到起点。 当系统负载很大时,即使是相当小的优先级降低也会对运行时间产生惊人的巨大影响。

@ allain

Can you lower your own process' priority without being superuser?

Sure. Be aware, however, that this is a one way street. You can't even get back to where you started. And even fairly small reductions in priority can have startlingly large effects on running time when there is significant load on the system.

酷遇一生 2024-07-11 14:29:03

如果在 unix 下做这样的事情,你想(作为 root)chmod 你的任务并设置 s 位。 然后,您可以在运行时更改您的运行身份、优先级、线程调度等。

只要你没有编写一个带有 bug 的大规模多线程应用程序,这样你就可以接管一个 48 个 CPU 的机器,并且没有人可以关闭你,因为你的每个 CPU 都以 100% 的速度旋转,并且所有线程都设置为 SHED_FIFO (运行完成)以 root 身份运行。

不..我不会根据经验说话....

If doing something like this under unix your want to (as root) chmod you task and set the s bit. Then you can change who you are running as, what your priority is, your thread scheduling, etc. at run time.

It is great as long as you are not writing a massively multithreaded app with a bug in it so that you take over a 48 CPU box and nobody can shut you down because your have each CPU spinning at 100% with all thread set to SHED_FIFO (runs to completion) running as root.

Nah .. I wouldn't be speaking from experience ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文