Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中更改当前进程的优先级_C_Unix_Cross Platform_Process Management - Fatal编程技术网

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

在C中更改当前进程的优先级,c,unix,cross-platform,process-management,C,Unix,Cross Platform,Process Management,在Windows上,我可以执行以下操作: HANDLE hCurrentProcess = GetCurrentProcess(); SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS); 如何在*nix上执行相同的操作?试试: #include <sys/time.h> #include <sys/resource.h> int main(){ setpriority(PRIO_PRO

在Windows上,我可以执行以下操作:

HANDLE hCurrentProcess = GetCurrentProcess();

SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS);
如何在*nix上执行相同的操作?

试试:

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

int main(){
    setpriority(PRIO_PROCESS, 0, -20);
}
#包括
#包括
int main(){
设置优先级(优先级进程,0,-20);
}
请注意,您必须以超级用户身份运行才能使其正常工作


(如需了解更多信息,请在提示符处键入“man setpriority”。

@allain您能否在不成为超级用户的情况下降低自己进程的优先级


当然。但是,请注意,这是一条单行道。你甚至不能回到你开始的地方。当系统负载较大时,即使是相当小的优先级降低也会对运行时间产生惊人的影响。

如果在unix下执行类似操作,您希望(作为root)chmod任务并设置s位。然后,您可以在运行时更改运行身份、优先级、线程调度等

只要你不是在编写一个包含bug的大规模多线程应用程序,这样你就可以接管一个48 CPU的盒子,没有人可以关闭你,因为你的每个CPU都以100%的速度旋转,所有线程都设置为SHED_FIFO(运行到完成)以root运行


不。。我不会凭经验说话

你能在不成为超级用户的情况下降低自己流程的优先级吗?是的,尽管你通常不能再次提高优先级。