日历时间
-
#include <sys/time.h>
-
int gettimeofday(struct timeval *tv, struct timezone *tz);
-
/*returns 0 on success, or -1 on error*/
-
struct timeval{
-
time_t tvsec; /*seconds since 00:00:00, 1 Jan 1970 UTC*/
-
suseconds_t tv_usec;/*Additional microseconds (long int)*/
-
}
-
-
#include <time.h>
-
time_t time(time_t *timep);
-
/*returns number of seconds since the Epoch, or (time_t)-1 on error*/
如果timep不为NULL,那么还会将自Epoch依赖的秒数置于timep所指向的位置
时间转换函数如下:
-
#include <time.h>
-
char *ctime(const time_t *timep);
-
/*returns pointer to statically allocated string terminated by newline and \0 on success, or NULL on error*/
SUSv3规定,调用ctime(), gmtime(), localtime(), asctime()中任意一个函数,都可能会覆盖其他函数返回,且静态分配的数据结构
-
#include <time.h>
-
struct tm *gmtime(const time_t *timep);
-
struct tm *localtime(const time_t *timep);
-
/*both return a pointer to a statically allocated broken-down time structure on success, or NULL on error*/
-
gmtime_r();localtime_r();分别是上述两个函数的可重入版本
-
struct tm{
-
int tm_sec;
-
int tm_min;
-
int tm_hour;
-
int tm_mday;
-
int tm_mon;
-
int tm_year;
-
int tm_wday;// day of the week, Sunday = 0
-
int tm_yday;// day in the year, 1 Jan = 0
-
int tm_isdst;
-
/* daylight saving time flag;
-
>0, DST in effect;
-
=0,DST is not effect;
-
<0:DST information not available
-
*/
-
};
-
-
#include <time.h>
-
time_t mktime(struct tm *timeptr);
-
/*returns seconds sice the Epoch corresponding to timeptr on success, or (time_t)-1 on error*/
函数mktime()可能会修改timeptr所指向的结构体,至少会确保对tm_wday和tm_yday字段值的设置
-
#include <time.h>
-
char *asctime(const struct tm *timeptr);
-
/*returns pointer to statically allocated string terminated by newline and \0 on success, or NULL on error*/
-
-
#include <time.h>
-
size_t strftime(char *outstr, size_t maxsize, const char *format, const struct tm *timeptr);
-
/*returns number of bytes placed in outstr on success, or 0 on error*/
软件时钟jiffies
lunux上各种系统调用的精度是受限于系统软件时钟的分辨率,它的度量单位被称为jiffies,其大小是定义在内核源代码的常量HZ。内核按照round-robin的分时调度算法分配CPU进程的单位。如,软时钟速度是100HZ,也就是说,一个jiffy是10ms
进程时间:
进程时间是进程创建后使用的CPU时间数量,用于记录的目的,内核把CPU时间分成以下两部分
1. 用户CPU时间是在用户模式下执行所花费的时间数量,有时也成为虚拟时间,对于程序来说,是它已经得到的CPU的时间
2. 系统CPU时间是在内核模式中执行所花费的时间数量。这是内核用于执行系统调用或代表程序执行其他任务的时间
-
#include <sys/time.h>
-
clock_t times(struct tms *buf);
-
/*returns number of clock ticks(sysconf(_SC_CLLK_TCK)) since arbitrary time in past on success, or (clock_t)-1 on error*/
-
-
struct tms{
-
clock_t tms_utime;/*user cpu time used by caller*/
-
clock_t tms_stime;/*system cpu time used by caller*/
-
clock_t tms_cutime;/*user cpu time of all (waited for) children*/
-
clock_t tms_cstime;/*system cpu time of all (waited for)children*/
-
};
由于times()描述中的任意时间点没有说明,导致只能取两次调用的差值,但是,还是可能会发生移除问题,所以才有了如下:
-
#include <time.h>
-
clock_t clock(void);
-
/*returns total cpu time used by calling process measured in CLOCKS_PER_SEC, or (clock_t)-1 on error*/
由于返回的计量单位是CLOCKS_PER_SEC,所以我们必须除以这个值来获得进程所使用程的CPU时间秒数
阅读(1149) | 评论(0) | 转发(0) |