编程 给定戒撸一段时间后秒射求出它的秒数

博客访问: 299726
博文数量: 64
博客积分: 2438
博客等级: 大尉
技术积分: 821
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
在学习信号时遇到了pause这个函数和alarm函数,可以用这两个处理一个有意思的问题,就是让你的程序每隔一定的时间执行一次function下面是代码:
&* filename: exec_function.c
&* description: from time to time to perform a function
&**************************************************************************************
&* struct itimerval{
struct timerval it_
struct timerval it_
&* struct timerval{
&* int setitimer( int which, const struct itimerval *value, struct itimerval *ovalue );
&**************************************************************************************
#include <stdio.h> //printf()
#include <unistd.h> //pause()
#include <signal.h> //signal()
#include <string.h> //memset()
#include <sys/time.h> //struct itimerval, setitimer()
void printMsg(int);
int main(void)
&&&&int res = 0;
&&&&struct itimerval tick;
&&&&signal( SIGALRM, printMsg );
&&&&memset( &tick, 0, sizeof(tick) );
&&&&tick.it_value.tv_sec = 1;
&&&&tick.it_value.tv_usec = 0;
&&&&tick.it_interval.tv_sec = 1;
&&&&tick.it_interval.tv_usec = 0;
&&&&res = setitimer( ITIMER_REAL, &tick, NULL );
&&&&if( res ){
&&&&&&&&printf( "set timer failed\n" );
&&& //int i = 5;
&&&&while( 1 ){
&&&&&&&&pause();
&&&&return 0;
void printMsg(int sig)
&&&&printf( "Hello, world !\n" );
程序在setitimer设置的时间到了之后就会呼叫SIGALRM signal,函数setitimer的第二个参数设置timeout时间,timerval it_value 设置第一次执行function的延迟秒数,timerval it_interval 设置以后每隔多长时间执行一次function, 函数setitimer第一个参数ITIMER_REAL表示一read-time方式减少timer,在timerout时会发送出SIGARLM signal,第三个参数会存放旧的timeout,如果不需要就NULL。while(1)是调用pause函数无限等待SIGARLM signal 以间隔执行function。如有什么不对的地方还希望指出来。。。
阅读(3421) | 评论(1) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
很好的, 收藏了
推荐一个博客,提供很多免费软件编程电子书下载:
http://free-
请登录后评论。编写程序,提示用户输入秒数,每隔一秒显示提示剩余的秒数的信息,并在时间结束时终止程序。JAVA编程_百度知道golang编程之时间编程
| Go语言中文网 | Golang中文社区 | Golang中国
<meta name="author" content="polaris ">
golang编程之时间编程
golang编程之时间编程
stephen830
golang编程之时间编程
编程离不开时间,时间管理,严格的说分成两块,一个是当前的时刻,对应的是一个点,还有是一段时间间隔。本文简单的讲讲go的时间相关的编程,比较简单,高手可以一笑而过。
golang对时间的支持,是package time做的事儿,里面有好多的函数,我就不一一举例学习,毕竟这是官方文档干的事情。我们初步的学习下常用的函数。
第一个是UNIX epoch time,确切的说就是自 00:00:00 GMT以来的秒数,不知道如何获取的,可以在shell下执行 date +%s
manu@manu-hacks:~/code/go/self$ date +%s
熟悉Linux下C编程的就是time函数的返回值:
#include <time.h>
time_t now = time(NULL);
golang中一个很重要的表征时间的数据类型是Time,基本就是三个成员变量 sec ,nsec,Location,详细意思可以参看注释。
type Time struct {
// sec gives the number of seconds elapsed since
// January 1, year 1 00:00:00 UTC.
// nsec specifies a non-negative nanosecond
// offset within the second named by Seconds.
// It must be in the range [0, ].
nsec int32
// loc specifies the Location that should be used to
// determine the minute, hour, month, day, and year
// that correspond to this Time.
// Only the zero Time has a nil Location.
// In that case it is interpreted to mean UTC.
loc *Location
OK,如何取到UNIX epoch time.
now := time.Now()
用time package中Now()函数获取到当前的时间信息,Now()函数非常的重要,他是后面一切转换的起始点。从Now()我们获取到了Time,从 Time类型我们从容的获取到UNIX epoch time ,自然获取到year ,month ,day,weekday, hour,minute,second,nanosecond.
获取UNIX epoch time:
var epoch_seconds int64 = now.Unix()
func (t Time) Year() int
cur_year := now.Year()
func (t Time) Month() Month
cur_month := now.Month()
if cur_month == time.November {
Month是int类型,fmt.Printf("%v") 或者fmt.Println可以打印出November来,同时Month type有String()函数,输出“November”这样的字符串
January Month = 1 + iota
year mon day,这些都可以在Date函数中一并返回:
func (t Time) Date() (year int, month Month, day int)
year,mon,day = now.Date()
func (t Time) Hour() int
cur_hour := now.Hour()
Minute可以通过Minute()返回,second可以通过Second()返回。
time还提供了Clock()的同时返回 hour,minute,second = now.Clock().
在C语言中,我们用gmtime_r获取UTC时间,localtime_r获取本地时间,Golang我们也可以做到
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
time_t now = time(NULL);
printf("elapsed %d second since
00:00:00\n",now);
struct tm now_utc_tm ={0};
if (gmtime_r(&now,&now_utc_tm) != NULL)
printf("UTC time is %d-%02d-%02d %02d:%02d:%02d %s\n",
now_utc_tm.tm_year+1900,now_utc_tm.tm_mon,
now_utc_tm.tm_mday,now_utc_tm.tm_hour,
now_utc_tm.tm_min,now_utc_tm.tm_sec,now_utc_tm.tm_zone);
struct tm now_local_tm = {0} ;
if(localtime_r(&now,&now_local_tm) != NULL)
printf("local time is %d-%02d-%02d %02d:%02d:%02d %s\n",
now_local_tm.tm_year+1900,now_local_tm.tm_mon,
now_local_tm.tm_mday,now_local_tm.tm_hour,
now_local_tm.tm_min, now_local_tm.tm_sec,now_local_tm.tm_zone);
golang的版本是:
package main
import "fmt"
import "time"
func main(){
now := time.Now()
year,mon,day := now.UTC().Date()
hour,min,sec := now.UTC().Clock()
zone,_ := now.UTC().Zone()
fmt.Printf("UTC time is %d-%d-%d %02d:%02d:%02d %s\n",
year,mon,day,hour,min,sec,zone)
year,mon,day = now.Date()
hour,min,sec = now.Clock()
zone,_ = now.Zone()
fmt.Printf("local time is %d-%d-%d %02d:%02d:%02d %s\n",
year,mon,day,hour,min,sec,zone)
输出分别是:
C版本的输出
------------------
time is 2013-10-22 15:49:18 GMT
local time is 2013-10-22 23:49:18 CST
go版本的输出
---------------------
time is 2013-11-22 15:51:22 UTC
local time is 2013-11-22 23:51:22 CST
-------------------------------------------------------------------------------------------------------------------------------------------------------------
我们另一个关心的话题,是时间间隔,比如我们profile一个以非常耗时的function,我们会在函数开始前记下时刻值,函数结束后,再次记录下时刻值,然后两者的差值,就是函数运行时间。
这表明Time是可以相减的,
start_time := time.Now()
expensive_function
end_time :=time.Now()
var duration Duration = end_time.Sub(start_time)
Duration是一种数据类型,其实是个int64类型,表征的是两个时刻之间的纳秒数。
type Duration int64
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
Duration类型有Minutes()/Second()/Nanoseconds(), 将duration折算成分钟/秒/纳秒。
now := time.Now()
time.Sleep(3*time.Second);
end_time := time.Now()
var dur_time time.Duration = end_time.Sub(now)
var elapsed_min float64 = dur_time.Minutes()
var elapsed_sec float64 = dur_time.Seconds()
var elapsed_nano int64 = dur_time.Nanoseconds()
fmt.Printf("elasped %f minutes or \nelapsed %f seconds or \nelapsed %d nanoseconds\n",
elapsed_min,elapsed_sec,elapsed_nano)
输出如下:
elasped 0.050005 minutes or
elapsed 3.000292 seconds or
nanoseconds
------------------------------------------------------------------------------------------------------------------------------------------------第二部分描述Duration明显用到了Sleep()函数,这个函数是以纳秒为单位的,相当于C语言中的nanosleep()
#include <time.h>
nanosleep(): _POSIX_C_SOURCE >= 199309L
int nanosleep(const struct timespec *req, struct timespec *rem);
unsigned int sleep(unsigned int seconds);
Go中的time.Sleep一律是以纳秒为单位的,当然本质是Duration类型:
如果sleep 3秒中需要写成:
time.Sleep()
这太不方便了,所以,Golang可以写成
time.Sleep(3*time.Second);
这样可读性就好多了,当然还有time.Minute,time.Hour
这个time package还有很多其他的内容,我就不一一赘述了。参考文献: 1 golang package time
支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
支持 @ 本站用户;支持表情(输入 : 提示),见
记住登录状态
还不是会员浏览器不支持嵌入式框架,或被配置为不显示嵌入式框架。

我要回帖

更多关于 给定正数w i 的文章

 

随机推荐