引用#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 。
- ESRCH : which 和 who 的组合与现存的所有进程均不匹配
- EINVAL : which 是个无效的值
setpriority() 函数:
该函数用来指定进程的优先级。进程指定的方法与 getpriority() 函数相同。如果调用成功,函数返回指定进程的优先级,出错则返回 -1,并设置相应的 errno。除了产生与 getpriority() 相同的两个错误外,还有可能产生以下错误。
- EPERM : 要设置优先级的进程与当前进程不属于同一个用户,并且当前进程没有 CAP_SYS_NICE 特许。
- EACCES : 该调用可能降低进程的优先级并且进程没有 CAP_SYS_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