Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1846849
  • 博文数量: 184
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2388
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-21 22:26
个人简介

90后空巢老码农

文章分类

全部博文(184)

文章存档

2021年(26)

2020年(56)

2019年(54)

2018年(47)

2017年(1)

我的朋友

分类: LINUX

2020-02-04 12:01:32

日历时间


点击(此处)折叠或打开

  1. #include <sys/time.h>
  2. int gettimeofday(struct timeval *tv, struct timezone *tz);
  3. /*returns 0 on success, or -1 on error*/
  4. struct timeval{
  5.     time_t tvsec; /*seconds since 00:00:00, 1 Jan 1970 UTC*/
  6.     suseconds_t tv_usec;/*Additional microseconds (long int)*/
  7. }

  8. #include <time.h>
  9. time_t time(time_t *timep);
  10. /*returns number of seconds since the Epoch, or (time_t)-1 on error*/
如果timep不为NULL,那么还会将自Epoch依赖的秒数置于timep所指向的位置
时间转换函数如下:





点击(此处)折叠或打开

  1. #include <time.h>
  2. char *ctime(const time_t *timep);
  3. /*returns pointer to statically allocated string terminated by newline and \0 on success, or NULL on error*/
SUSv3规定,调用ctime(), gmtime(), localtime(), asctime()中任意一个函数,都可能会覆盖其他函数返回,且静态分配的数据结构

点击(此处)折叠或打开

  1. #include <time.h>
  2. struct tm *gmtime(const time_t *timep);
  3. struct tm *localtime(const time_t *timep);
  4. /*both return a pointer to a statically allocated broken-down time structure on success, or NULL on error*/
  5. gmtime_r();localtime_r();分别是上述两个函数的可重入版本

点击(此处)折叠或打开

  1. struct tm{
  2.     int tm_sec;
  3.     int tm_min;
  4.     int tm_hour;
  5.     int tm_mday;
  6.     int tm_mon;
  7.     int tm_year;
  8.     int tm_wday;// day of the week, Sunday = 0
  9.     int tm_yday;// day in the year, 1 Jan = 0
  10.     int tm_isdst;
  11.     /* daylight saving time flag;
  12.     >0, DST in effect;
  13.     =0,DST is not effect;
  14.     <0:DST information not available
  15.     */
  16. };

  17. #include <time.h>
  18. time_t mktime(struct tm *timeptr);
  19. /*returns seconds sice the Epoch corresponding to timeptr on success, or (time_t)-1 on error*/
函数mktime()可能会修改timeptr所指向的结构体,至少会确保对tm_wday和tm_yday字段值的设置

点击(此处)折叠或打开

  1. #include <time.h>
  2. char *asctime(const struct tm *timeptr);
  3. /*returns pointer to statically allocated string terminated by newline and \0 on success, or NULL on error*/

  4. #include <time.h>
  5. size_t strftime(char *outstr, size_t maxsize, const char *format, const struct tm *timeptr);
  6. /*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时间是在内核模式中执行所花费的时间数量。这是内核用于执行系统调用或代表程序执行其他任务的时间


点击(此处)折叠或打开

  1. #include <sys/time.h>
  2. clock_t times(struct tms *buf);
  3. /*returns number of clock ticks(sysconf(_SC_CLLK_TCK)) since arbitrary time in past on success, or (clock_t)-1 on error*/

  4. struct tms{
  5.     clock_t tms_utime;/*user cpu time used by caller*/
  6.     clock_t tms_stime;/*system cpu time used by caller*/
  7.     clock_t tms_cutime;/*user cpu time of all (waited for) children*/
  8.     clock_t tms_cstime;/*system cpu time of all (waited for)children*/
  9. };
由于times()描述中的任意时间点没有说明,导致只能取两次调用的差值,但是,还是可能会发生移除问题,所以才有了如下:

点击(此处)折叠或打开

  1. #include <time.h>
  2. clock_t clock(void);
  3. /*returns total cpu time used by calling process measured in CLOCKS_PER_SEC, or (clock_t)-1 on error*/

由于返回的计量单位是CLOCKS_PER_SEC,所以我们必须除以这个值来获得进程所使用程的CPU时间秒数





阅读(1101) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~