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

来源:互联网 发布:怪物猎人3捏脸数据库 编辑:程序博客网 时间:2024/06/03 06:30
进程的优先级设置与获取:值越小优先级越高
       #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;    }}

0 0
原创粉丝点击