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

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 15:52:55


点击(此处)折叠或打开

  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. #define _X_ __LINE__,__FILE__,'1'

  10. void HUP_FUN(int sig);
  11. void INT_FUN(int sig);
  12. void QUIT_FUN(int sig);
  13. void ALRM_FUN(int sig);
  14. void TERM_FUN(int sig);
  15. void TSTP_FUN(int sig);
  16. void TTIN_FUN(int sig);

  17. char * StrTrim(char * pstr);
  18. int    GetDateTime(char * cDate, char * cTime);
  19. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...);

  20. char sDebugFile[128];

  21. int main(int argc, char ** argv)
  22. {
  23.     char sDate[9], sTime1[10], sTime2[10];
  24.     
  25.     memset(sDate,0,sizeof(sDate));
  26.     memset(sTime1,0,sizeof(sTime1));
  27.     
  28.     GetDateTime(sDate,sTime1);
  29.     StrTrim(sDate);
  30.     StrTrim(sTime1);

  31.     memset(sDebugFile, 0, sizeof(sDebugFile));
  32.     sprintf(sDebugFile, "Created_%s_%s.signal", sDate, sTime1);

  33.     /*signal(SIGHUP, SIG_IGN);*/ /*忽略终端关闭 1号SIGINT信号*/
  34.     signal(SIGHUP, HUP_FUN);
  35.     signal(SIGTSTP, SIG_IGN); /*忽略ctrl + z 20号SIGINT信号*/
  36.     /*signal(SIGTSTP, TSTP_FUN);*/    
  37.     /*signal(SIGINT, SIG_IGN);*/     /*忽略ctrl + c 2号SIGINT信号*/
  38.     signal(SIGINT, INT_FUN);
  39.     signal(SIGQUIT, SIG_IGN); /*忽略ctrl + \ 3号SIGQUIT信号*/
  40.     /*signal(SIGQUIT, QUIT_FUN);*/
  41.     signal(SIGSTOP, SIG_IGN);     /*19号信号不能忽略*/
  42.     /*signal(SIGTERM, SIG_IGN);*/    
  43.     signal(SIGTERM, TERM_FUN);
  44.     signal(SIGALRM, ALRM_FUN);    
  45.     /*signal(SIGCHLD, SIG_IGN);*/
  46.     signal(SIGTTIN, TTIN_FUN);
  47.     
  48.     int timeout = 10;
  49.     int timeRet = -1;
  50.     timeRet = alarm(0); /*清除当前进程的alarm设置*/
  51.     timeRet = alarm(20); /*删除旧的alarm设置,重新设置新的定时时间,并返回旧的alarm的剩余时间*/
  52.     
  53.     int i = -1;
  54.     int j = -1;
  55.     int m = -1;
  56.     SipWriteLog(_X_,"0****************************");
  57.     printf("0****************************\n");
  58.     sleep(2);
  59.     SipWriteLog(_X_,"1****************************");
  60.     printf("1****************************\n");
  61.     for(i=0; i<5000; i++)
  62.     {
  63.         for(j=0; j<1000; j++)
  64.         {
  65.             for(m=0; m<1000; m++)
  66.             {
  67.             }
  68.         }
  69.     }
  70.     /*计算时间间隔*/
  71.     memset(sDate,0,sizeof(sDate));
  72.     memset(sTime2,0,sizeof(sTime2));
  73.     GetDateTime(sDate,sTime2);
  74.     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];
  75.     printf("sTime1 = [%s]\n", sTime1);
  76.     printf("sTime2 = [%s]\n", sTime2);
  77.     SipWriteLog(_X_,"sTime1 = [%s]", sTime1);
  78.     SipWriteLog(_X_,"sTime2 = [%s]", sTime2);
  79.     if (time_internal < 0)
  80.     time_internal = time_internal + 60*1000;
  81.     printf("三层for循环耗时 = [%d]毫秒\n", time_internal);
  82.     SipWriteLog(_X_,"三层for循环耗时 = [%d]毫秒", time_internal);
  83.     
  84.     /*sleep(10);*/
  85.     SipWriteLog(_X_,"2****************************");
  86.     printf("2****************************\n");
  87.     printf("请输入num值:");
  88.     fflush(stdout);
  89.     int num = -1;
  90.     scanf("%d", &num);
  91.     printf("num = [%d]\n", num);
  92.     printf("3****************************\n");
  93.     int ret = -1;
  94.     ret = sleep(2);
  95.     printf("ret = [%d]\n", ret);
  96.     SipWriteLog(_X_,"3****************************");
  97.     printf("3****************************\n");
  98.     /*exit(0);*/
  99.     ret = sleep(1);
  100.     printf("ret = [%d]\n", ret);
  101.     SipWriteLog(_X_,"4****************************");
  102.     printf("4****************************\n");
  103.     ret = sleep(3);
  104.     printf("ret = [%d]\n", ret);
  105.     SipWriteLog(_X_,"5****************************");
  106.     printf("5****************************\n");
  107.     ret = sleep(4);
  108.     printf("ret = [%d]\n", ret);
  109.     SipWriteLog(_X_,"6****************************");
  110.     printf("6****************************\n");
  111.     ret = sleep(5);
  112.     printf("ret = [%d]\n", ret);
  113.     SipWriteLog(_X_,"7****************************");
  114.     printf("7****************************\n");
  115.     ret = sleep(6);
  116.     printf("ret = [%d]\n", ret);
  117.     SipWriteLog(_X_,"8****************************");
  118.     printf("8****************************\n");
  119.     ret = sleep(7);
  120.     printf("ret = [%d]\n", ret);
  121.     SipWriteLog(_X_,"9****************************");
  122.     printf("9****************************\n");
  123.     ret = sleep(8);
  124.     printf("ret = [%d]\n", ret);
  125.     SipWriteLog(_X_,"10****************************");
  126.     printf("10****************************\n");
  127.     ret = sleep(1);
  128.     printf("ret = [%d]\n", ret);
  129.     printf("程序结束****************************\n");
  130.     
  131.     /*alarm(0);*/
  132.     return 0;
  133. }

  134. void HUP_FUN(int sig)
  135. {
  136.     SipWriteLog(_X_,"收到 1号SIGHUP信号!");
  137.     printf("收到 1号SIGHUP信号!\n");
  138.     int i = -1;
  139.     int j = -1;
  140.     int m = -1;
  141.     for(i=0; i<5000; i++)
  142.     {
  143.         for(j=0; j<1000; j++)
  144.         {
  145.             for(m=0; m<1000; m++)
  146.             {
  147.             }
  148.         }
  149.     }
  150. }

  151. void INT_FUN(int sig)
  152. {
  153.     SipWriteLog(_X_,"收到ctrl + c 1号SIGHUP信号!");
  154.     printf("收到ctrl + c 1号SIGHUP信号!\n");
  155.     /*
  156.     int i = -1;
  157.     int j = -1;
  158.     int m = -1;
  159.     for(i=0; i<5000; i++)
  160.     {
  161.         for(j=0; j<1000; j++)
  162.         {
  163.             for(m=0; m<1000; m++)
  164.             {
  165.             }
  166.         }
  167.     }
  168.     */
  169. }


  170. void QUIT_FUN(int sig)
  171. {
  172.     SipWriteLog(_X_,"收到ctrl + \\ 3号SIGQUIT信号!");
  173.     printf("收到ctrl + \\ 3号SIGQUIT信号!\n");
  174.     int i = -1;
  175.     int j = -1;
  176.     int m = -1;
  177.     for(i=0; i<5000; i++)
  178.     {
  179.         for(j=0; j<1000; j++)
  180.         {
  181.             for(m=0; m<1000; m++)
  182.             {
  183.             }
  184.         }
  185.     }
  186. }


  187. void ALRM_FUN(int sig)
  188. {
  189.     SipWriteLog(_X_,"收到时钟信号 14号SIGALRM信号!");
  190.     printf("收到时钟信号 14号SIGALRM信号!\n");
  191.     int i = -1;
  192.     int j = -1;
  193.     int m = -1;
  194.     for(i=0; i<500; i++)
  195.     {
  196.         for(j=0; j<1000; j++)
  197.         {
  198.             for(m=0; m<1000; m++)
  199.             {
  200.             }
  201.         }
  202.     }
  203. }


  204. void TERM_FUN(int sig)
  205. {
  206.     SipWriteLog(_X_,"收到15号SIGTERM信号!");
  207.     printf("收到15号SIGTERM信号!\n");
  208.     int i = -1;
  209.     int j = -1;
  210.     int m = -1;
  211.     for(i=0; i<5000; i++)
  212.     {
  213.         for(j=0; j<1000; j++)
  214.         {
  215.             for(m=0; m<1000; m++)
  216.             {
  217.             }
  218.         }
  219.     }
  220. }


  221. void TSTP_FUN(int sig)
  222. {
  223.     SipWriteLog(_X_,"收到ctrl + z 20号SIGTSTP信号!");
  224.     printf("收到ctrl + z 20号SIGTSTP信号!\n");
  225.     int i = -1;
  226.     int j = -1;
  227.     int m = -1;
  228.     for(i=0; i<5000; i++)
  229.     {
  230.         for(j=0; j<1000; j++)
  231.         {
  232.             for(m=0; m<1000; m++)
  233.             {
  234.             }
  235.         }
  236.     }
  237. }


  238. void TTIN_FUN(int sig)
  239. {
  240.     SipWriteLog(_X_,"收到21号SIGTINT信号!");
  241.     printf("收到21号SIGTINT信号!\n");
  242.     int i = -1;
  243.     int j = -1;
  244.     int m = -1;
  245.     for(i=0; i<5; i++)
  246.     {
  247.         for(j=0; j<1; j++)
  248.         {
  249.             for(m=0; m<1; m++)
  250.             {
  251.             }
  252.         }
  253.     }
  254. }


  255. /***********************************************************
  256. * 函 数 名:pro_sig()
  257. * 功能描述: 信号处理
  258. * 输入参数:
  259. * 输出参数:
  260. * 返 回:
  261. * 流程描述:
  262. * 说明:
  263. * 修改记录:
  264. * [修改人] [日期] - [描述]
  265. ***********************************************************/
  266. /*
  267. void    pro_sig(int    sig)            
  268. {
  269.     switch (sig)
  270.     {
  271.         case SIGINT:            
  272.         case SIGQUIT:            
  273.         case SIGTERM:            
  274.             signal(SIGINT, SIG_IGN);
  275.             signal(SIGQUIT, SIG_IGN);
  276.             signal(SIGTERM, SIG_IGN);
  277.             printf("进程退出!\n");
  278.             exit(-1);
  279.     }
  280. }
  281. */

  282. char * StrTrim(char * pstr)
  283. {
  284.     if(NULL == pstr)
  285.     {
  286.         return NULL;
  287.     }
  288.     char * phead = pstr;
  289.     char * ptail = pstr + strlen(pstr) - 1;
  290.     while((*phead == ' ')&&(phead < ptail)) phead++;
  291.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  292.     *(ptail + 1) = '\0';
  293.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  294.     return pstr;
  295. }

  296. /***********************************************************
  297. * 函 数 名:GetDateTime()
  298. * 功能描述: 分别获取系统日期时间
  299. * 输入参数:
  300. *            cDate-日期接收缓冲区;
  301. *            cTime-日期接收缓冲区;
  302. * 输出参数:cDate
  303. * 返 回:0 成功
  304. * 流程描述:
  305. * 说明: 返回日期格式:YYYYMMDD
  306. * 修改记录:
  307. * [修改人] [日期] - [描述]
  308. ***********************************************************/
  309. int    GetDateTime(char * cDate, char * cTime)
  310. {
  311.     struct tm *    cur;
  312.     time_t    timep;

  313.     struct timeb time_msec;
  314.     unsigned short msec;

  315.     time(&timep);
  316.     cur = localtime(&timep);

  317.     ftime(&time_msec);
  318.     msec=time_msec.millitm;

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

  323.     return 0;
  324. }


  325. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...)
  326. {
  327.     char sdate[25];
  328.     va_list ap;
  329.     FILE *fp;
  330.     int filelen;
  331.     
  332.      fp=fopen(sDebugFile,"a");
  333.      if(fp==NULL) return;
  334.     memset(sdate,0,sizeof(sdate));
  335.     GetDateTime(sdate,sdate+9);
  336.     sdate[8]=' ';

  337.     fprintf(fp, "[%s] [%s][%d] >> ",sdate,_file_,_line_);
  338.     va_start(ap,strFmt);
  339.     vfprintf(fp, strFmt, ap);
  340.     va_end(ap);
  341.     fprintf(fp,"\n");
  342.     fflush(NULL);
  343.     fclose(fp);
  344.     return ;
  345. }

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

上一篇:最简单的CS架构代码

下一篇:sigsetjmp函数

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