首页 > 系统相关 >linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒

linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒

时间:2022-12-26 10:23:54浏览次数:46  
标签:struct tv 纳秒 time start gettimeofday tm sec linux

一、用select()函数实现非阻塞时的等待时间,用到结构体struct timeval {},这里就不多说了。

二、用gettimeofday()可获得微妙级(0.000001秒)的系统时间,调用两次gettimeofday(),前后做减法,从而达到定时或者计算时间的目的。

复制代码
原型:int gettimeofday(struct timeval *tv,struct timezone *tz),会把目前的时间tv所指的结构返回,当地时区的信息则放到tz所指的结构中。这两个结构都放在/usr/include/sys/time.h 中。

#include <stdio.h> 
#include <stdlib.h> //malloc要用,没有的话,会有警告信息:隐式声明与内建函数'malloc'不兼容。不过警告信息不用管也没事

#include <assert.h>
#include <sys/time.h>

int main()
{
float time_use=0;
struct timeval start;
struct timeval end;//struct timezone tz; //后面有说明
gettimeofday(&start,NULL); //gettimeofday(&start,&tz);结果一样
printf("start.tv_sec:%dn",start.tv_sec);
printf("start.tv_usec:%dn",start.tv_usec);

sleep(3);
gettimeofday(&end,NULL);
printf("end.tv_sec:%dn",end.tv_sec);
printf("end.tv_usec:%dn",end.tv_usec);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %fn",time_use);

//输出:time_use is 3001410.000000

//下面的采用指针的方式也可以,但是要注意指针类型若不分配内存的话,编译正确,但是运行结果会不对

/*************************************************

struct timeval *start;
struct timeval *end;
//struct timezone *tz;

start=(struct timeval *)malloc( sizeof(struct timeval) );
assert(start!=NULL);
end=(struct timeval *)malloc( sizeof(struct timeval) );
assert(start!=NULL);
gettimeofday(start,NULL);//gettimeofday(start,tz);

printf("start->tv_sec:%dn",start->tv_sec); //printf("(*start).tv_sec:%dn",(*start).tv_sec);一样
printf("start->tv_usec:%dn",start->tv_usec);

//printf("tz->tz_minuteswest:%dn",tz->tz_minuteswest);
//printf("tv->tz_dsttime:%dn",tz->tz_dsttime);
sleep(3);
gettimeofday(end,NULL);//gettimeofday(end,tz);
printf("end->tv_sec:%dn",end->tv_sec);
printf("end->tv_usec:%dn",end->tv_usec);
time_use=(end->tv_sec-start->tv_sec)*1000+(end->tv_usec-start->tv_usec)/1000;//毫秒
printf("time_use is %fn",time_use);

//输出:time_use is 3001.000000

**********************************************/
/*****************************
struct timeval 
{ 
time_t tv_sec; // seconds 
suseconds_t tv_usec; // 微妙10-6
};

struct timezone
{
 int tz_minuteswest;//和格林威治时间差了多少分钟
int tz_dsttime; //日光节约时间的状态
}
******************************/

}
复制代码

三、最小到秒的时间的获取

复制代码
int time(char cnt) 
{

time_t t; //实例化time_t结构

struct tm *timenow1; //实例化tm结构指针
struct tm *timenow2; //实例化tm结构指针 

time(&t);//time函数读取现在的时间(国际标准时间非北京时间),然后传值给t
timenow1=localtime(&t); //localtime函数把从time取得的时间t换算成你电脑中的时间(就是你设置的地区)
printf("the current time1 is: %02d:%02d:%02dn",timenow1->tm_hour,timenow1->tm_min,timenow1- >tm_sec); 
printf("Local time1 is %sn",asctime(timenow1)); //上句中asctime函数把时间转换成字符,通过printf()函数输出

time(&t);

timenow2=localtime(&t);
printf("the current time2 is: %02d:%02d:%02dn",timenow2->tm_hour,timenow2->tm_min,timenow1->tm_sec); 
printf("Local time2 is %sn",asctime(timenow2));

if((timenow2->tm_hour==timenow1->tm_hour) && (timenow2->tm_min==timenow1->tm_min))
{
 n=timenow2->tm_sec-timenow2->tm_sec;
 printf("n is %dn",n);//秒
}

//注:如果想获得国际标准时间,将localtime换成gmtime函数
//注:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:

/*
struct tm //最小到秒,#include <time.h> 
{
 int tm_sec;//seconds 0-61
 int tm_min;//minutes 1-59
 int tm_hour;//hours 0-23
 int tm_mday;//day of the month 1-31
 int tm_mon;//months since jan 0-11
 int tm_year;//years from 1900
 int tm_wday;//days since Sunday, 0-6
 int tm_yday;//days since Jan 1, 0-365
 int tm_isdst;//Daylight Saving time indicator
};
*/
}
复制代码

四、纳秒

复制代码
函数原型:int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)

其中参数timespec定义是:

struct timespec 
{ 
 time_t tv_sec; /* seconds * /
 long tv_nsec; /* nanoseconds * /
}

实际应用(部分,不完整):

struct timesepc req; 
struct timespec rem;

int ret; 

req.tv_sec = 2; //这就表示2秒 
req.tv_nsec = 0; 

ret = nanosleep(&req, &rem); 

if (ret < 0) 
{ 
//.... 
}
复制代码

标签:struct,tv,纳秒,time,start,gettimeofday,tm,sec,linux
From: https://www.cnblogs.com/kn-zheng/p/17005105.html

相关文章

  • linux下静态库和动态库一些东西
    http://www.cnblogs.com/changefuture/archive/2011/12/22/2297460.htmlLinux 动态链接库和静态库示例文件预览文件目录树如下,如你所见,非常简单。libtest/  |--......
  • linux中shell变量$#,$@,$0,$1,$2的含义解释
    linux中shell变量$#,$@,$0,$1,$2的含义解释:变量说明:$$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set......
  • linux服务器大量TIME_WAIT的原因
    大家好,我是小林。之前有位读者面字节被问到两个很经典的TCP问题:第一个问题:服务端大量处于TIME_WAIT状态连接的原因。第二个问题:服务端大量处于CLOSE_WAIT状态连......
  • Linux基础
    用户和用户组管理su-root#切换到root用户useraddtom#添加用户tomuserdeltom#删除用户tom,但不删除用户目录userdel-rtom#删除用户tom并将用户目录删除us......
  • 有关linux中的文件IO的操作
    ​Linux应用编程中最需要掌握的基础就是文件I/O的操作,学习过linux或者有过了解的应该都会听过一句话:linux中一切皆文件,文件是linux系统的核心设计思想。所以掌握文件的操......
  • 关于linux下磁盘相关实验
    实验要求:创建一个至少有两个PV组成的大小为20G的名为testvg的VG,要求PE大小为16M,而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录新建用户archlinux,要求其......
  • Linux下常用操作汇总
    查看linux操作系统位数(1)终端输入:file/sbin/init如显示:/sbin/init:ELF32-bitLSBexecutable,Intel80386,version1(SYSV),forGNU/Linux2.6.9,dynamicall......
  • Linux进程调度
    进程调度概览多任务多任务操作系统就是能同时运行多个进程的操作系统:在单处理器上,这会产生多个进程在同时运行的幻觉在多处理器上,这会使多个进程在不同的处理器上真正......
  • 【Mysql】Linux安装Mysql
     目录: 1、检查是否已安装Mysql 2、官网下载MySQL安装包 3、上传mysql安装包并解压--> 移动并修改文件名 4、先检查是否有mysql用户组和mysql用......
  • Linux学习目录
    超哥带你学linuxDay1:(1)​​linux博客内容地址(2):​​服务器核心知识​​(3)​​linux入门介绍知识​​(4)​​vmware系统安装知识(5)​​远程连接linux知识今日作业:1.服......