Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间
2. 时间表示的数据结构
typedef long time_t;
#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);
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);
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. 定时器
#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 学习笔记(十一) 时间的更多相关文章
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
- 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 ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...
- Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
- Linux System Programming 学习笔记(五) 进程管理
1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...
随机推荐
- Bootstrap历练实例:大小Well
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- [LUOGU]P1508 Likecloud-吃、吃、吃
题目背景 问世间,青春期为何物? 答曰:"甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!" 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日 ...
- skimage学习(一)
skimage即是Scikit-Image.基于python脚本语言开发的数字图片处理包 skimage包由许多的子模块组成,各个子模块提供不同的功能.主要子模块列表如下: data子模块学习 导入d ...
- (72)zabbix监控日志文件 MySQL日志为例
一般情况下,日志最先反映出应用当前的问题,在海量日志里面找到我们异常记录,然后记录下来,并且根据情况报警,大家可以监控系统日志.nginx.Apache.业务日志. 这边我拿常见的MySQL日志做监控 ...
- 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
使用ab( apache benchmark )测试的时候,使用如下命令: ab -n 15000 -c 200 http://localhost/abc/abc.php 执行操作一定条数,或连续 ...
- http 基础与通讯原理
目录 http 基础与通讯原理 Internet 与中国 1990年10月 注册CN顶级域名 1993年3月2日 接入第一根专线 1994年4月20日 实现与互联网的全功能连接 1994年5月21日 ...
- 使用三层交换配置DHCP为不同VLAN分配IP地址
三层交换的原理以及DHCP的原理,作者在这里就不详细的解释了,在这里通过一个案例来了解使用三层交换做DHCP服务器,并为不同网段分配IP地址.在生产环境中,使用路由器或交换机做DHCP服务器要常见一些 ...
- Vue木桶布局插件
公司最近在重构,使用的是Vue框架.涉及到一个品牌的布局,因为品牌的字符长度不一致,所以导致每一个的品牌标签长短不一.多行布局下就会导致每行的品牌布局参差不齐,严重影响美观.于是就有了本篇的木 ...
- poj-1700 crossing river(贪心题)
题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...
- HDU:2255-奔小康赚大钱(KM算法模板)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...