1. 内核提供三种不同的方式来记录时间

Wall time (or real time):actual time and date in the real world
Process time:the time that a process spends executing on a processor 包括用户时间user time 和 系统时间system time
Monotonic time:use the system's uptime (time since boot) for this purpose,guarantee that the time source is strictly linearly increasing
 
Unix表示绝对时间:the number of elapsed seconds since the epoch, which is defined as 00:00:00 UTC on the morning of 1 January 1970
 
On Linux, the frequency of the system timer is called HZ,The value of HZ is architecture-specific 
POSIX functions that return time in terms of clock ticks use CLOCKS_PER_SEC to represent the fixed frequency
 

2. 时间表示的数据结构

 
原始表示:
typedef long time_t;
That won't last long before overflowing!
 
微秒级精度表示:
#include <sys/time.h>
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
纳秒级精度表示:
#include <time.h>
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

3. 获取当前时间

#include <time.h>
time_t time (time_t *t);

time() returns the current time represented as the number of seconds elapsed since the epoch

#include <sys/time.h>
int gettimeofday (struct timeval *tv, struct timezone *tz);
gettimeofday() places the current time in the timeval structure pointed at by tv and returns 0
参数tz总是为NULL
 
struct timeval tv;
int ret;
ret = gettimeofday (&tv, NULL);
if (ret)
perror ("gettimeofday");
else
printf ("seconds=%ld useconds=%ld\n", (long) tv.sec, (long) tv.tv_usec);
#include <sys/times.h>
struct tms {
clock_t tms_utime; /* user time consumed */
clock_t tms_stime; /* system time consumed */
clock_t tms_cutime; /* user time consumed by children */
clock_t tms_cstime; /* system time consumed by children */
};
clock_t times (struct tms *buf);
User time is the time spent executing code in user space.
System time is the time spent executing code in kernel space.
 
 

4. 设置当前时间

#include <time.h>
int stime (time_t *t); #include <sys/time.h>
int settimeofday (const struct timeval *tv, const struct timezone *tz);

参数tz总是为NULL

struct timeval tv = { ,  };
int ret;
ret = settimeofday (&tv, NULL);
if (ret)
perror ("settimeofday");
 

5. sleep休眠

/*  puts the invoking process to sleep for the number of seconds  */
#include <unistd.h>
unsigned int sleep (unsigned int seconds);
/*  usleep() puts the invoking process to sleep for usec microseconds */
#define _XOPEN_SOURCE 500
#include <unistd.h>
int usleep (unsigned int usec);
#include <sys/select.h>
int select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);

select函数将参数n设为0,三个检测事件集合全设为NULL,这时select就等同于一个精确时间的休眠函数,而且这种用法最具有可移植性

struct timeval tv = { .tv_sec = , .tv_usec =  };
select (0, NULL, NULL, NULL, &tv);

6. 定时器

Timers provide a mechanism for notifying a process when a given amount of time elapses
 
简单定时器:
#include <unistd.h>
unsigned int alarm (unsigned int seconds);

schedules the delivery of a SIGALRM signal to the invoking process after seconds of real time have elapsed

void alarm_handler (int signum)
{
printf ("Five seconds passed!\n");
}
void func (void)
{
signal (SIGALRM, alarm_handler);
alarm ();
pause ();
}

间隔定时器:

#include <sys/time.h>
int getitimer (int which, struct itimerval *value);
int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue); struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
}; struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
void alarm_handler (int signo)
{
printf ("Timer hit!\n");
}
void foo (void)
{
struct itimerval delay;
int ret;
signal (SIGALRM, alarm_handler);
delay.it_value.tv_sec = ;
delay.it_value.tv_usec = ;
delay.it_interval.tv_sec = ;
delay.it_interval.tv_usec = ;
ret = setitimer (ITIMER_REAL, &delay, NULL);
if (ret) {
perror ("setitimer");
return;
}
pause ();
}

Linux System Programming 学习笔记(十一) 时间的更多相关文章

  1. Linux System Programming 学习笔记(六) 进程调度

    1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...

  2. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  3. Linux System Programming 学习笔记(七) 线程

    1. Threading is the creation and management of multiple units of execution within a single process 二 ...

  4. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  5. Linux System Programming 学习笔记(一) 介绍

    1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...

  6. Linux System Programming 学习笔记(十) 信号

    1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0)   内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...

  7. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  8. Linux System Programming 学习笔记(八) 文件和目录管理

    1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...

  9. Linux System Programming 学习笔记(五) 进程管理

    1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...

随机推荐

  1. CVE-2011-0065

      环境 备注 操作系统 Windows 7 x86 sp1 专业版 漏洞软件 Firefox 版本号:3.6.16 调试器 Windbg 版本号:6.12.0002.633 0x00 漏洞描述 在F ...

  2. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  3. 【原】基于matlab的蓝色车牌定位与识别---绪论

    本着对车牌比较感兴趣,自己在课余时间摸索关于车牌的定位与识别,现将自己所做的一些内容整理下,也方便和大家交流. 考虑到车牌的定位涉及到许多外界的因素,因此有必要对车牌照的获取条件进行一些限定: 一.大 ...

  4. OI算法复习汇总

    各大排序 图论: spfa floyd dijkstra *拉普拉斯矩阵 hash表 拓扑排序 哈夫曼算法 匈牙利算法 分块法 二分法 费马小定理: a^(p-1) ≡1(mod p) 网络流 二分图 ...

  5. [JZOJ] 5837.Omeed

    先摆出来这个式子 \[ score=A\sum S_i+B\sum S_i\times f(i) \] 先研究\(f\)函数(也就是Combo函数) 显然的有 \[ f(i)=P_i(f(i-1)+1 ...

  6. rest_framework之status HTTP状态码

    Django Rest Framework有一个status.py的文件 通常在我们Django视图(views)中,HTTP状态码使用的是纯数字,像400,404,200,304等,并不是那么很好理 ...

  7. MySQL创建根据经纬度计算距离的函数

    按照经纬度计算距离 日常开发中,特别是做微信项目时,经常会遇到根据用户地理位置来展示附近商家的功能,通常解决这种问题的思路是,后台设置商家的经纬度,然后再根据前台传的经纬度进行计算,具体经纬度转换以及 ...

  8. 《零基础入门学习Python》【第一版】视频课后答案第002讲

    测试题答案: 0. 什么是BIF?BIF 就是 Built-in Functions,内置函数.为了方便程序员快速编写脚本程序(脚本就是要编程速度快快快!!!),Python 提供了非常丰富的内置函数 ...

  9. day13-生成器

    def generator(): print(1) yield 'a' rcp = generator() print(rcp.__next__()) 只要含有yield关键字的函数都是生成器函数.y ...

  10. stm32之ADC应用实例(单通道、多通道、基于DMA)

    文本仅做记录.. 硬件:STM32F103VCT6 开发工具:Keil uVision4 下载调试工具:ARM仿真器 网上资料很多,这里做一个详细的整合.(也不是很详细,但很通俗).  所用的芯片内嵌 ...