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 ...
随机推荐
- java中的同步与异步
在多线程的环境中,经常会碰到数据的共享问题,即当多个线程需要访问同一个资源时,它们需要以某种顺序来确保该资源在某--时刻只能被-一个线程使用,否则,程序的运行结果将会是不可预料的,在这种情况下就必须对 ...
- 【转载】Alpha、Beta、RC、GA版本的区别
转自:http://www.blogjava.net/RomulusW/archive/2008/05/04/197985.html Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只 ...
- javaweb基础(22)_Servlet+JSP+JavaBean实战登陆
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- OC中的宏定义
我们都知道,宏定义是编译期常量.而OC是一种动态语言. 1.iOS系统版本判断的两个宏定义 __IPHONE_OS_VERSION_MAX_ALLOWED // iOS系统版本最大允许 __IPHON ...
- 看了下opengl相关的资料,踩了一个坑,记录一下
2019/03/10 下午看了下关于opengl的资料,然后把敲了下代码,然后程序报错了.代码如下: #include <glad/glad.h> #include <GLFW/gl ...
- 【启发式拆分】bzoj4059: [Cerc2012]Non-boring sequences
这个做法名字是从武爷爷那里看到的…… Description 我们害怕把这道题题面搞得太无聊了,所以我们决定让这题超短.一个序列被称为是不无聊的,仅当它的每个连续子序列存在一个独一无二的数字,即每个子 ...
- python入门:in 的用法(它在不在这个字符串里面)
#!/usr/bin/env python # -*- coding:utf-8 -*- #in 的用法(它在不在这个字符串里面) #ret(返回,译音:ruai特) #给s赋值为字符串“Alex S ...
- vue 顶级组件
快 有时候懒的把一些通用组件写到template里面去,而业务中又需要用到,比如表示loading状态这样组件. 如果是这样的组件,可以选择把组件手动初始化,让组件在整个app生命周期中始终保持活跃. ...
- ubuntu 设置定时任务
crontab -l #查看详情crontab -e #设置定时任务 * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时 ...
- 爬虫之scrapy工作流程
Scrapy是什么? scrapy 是一个为了爬取网站数据,提取结构性数据而编写的应用框架,我们只需要实现少量代码,就能够快速的抓取到数据内容.Scrapy 使用了 Twisted['twɪstɪd] ...