Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1058359
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 15:58:14


点击(此处)折叠或打开

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7. #include <sys/timeb.h>
  8. #include <stdarg.h>

  9. void alarm_fun(int num);
  10. int mySleep(int sec);
  11. char * StrTrim(char * pstr);
  12. int    GetDateTime(char * cDate, char * cTime);

  13. int main(int argc, char ** argv)
  14. {
  15.     printf("**********************\n");
  16.     mySleep(5);
  17.     printf("**********************\n");
  18.     return 0;
  19. }

  20. int mySleep(int sec)
  21. {
  22.     signal(SIGALRM, alarm_fun);
  23.     alarm(sec);
  24.     pause();
  25.     return(alarm(0));
  26. }

  27. void alarm_fun(int num)
  28. {
  29.     printf("收到SIGINT信号!\n");
  30.     
  31.     char sDate[9], sTime1[10], sTime2[10];
  32.     
  33.     memset(sDate,0,sizeof(sDate));
  34.     memset(sTime1,0,sizeof(sTime1));
  35.     
  36.     GetDateTime(sDate,sTime1);
  37.     StrTrim(sDate);
  38.     StrTrim(sTime1);
  39.     
  40.     int i = -1;
  41.     int j = -1;
  42.     int m = -1;
  43.     printf("0****************************\n");
  44.     sleep(2);
  45.     printf("1****************************\n");
  46.     for(i=0; i<5000; i++)
  47.     {
  48.         for(j=0; j<1000; j++)
  49.         {
  50.             for(m=0; m<1000; m++)
  51.             {
  52.             }
  53.         }
  54.     }
  55.     
  56.     /*计算时间间隔*/
  57.     memset(sDate,0,sizeof(sDate));
  58.     memset(sTime2,0,sizeof(sTime2));
  59.     GetDateTime(sDate,sTime2);
  60.     int time_internal=sTime2[4]*10*1000+sTime2[5]*1000+sTime2[6]*100+sTime2[7]*10+sTime2[8] - sTime1[4]*10*1000-sTime1[5]*1000-sTime1[6]*100-sTime1[7]*10-sTime1[8];
  61.     printf("sTime1 = [%s]\n", sTime1);
  62.     printf("sTime2 = [%s]\n", sTime2);
  63.     if (time_internal < 0)
  64.     time_internal = time_internal + 60*1000;
  65.     printf("三层for循环耗时 = [%d]毫秒\n", time_internal);
  66.     
  67.     return;

  68. }

  69. char * StrTrim(char * pstr)
  70. {
  71.     if(NULL == pstr)
  72.     {
  73.         return NULL;
  74.     }
  75.     char * phead = pstr;
  76.     char * ptail = pstr + strlen(pstr) - 1;
  77.     while((*phead == ' ')&&(phead < ptail)) phead++;
  78.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  79.     *(ptail + 1) = '\0';
  80.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  81.     return pstr;
  82. }

  83. /***********************************************************
  84. * 函 数 名:GetDateTime()
  85. * 功能描述: 分别获取系统日期时间
  86. * 输入参数:
  87. *            cDate-日期接收缓冲区;
  88. *            cTime-日期接收缓冲区;
  89. * 输出参数:cDate
  90. * 返 回:0 成功
  91. * 流程描述:
  92. * 说明: 返回日期格式:YYYYMMDD
  93. * 修改记录:
  94. * [修改人] [日期] - [描述]
  95. ***********************************************************/
  96. int    GetDateTime(char * cDate, char * cTime)
  97. {
  98.     struct tm *    cur;
  99.     time_t    timep;

  100.     struct timeb time_msec;
  101.     unsigned short msec;

  102.     time(&timep);
  103.     cur = localtime(&timep);

  104.     ftime(&time_msec);
  105.     msec=time_msec.millitm;

  106.     sprintf(cDate,"%04d%02d%02d",cur->tm_year+1900,cur->tm_mon+1,cur->tm_mday);
  107.     cDate[8]=0;
  108.     sprintf(cTime,"%02d%02d%02d%03d",cur->tm_hour,cur->tm_min,cur->tm_sec,msec);
  109.     cTime[9]=0;

  110.     return 0;
  111. }

阅读(788) | 评论(0) | 转发(0) |
0

上一篇:pause函数

下一篇:sigprocmask函数

给主人留下些什么吧!~~