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. SC || Chapter 8

    栈:方法调用和局部变量的存储位置,保存基本类型 堆:在一块内存里分为多个小块,每块包含 一个对象,或者未被占用

  2. java基础——Map集合

    Map以键值对的形式存储数据,其中Map.entry,是Map的内部类,它用来描述Map中的键值对.Map是一个接口,HashMap是他的一个实现类 Map中有几个重要的方法: get(Object ...

  3. redis的一个bug

    清楚redis缓存的时候,出现以下问题: (error) MISCONF Redis is configured to save RDB snapshots, but is currently not ...

  4. NOIP2013 乌龟棋

    描述 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点 ...

  5. 洛谷 P1228 【地毯填补问题】

    事实上感觉四个的形状分别是这样: spj报错: 1:c 越界 2:x,y 越界 3:mp[x][y] 已被占用 4:mp[x][y] 从未被使用 题解: 初看这个问题,似乎无从下手,于是我们可以先考虑 ...

  6. 【Git版本控制】GitLab Fork项目的工作流程

    转载自简书: GitLab Fork项目工作流程

  7. 【Python学习之五】高级特性5(切片、迭代、列表生成器、生成器、迭代器)

    5.迭代器 由之前的生成器可知,for循环用于可迭代对象:Iterable.包括集合数据类型: list.tuple.dict.set.str 等,以及两种生成器.判断迭代器,使用 isinstanc ...

  8. LeetCode1089复写零

    问题: 给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移. 注意:请不要在超过该数组长度的位置写入元素. 要求:请对输入的数组 就地 进行上述修改,不要 ...

  9. vue使用原生js实现滚动页面跟踪导航高亮

    需要使用vue做一个专题页面. 滚动页面指定区域导航高亮. BetterScroll:可能是目前最好用的移动端滚动插件 如何自定义CSS滚动条的样式? 监听滚动页面事件,对比当前页面的位置与元素的位置 ...

  10. CSS3边框图片-像素虚边的问题

    虽然CSS3新增了这个功能,但是在W3school里面并没有给出具体详细的解释,还好网上不乏大神给你我们很全面的解释其中的原理-css3:border-image边框图像详解 边框图片的原理是四个角不 ...