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. ADO 输入输出文本及获取指定字符串

    ---恢复内容开始--- 1.获取文本:声明别量,指定文本路径,获取文本内容. string Text=System.IO.File.ReadAllText(@"C:\xxx\xxx\xxx ...

  2. Python基础篇 -- 集合

    set集合 set 中的元素是不重复的,无序的 里面的元素必须是可hash的,(int str tuple bool) set 就是dict 类型的数据,但是不保存value 只保存 key set集 ...

  3. datatable css not work

    样式不显示问题 无论是放内联样式文件还是直接放HTML文件都不显示 后来发现是因为datatable是放在后面初始化,它自带的样式覆盖了我们自定义的样式 所以要注意写code时,很多时候不是code不 ...

  4. bug汇总

    bug 2018年8月23日 bug 1:散点图画不出来. plt.scatter(validation_examples["longitude"], validation_exa ...

  5. Unbuntu18.04如何备份

    以后可能用的到:https://blog.csdn.net/qq_35523593/article/details/78545530

  6. 小程序电脑调试没有问题,真机预览报错fail hand shake error

    今天在做小程序的过程中使用HTTPS请求数据时,遇到安卓机型无法获取到数据,通过一系列的排查,发现是因为ssl证书的问题,后来通过https://www.myssl.cn/tools/check-se ...

  7. 使用Blend的一些问题和技巧

    WPF开发,界面处理首选Blend,如果你开发了两年WPF都没接触过blend(当然这种几率不高),或者你刚接触WPF,可以考虑使用Blend,这货也算得上一个神器,上手也不难.以下有两位讲得不错,大 ...

  8. Nginx配置语法和日志

    nginx配置 配置文件 重启服务 http请求 nginx日志 一共有两个日志文件 在配置文件中添加这个,就可以在日志文件中看到请求的userAgent 配置语法的检查 nginx重新加载配置 发送 ...

  9. 常见python快捷键

    http://www.cnblogs.com/toutou/p/4778818.html Ctrl+/注释(取消注释)选择的行 Shift + Enter开始新行 Ctrl + Enter智能换行 T ...

  10. Python学习-day7 类 部分socket

    这周还是继续关于类的学习,在面向对象的学习过程中又学习了网络编程,并且提交了编写FTP的作业. 复习一下类的相关概念和定义 类      属性           实例变量:内存中           ...