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

进程的优先级设置与获取:值越小优先级越高
       #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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值