• 12.时钟与信号


      1 #include <stdio.h>
      2 #include <time.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 #include <signal.h>
      6 //excise to ms
      7 #include <sys/time.h>
      8 
      9 //get time
     10 void TestTime()
     11 {
     12 
     13     time_t iTime = time(NULL);
     14     //get time(second).from 1900.01.01 to now
     15     time(&iTime);
     16     printf("iTime:%d
    ",(int)iTime);
     17 
     18     //time's struct
     19     struct tm *pNow;
     20     pNow = localtime(&iTime);
     21     //get current time
     22     printf("TIME:%4d-%02d-%02d %02:%02d:%02d week %d 
    "
     23            ,pNow->tm_year + 1900,pNow->tm_mon +1 ,pNow->tm_mday,
     24            pNow->tm_hour,pNow->tm_min,pNow->tm_sec,
     25            pNow->tm_wday);
     26 
     27     char szTime[40];
     28                          //data time
     29     strftime(szTime,40,"TIME:%x %X
    ",pNow);
     30     printf("%s
    ",szTime);
     31 
     32     strftime(szTime,40,"TIME: %Y-%m-%d %H:%M:%S week %w
    ",pNow);
     33     printf("%s
    ",szTime);
     34 }
     35 
     36 //get precise time
     37 void TestMicroTime()
     38 {
     39     struct timeval tv;
     40     struct timezone tz;
     41 
     42     gettimeofday(&tv,&tz);
     43 
     44     struct tm *pNow = localtime(&(tv.tv_sec));
     45 
     46     printf("Precise TIME:%4d-%02d-%02d %02d:%02d:%02d
    "
     47            ,pNow->tm_year + 1900,pNow->tm_mon +1 ,pNow->tm_mday,
     48            pNow->tm_hour,pNow->tm_min,pNow->tm_sec,
     49            (int)tv.tv_usec);
     50     printf("zone:offset minute=%d,dst=%d
    ",tz.tz_minuteswest,tz.tz_dsttime);
     51 
     52 
     53     //calc runing time
     54     struct timeval tv1,tv2;
     55     gettimeofday(&tv1,NULL);
     56     int i=0,sum=0;
     57     while(i++<0xfffffff)
     58     {
     59         sum += i;
     60     }
     61     gettimeofday(&tv2,NULL);
     62 
     63     int usec = tv2.tv_usec - tv1.tv_usec;
     64     int sec = tv2.tv_sec - tv1.tv_sec;
     65 
     66     if(usec<0)
     67     {
     68         usec += 1000*1000;
     69         sec--;
     70     }
     71 
     72     printf("Interval time: %d,%d
    ",sec,usec);
     73 }
     74 
     75 void fun(int sig)
     76 {
     77     printf("Receive signal:%s [%d]
    ",strsignal(sig),sig);
     78 
     79     //resume default dispose
     80     signal(SIGINT,SIG_DFL);
     81 }
     82 
     83 void TestSignal()
     84 {
     85     printf("Test signal...
    ");
     86     //capture SIGINT
     87     signal(SIGINT,fun);
     88     //capture SIGTERM
     89     signal(SIGTERM,fun);
     90     //ignore this signal
     91     signal(SIGKILL,SIG_IGN);
     92     while(1);
     93 }
     94 
     95 
     96 int main()
     97 {
     98     //TestTime();
     99     //TestMicroTime();
    100     TestSignal();
    101 
    102     return 0;
    103 }
  • 相关阅读:
    curl发送post请求,统计响应时间
    云集微店、拼多多等顽疾凸显,社交电商如何突围?
    App音频内录 录音
    nginx支持android、ios、微信扫一扫
    hadoop 2.7.1安装和配置
    Centos7上HBase的安装和配置
    HBase各版本对Hadoop版本的支持情况
    40个Java多线程问题总结
    JAVA多线程之volatile 与 synchronized 的比较
    深入解析spring中用到的九种设计模式
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8986881.html
Copyright © 2020-2023  润新知