linux2.6优先级为0-139,其中100-139为普通进程,0-99为实时进程。
/*pri.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sched.h>
#include <sys/time.h>
#include <sys/resource.h>
int main(int argc,char *argv[])
{
unsigned int i=0,j=0,k=0,h;
nice(-1);
int pid[1];
struct sched_param p[1];
printf("max_pri is:%d,min_pri:%d\n ",sched_get_priority_max(2),sched_get_priority_min(2));
struct sched_param para;
para.sched_priority =23;
sched_setscheduler(0,SCHED_FIFO,¶);
printf("Parent pid is %d policy is %d, prio: %d\n",getpid(),sched_getscheduler(0),para.sched_priority);
while(1);
}
按照以下步骤:
gcc -o pri pri.c
./pri &
[root@FPC10 zhisxu]# ./pri
max_pri is:99,min_pri:1
Parent pid is 9461 policy is 1, prio: 23
[root@FPC10 ~]# top
top - 12:51:57 up 13 days, 3:18, 6 users, load average: 1.54, 0.57, 0.25
Tasks: 116 total, 3 running, 113 sleeping, 0 stopped, 0 zombie
Cpu(s): 87.5%us, 7.3%sy, 0.0%ni, 5.0%id, 0.0%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 2031980k total, 901064k used, 1130916k free, 272964k buffers
Swap: 2031608k total, 0k used, 2031608k free, 499884k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
9461 root -24 -1 1500 328 276 R 100 0.0 1:39.03 pri
好像应该也可以用下面这种方法实现: pid=getpid();
sched_setscheduler(pid, SCHED_FIFO, ¶m);