在C中更改当前进程的优先级
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
请注意,您必须以超级用户身份运行才能正常工作。
(有关详细信息,请在提示时键入“man setpriority”。)
Try:
Note that you must be running as superuser for this to work.
(for more info, type 'man setpriority' at a prompt.)
@ allin
当然。 但请注意,这是一条单行道。 你甚至无法回到起点。 当系统负载很大时,即使是相当小的优先级降低也会对运行时间产生惊人的巨大影响。
@ allain
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.
如果在 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 ....