系统相关
首页 > 系统相关> > linux系统编程-时间

linux系统编程-时间

作者:互联网

程序需要关注两种时间:一种是真实时间(时间戳),第二种是进程使用CPU的时间总量

1、真实时间之间的转换

在这里插入图片描述
真实时间格式:

struct tm{
	int tm_sec;
	int tm_min;
	int tm_hour;
	int tm_mday;
	...
};
struct timeval{
	time_t tv_sec;
	suseconds_t tv_usec;
};

2. 进程使用CPU的时间

进程使用CPU的时间包括用户模式下执行所花费的时间内核模式中执行所花费的时间

clock_t times(truct tms *buf);

返回结构体:

truct tms{
	clock_t tms_utime; //调用times的进程用户cpu时间
	clock_t tms_stime; //调用times的进程系统cpu时间
	clock_t tms_cutime; //用户cpu时间(包含该进程所有子进程)
	clock_t tms_cstime; //系统cpu时间(包含该进程所有子进程)
}

注意:times()的返回值的计量单位是 CLOCKS_PER_SEC,所以我们必须除以这个值来获得进程
所使用的 CPU 时间秒数,CLOCKS_PER_SEC通过**sysconf(_SC_CLK_TCK)**获取。

clock_t clock(void);

clock调用的返回值描述了调用进程使用的总的 CPU 时间(包括用户和系统)。

标签:tms,clock,编程,系统,tm,时间,linux,进程,CPU
来源: https://blog.csdn.net/weixin_40555950/article/details/120799871