进程优先级的设置
2013-07-01 15:06
155 查看
可以通过改变进程的优先级来保证进程优先运行。在 Linux下,通过系统调用 nice()可以改变进程的优先级。nice()系统调用用来改变调用进程的优先级。函数声明如下:
getpriority() 和 setpriority() 两函数的声明:
getpriority() 函数:
该函数返回一组进程的优先级。参数 which 和 who 组合确定返回哪一组进程的优先级。which 的可能取值以及 who 的意义如下:
PRIO_PROCESS : 一个特定的进程,此时 who 的取值为进程 ID。
PRIO_PGRP : 一个进程组的所有进程,此时 who 的取值为进程组 ID。
PRIO_USER : 一个用户拥有的所有进程,此时参数 who 取值为实际用户 ID 。
getpriority() 函数如果调用成功返回指定进程的优先级,如果出错将返回 -1,并设置 errno 的值。errno 可能的取值如下:
ESRCH : which 和 who 的组合与现存的所有进程均不匹配
EINVAL : which 是个无效的值
注意:当指定的一组进程的优先级不同时, getpriority() 将返回其中优先级最低的一个。此外,当 getpriority() 返回 -1 时,可能是发生错误,也有可能是返回的是指定进程的优先级。区分它们的惟一方法是在调用 getpriority() 前将 errno 清零。如果函数返回 -1 且 errno 不为零,说明有错误产生。
setpriority() 函数:
该函数用来指定进程的优先级。进程指定的方法与 getpriority() 函数相同。如果调用成功,函数返回指定进程的优先级,出错则返回 -1,并设置相应的 errno。除了产生与 getpriority() 相同的两个错误外,还有可能产生以下错误。
EPERM : 要设置优先级的进程与当前进程不属于同一个用户,并且当前进程没有 CAP_SYS_NICE 特许。
EACCES : 该调用可能降低进程的优先级并且进程没有 CAP_SYS_NICE 特许。
通过 getpriority() 和 setpriority() 完全可以改变进程的优先级。nice 系统调用是它们的一种组合形式,nice 系统调用等价于:
测试程序:
运行及输出:
#include <unistd.h> int nice( int increment );
getpriority() 和 setpriority() 两函数的声明:
#include <sys/resource.h> int getpriority( int which, int two ); int setpriority( int which, int who, int prio );
getpriority() 函数:
该函数返回一组进程的优先级。参数 which 和 who 组合确定返回哪一组进程的优先级。which 的可能取值以及 who 的意义如下:
PRIO_PROCESS : 一个特定的进程,此时 who 的取值为进程 ID。
PRIO_PGRP : 一个进程组的所有进程,此时 who 的取值为进程组 ID。
PRIO_USER : 一个用户拥有的所有进程,此时参数 who 取值为实际用户 ID 。
getpriority() 函数如果调用成功返回指定进程的优先级,如果出错将返回 -1,并设置 errno 的值。errno 可能的取值如下:
ESRCH : which 和 who 的组合与现存的所有进程均不匹配
EINVAL : which 是个无效的值
注意:当指定的一组进程的优先级不同时, getpriority() 将返回其中优先级最低的一个。此外,当 getpriority() 返回 -1 时,可能是发生错误,也有可能是返回的是指定进程的优先级。区分它们的惟一方法是在调用 getpriority() 前将 errno 清零。如果函数返回 -1 且 errno 不为零,说明有错误产生。
setpriority() 函数:
该函数用来指定进程的优先级。进程指定的方法与 getpriority() 函数相同。如果调用成功,函数返回指定进程的优先级,出错则返回 -1,并设置相应的 errno。除了产生与 getpriority() 相同的两个错误外,还有可能产生以下错误。
EPERM : 要设置优先级的进程与当前进程不属于同一个用户,并且当前进程没有 CAP_SYS_NICE 特许。
EACCES : 该调用可能降低进程的优先级并且进程没有 CAP_SYS_NICE 特许。
通过 getpriority() 和 setpriority() 完全可以改变进程的优先级。nice 系统调用是它们的一种组合形式,nice 系统调用等价于:
int nice( int increamet ) { int oldpro = getpriority( PRIO_PROCESS, getpid() ); return setpriority( PRIO_PROCESS, getpid(), oldpro + increament ); }
测试程序:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <sys/resource.h> #include <sys/wait.h> #include <stdlib.h> int main(void) { pid_t pid; int stat_val = 0; int oldpri, newpri; printf("nice study\n"); pid = fork(); switch( pid ) { case 0: printf("Child is running, Curpid is %d, Parentpid is %d\n", pid, getppid()); oldpri = getpriority(PRIO_PROCESS, getpid()); printf("Old priority = %d\n", oldpri); newpri = nice(2); printf("New priority = %d\n", newpri); exit(0); case -1: perror("Process creation failed\n"); break; default: printf("Parent is running,Childpid is %d, Parentpid is %d\n", pid, getpid()); break; } wait(&stat_val); exit(0); }
运行及输出:
beyes@linux-beyes:~/C/base> ./nice.exe nice study Child is running, Curpid is 0, Parentpid is 4421 Old priority = 0 New priority = 2 Parent is running,Childpid is 4422, Parentpid is 4421
相关文章推荐
- 【android】进程优先级(Framework设置优先级,LowMemoryKiller查杀)
- linux CPU隔离和进程调度优先级设置
- LINUX进程优先级的设置和一些注意地方
- C++ 进程线程优先级设置
- linux 使用nice命令设置进程优先级
- 进程优先级获取和设置
- 进程的优先级设置与获取,进程时间
- linux进程优先级设置
- 设置当前进程优先级
- 设置进程的优先级
- 进程的优先级设置与获取,进程时间
- linux 进程优先级 之设置实时进程 (另一种方式是设置nice值)
- linux进程优先级、进程nice/renice设置
- C# 设置进程优先级
- linux nice 设置进程优先级
- linux下设置进程优先级方法!
- 进程优先级的设置
- 给进程设置实时优先级
- VS C++ 进程篇之一设置优先级
- VB设置进程优先级