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; } }