linux cpu信息

/*
	who参数
	RUSAGE_SELF:只返回当前程序的CPU时间占用信息
	RUSAGE_CHILDREN:还包括子进程的CPU时间占用信息
*/

int getrusage(int who, struct rusage *usage);

举例

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

int main()
{
	struct rusage r_usage;

	getrusage(RUSAGE_SELF, &r_usage);

	printf("CPU usage: User = %ld.%06ld, System = %ld.%06ld\n",
		r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
		r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);
	
	return 0;
}
CPU usage: User = 0.000000, System = 0.000000

优先级
缺省优先级是0,合法优先级范围是-20到+20。数值越低代表有较高的优先次序,执行会较频繁

/*
	who参数
	PRIO_PROCESS 为进程识别码
	PRIO_PGRP 为进程的组识别码
	PRIO_USER 为用户识别码
*/
int getpriority(int which, int who);
int setpriority(int which, int who, int prio);

举例

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>

int main()
{
	int priority;

	priority = getpriority(PRIO_PROCESS, getpid());
	printf("Current priority = %d\n", priority);

	return 0;
}
Current priority = 0
posted @ 2019-03-15 17:23  thomas_blog  阅读(122)  评论(0编辑  收藏  举报