您的位置:首页 > 其它

进程的优先级设置与获取,进程时间

2016-09-28 08:32 169 查看
进程的优先级设置与获取:值越小优先级越高

       #include <sys/time.h>

       #include <sys/resource.h>

       int niece(int add)

       int getpriority(int which, id_t who);

       int setpriority(int which, id_t who, int prio);

    which:

    PRIO_PROCESS  进程

    PRIO_PGRP     进程组

    PRIO_USER     用户ID
    who: = 0

进程时间:

       #include <sys/times.h>

       clock_t times(struct tms *buf);

struct tms {

               clock_t tms_utime;  /* user time */

               clock_t tms_stime;  /* system time */

               clock_t tms_cutime; /* user time of children */

               clock_t tms_cstime; /* system time of children */

           };

#include<sys/resource.h>
#include<sys/times.h>
void PocessPriorityTest()
{
pid_t pid_1,pid_2;
pid_1 = fork();
if(pid_1 ==0)
{
cout<<"pid_1 priority = "<<nice(0)<<endl;
getpriority(PRIO_PROCESS,0);
setpriority(PRIO_PROCESS,0,1);
setpriority(PRIO_PROCESS,0,20);
cout<<"pid_1 priority = "<<nice(0)<<endl;
for(int i=0; i<4; i++)
{
cout<<"pid_1 "<<endl;
sleep(1);
}
exit(1);

}
else
{
pid_2 = fork();
if(pid_2 ==0)
{
struct tms tmsstart ,tmsend;
clock_t start = times(&tmsstart);
cout<<"pid_2 priority = "<<nice(0)<<endl;

for(int i=0; i<4; i++)
{
cout<<"pid_2 "<<endl;
sleep(1);
}
clock_t ends = times(&tmsend);
cout<<"time = "<<ends-start<<endl;

exit(1);
}
waitpid(pid_1,NULL,0);
waitpid(pid_2,NULL,0);
cout<<"process end"<<endl;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: