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

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 16:02:17

sigaction.c文件

点击(此处)折叠或打开

  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 fun(int sig);

  11. char * StrTrim(char * pstr);
  12. int    GetDateTime(char * cDate, char * cTime);
  13. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...);

  14. char sDebugFile[128];

  15. int main(int argc, char ** argv)
  16. {
  17.     char sDate[9], sTime1[10], sTime2[10];
  18.     
  19.     memset(sDate,0,sizeof(sDate));
  20.     memset(sTime1,0,sizeof(sTime1));
  21.     
  22.     GetDateTime(sDate,sTime1);
  23.     StrTrim(sDate);
  24.     StrTrim(sTime1);

  25.     memset(sDebugFile, 0, sizeof(sDebugFile));
  26.     sprintf(sDebugFile, "Created_%s_%s.signal", sDate, sTime1);
  27.     
  28.     struct sigaction act;
  29.     memset(&act, 0x00, sizeof(struct sigaction));
  30.     act.sa_handler=fun;
  31.     sigemptyset(&act.sa_mask);
  32.     sigaddset(&act.sa_mask, SIGALRM);
  33.     /*sigaction(SIGALRM, &act, NULL);*/
  34.     signal(SIGALRM, fun);
  35.     alarm(5);
  36.     
  37.     int i = -1;
  38.     int j = -1;
  39.     int m = -1;
  40.     SipWriteLog(_X_,"0****************************");
  41.     printf("0****************************\n");
  42.     sleep(2);
  43.     SipWriteLog(_X_,"1****************************");
  44.     printf("1****************************\n");
  45.     for(i=0; i<5000; i++)
  46.     {
  47.         for(j=0; j<1000; j++)
  48.         {
  49.             for(m=0; m<1000; m++)
  50.             {
  51.             }
  52.         }
  53.     }

  54.     /*计算时间间隔*/
  55.     memset(sDate,0,sizeof(sDate));
  56.     memset(sTime2,0,sizeof(sTime2));
  57.     GetDateTime(sDate,sTime2);
  58.     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];
  59.     printf("sTime1 = [%s]\n", sTime1);
  60.     printf("sTime2 = [%s]\n", sTime2);
  61.     SipWriteLog(_X_,"sTime1 = [%s]", sTime1);
  62.     SipWriteLog(_X_,"sTime2 = [%s]", sTime2);
  63.     if (time_internal < 0)
  64.     time_internal = time_internal + 60*1000;
  65.     printf("三层for循环耗时 = [%d]毫秒\n", time_internal);
  66.     SipWriteLog(_X_,"三层for循环耗时 = [%d]毫秒", time_internal);
  67.     

  68.     printf("程序结束****************************\n");
  69.     
  70.     return 0;
  71. }


  72. void fun(int sig)
  73. {
  74.     SipWriteLog(_X_,"收到信号!");
  75.     printf("收到信号!\n");
  76. }



  77. char * StrTrim(char * pstr)
  78. {
  79.     if(NULL == pstr)
  80.     {
  81.         return NULL;
  82.     }
  83.     char * phead = pstr;
  84.     char * ptail = pstr + strlen(pstr) - 1;
  85.     while((*phead == ' ')&&(phead < ptail)) phead++;
  86.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  87.     *(ptail + 1) = '\0';
  88.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  89.     return pstr;
  90. }

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

  108.     struct timeb time_msec;
  109.     unsigned short msec;

  110.     time(&timep);
  111.     cur = localtime(&timep);

  112.     ftime(&time_msec);
  113.     msec=time_msec.millitm;

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

  118.     return 0;
  119. }


  120. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...)
  121. {
  122.     char sdate[25];
  123.     va_list ap;
  124.     FILE *fp;
  125.     int filelen;
  126.     
  127.      fp=fopen(sDebugFile,"a");
  128.      if(fp==NULL) return;
  129.     memset(sdate,0,sizeof(sdate));
  130.     GetDateTime(sdate,sdate+9);
  131.     sdate[8]=' ';

  132.     fprintf(fp, "[%s] [%s][%d] >> ",sdate,_file_,_line_);
  133.     va_start(ap,strFmt);
  134.     vfprintf(fp, strFmt, ap);
  135.     va_end(ap);
  136.     fprintf(fp,"\n");
  137.     fflush(NULL);
  138.     fclose(fp);
  139.     return ;
  140. }
sigaction2.c文件

点击(此处)折叠或打开

  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. char * StrTrim(char * pstr);
  11. int    GetDateTime(char * cDate, char * cTime);
  12. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...);

  13. char sDebugFile[128];

  14. void show_handler(int sig)
  15. {
  16.     printf("show_handler1:收到信号SIGINT=[%d]\n", sig);
  17.     /*
  18.     int i = -1;
  19.     int j = -1;
  20.     int m = -1;
  21.     for(i=0; i<5000; i++)
  22.     {
  23.         for(j=0; j<1000; j++)
  24.         {
  25.             for(m=0; m<1000; m++)
  26.             {
  27.             }
  28.         }
  29.     }
  30.     */
  31.     printf("show_handler1:信号SIGINT=[%d]结束\n", sig);
  32. }

  33. void show_handler2(int sig)
  34. {
  35.     printf("show_handler2:收到信号SIGINT=[%d]\n", sig);
  36. }

  37. int main(void)
  38. {
  39.     char sDate[9], sTime1[10], sTime2[10];
  40.     
  41.     memset(sDate,0,sizeof(sDate));
  42.     memset(sTime1,0,sizeof(sTime1));
  43.     
  44.     GetDateTime(sDate,sTime1);
  45.     StrTrim(sDate);
  46.     StrTrim(sTime1);
  47.     
  48.     
  49.     struct sigaction act, oldact;
  50.     signal(SIGINT, show_handler);
  51.     
  52.     oldact.sa_handler = signal(SIGINT, show_handler2);
  53.     
  54.     oldact.sa_handler(1);
  55.     oldact.sa_handler(2);
  56.     
  57.     act.sa_handler = show_handler;
  58.     sigemptyset(&act.sa_mask);
  59.     sigaddset(&act.sa_mask, SIGQUIT);
  60.     /*act.sa_flags = SA_RESETHAND | SA_NODEFER;*/
  61.     act.sa_flags = SA_NODEFER;
  62.     /*act.sa_flags = 0;*/

  63.     int ret = -1;
  64.     ret = sigaction(SIGINT, &act, &oldact);
  65.     
  66.     /*signal(SIGINT, show_handler);*/
  67.     printf("**********ret = [%d]*********\n", ret);
  68.     int i = 0;
  69.     while(i < 10)
  70.     {
  71.         sleep(1);
  72.         printf("sleeping %d\n", i);
  73.         i++;
  74.         /*计算时间间隔*/
  75.         memset(sDate,0,sizeof(sDate));
  76.         memset(sTime2,0,sizeof(sTime2));
  77.         GetDateTime(sDate,sTime2);
  78.         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];
  79.         if (time_internal < 0)
  80.         time_internal = time_internal + 60*1000;
  81.         printf("循环耗时 = [%d]毫秒\n", time_internal);
  82.     }
  83.     
  84.     printf("执行show_handler2函数!\n");
  85.     show_handler2(1);
  86.     oldact.sa_handler(1);
  87.     printf("act.sa_mask = [%d]\n", act.sa_mask);
  88.     printf("act.sa_flags = [%d]\n", act.sa_flags);
  89.     printf("oldact.sa_mask = [%d]\n", oldact.sa_mask);
  90.     printf("oldact.sa_flags = [%d]\n", oldact.sa_flags);
  91.     signal(SIGINT, oldact.sa_handler);
  92.     i = 0;
  93.     while(i < 10)
  94.     {
  95.         sleep(1);
  96.         printf("sleeping %d\n", i);
  97.         i++;
  98.         /*计算时间间隔*/
  99.         memset(sDate,0,sizeof(sDate));
  100.         memset(sTime2,0,sizeof(sTime2));
  101.         GetDateTime(sDate,sTime2);
  102.         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];
  103.         if (time_internal < 0)
  104.         time_internal = time_internal + 60*1000;
  105.         printf("循环耗时 = [%d]毫秒\n", time_internal);
  106.     }
  107.     printf("执行show_handler2函数完毕!\n");
  108. }

  109. char * StrTrim(char * pstr)
  110. {
  111.     if(NULL == pstr)
  112.     {
  113.         return NULL;
  114.     }
  115.     char * phead = pstr;
  116.     char * ptail = pstr + strlen(pstr) - 1;
  117.     while((*phead == ' ')&&(phead < ptail)) phead++;
  118.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  119.     *(ptail + 1) = '\0';
  120.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  121.     return pstr;
  122. }

  123. /***********************************************************
  124. * 函 数 名:GetDateTime()
  125. * 功能描述: 分别获取系统日期时间
  126. * 输入参数:
  127. *            cDate-日期接收缓冲区;
  128. *            cTime-日期接收缓冲区;
  129. * 输出参数:cDate
  130. * 返 回:0 成功
  131. * 流程描述:
  132. * 说明: 返回日期格式:YYYYMMDD
  133. * 修改记录:
  134. * [修改人] [日期] - [描述]
  135. ***********************************************************/
  136. int    GetDateTime(char * cDate, char * cTime)
  137. {
  138.     struct tm *    cur;
  139.     time_t    timep;

  140.     struct timeb time_msec;
  141.     unsigned short msec;

  142.     time(&timep);
  143.     cur = localtime(&timep);

  144.     ftime(&time_msec);
  145.     msec=time_msec.millitm;

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

  150.     return 0;
  151. }

  152. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...)
  153. {
  154.     char sdate[25];
  155.     va_list ap;
  156.     FILE *fp;
  157.     int filelen;
  158.     
  159.      fp=fopen(sDebugFile,"a");
  160.      if(fp==NULL) return;
  161.     memset(sdate,0,sizeof(sdate));
  162.     GetDateTime(sdate,sdate+9);
  163.     sdate[8]=' ';

  164.     fprintf(fp, "[%s] [%s][%d] >> ",sdate,_file_,_line_);
  165.     va_start(ap,strFmt);
  166.     vfprintf(fp, strFmt, ap);
  167.     va_end(ap);
  168.     fprintf(fp,"\n");
  169.     fflush(NULL);
  170.     fclose(fp);
  171.     return ;
  172. }
sigaction2.c文件

点击(此处)折叠或打开

  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. char * StrTrim(char * pstr);
  11. int    GetDateTime(char * cDate, char * cTime);
  12. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...);

  13. char sDebugFile[128];

  14. void show_handler(int sig)
  15. {
  16.     printf("收到信号SIGINT=[%d]\n", sig);
  17.     int i = -1;
  18.     int j = -1;
  19.     int m = -1;
  20.     for(i=0; i<5000; i++)
  21.     {
  22.         for(j=0; j<1000; j++)
  23.         {
  24.             for(m=0; m<1000; m++)
  25.             {
  26.             }
  27.         }
  28.     }
  29.     printf("信号SIGINT=[%d]结束\n", sig);
  30. }

  31. void show_handler2(int sig)
  32. {
  33.     printf("show_handler2:收到信号SIGINT=[%d]\n", sig);
  34. }

  35. typedef void sighandler(int);
  36. sighandler * mySignal(int signum, sighandler * handler)
  37. {
  38.     printf("执行mySignal函数!\n");
  39.     struct sigaction act, oldact;
  40.     act.sa_handler = handler;
  41.     sigemptyset(&act.sa_mask);
  42.     act.sa_flags=0;
  43.     if(signum == SIGALARM)
  44.     {
  45.         #ifdef SA_INTERRUPT
  46.             act.sa_flags |= SA_INTERRUPT;
  47.         #endif
  48.     }
  49.     else
  50.     {
  51.         #ifdef SA_RESTART
  52.             act.sa_flags |= SA_RESTART;
  53.         #endif
  54.     }
  55.         
  56.     if(sigaction(signum, &act, &oldact) < 0);
  57.     {
  58.         return(SIG_ERR);
  59.     }
  60.     return(oldact.sa_handler);
  61. }

  62. int main(void)
  63. {
  64.     char sDate[9], sTime1[10], sTime2[10];
  65.     
  66.     memset(sDate,0,sizeof(sDate));
  67.     memset(sTime1,0,sizeof(sTime1));
  68.     
  69.     GetDateTime(sDate,sTime1);
  70.     StrTrim(sDate);
  71.     StrTrim(sTime1);
  72.     
  73.     mySignal(SIGINT, show_handler);
  74.     
  75.     int i = 0;
  76.     while(i < 10)
  77.     {
  78.         sleep(1);
  79.         printf("sleeping %d\n", i);
  80.         i++;
  81.         /*计算时间间隔*/
  82.         memset(sDate,0,sizeof(sDate));
  83.         memset(sTime2,0,sizeof(sTime2));
  84.         GetDateTime(sDate,sTime2);
  85.         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];
  86.         if (time_internal < 0)
  87.         time_internal = time_internal + 60*1000;
  88.         printf("循环耗时 = [%d]毫秒\n", time_internal);
  89.     }
  90.     
  91.     struct sigaction act, oldact;
  92.     act.sa_handler = show_handler;
  93.     sigemptyset(&act.sa_mask);
  94.     sigaddset(&act.sa_mask, SIGQUIT);
  95.     /*act.sa_flags = SA_RESETHAND | SA_NODEFER;*/
  96.     act.sa_flags = SA_NODEFER;
  97.     /*act.sa_flags = 0;*/

  98.     int ret = -1;
  99.     ret = sigaction(SIGINT, &act, &oldact);
  100.     while(i < 10)
  101.     {
  102.         sleep(1);
  103.         printf("sleeping %d\n", i);
  104.         i++;
  105.         /*计算时间间隔*/
  106.         memset(sDate,0,sizeof(sDate));
  107.         memset(sTime2,0,sizeof(sTime2));
  108.         GetDateTime(sDate,sTime2);
  109.         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];
  110.         if (time_internal < 0)
  111.         time_internal = time_internal + 60*1000;
  112.         printf("循环耗时 = [%d]毫秒\n", time_internal);
  113.     }
  114.     
  115.     /*signal(SIGINT, show_handler);*/
  116.     printf("**********ret = [%d]*********\n", ret);
  117.     
  118.     printf("执行show_handler2函数!\n");
  119.     show_handler2(1);
  120.     oldact.sa_handler(1);
  121.     printf("act.sa_mask = [%d]\n", act.sa_mask);
  122.     printf("act.sa_flags = [%d]\n", act.sa_flags);
  123.     printf("oldact.sa_mask = [%d]\n", oldact.sa_mask);
  124.     printf("oldact.sa_flags = [%d]\n", oldact.sa_flags);
  125.     signal(SIGINT, oldact.sa_handler);
  126.     i = 0;
  127.     while(i < 10)
  128.     {
  129.         sleep(1);
  130.         printf("sleeping %d\n", i);
  131.         i++;
  132.         /*计算时间间隔*/
  133.         memset(sDate,0,sizeof(sDate));
  134.         memset(sTime2,0,sizeof(sTime2));
  135.         GetDateTime(sDate,sTime2);
  136.         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];
  137.         if (time_internal < 0)
  138.         time_internal = time_internal + 60*1000;
  139.         printf("循环耗时 = [%d]毫秒\n", time_internal);
  140.     }
  141.     printf("执行show_handler2函数完毕!\n");
  142. }

  143. char * StrTrim(char * pstr)
  144. {
  145.     if(NULL == pstr)
  146.     {
  147.         return NULL;
  148.     }
  149.     char * phead = pstr;
  150.     char * ptail = pstr + strlen(pstr) - 1;
  151.     while((*phead == ' ')&&(phead < ptail)) phead++;
  152.     while((*ptail == ' ')&&(ptail > phead)) ptail--;
  153.     *(ptail + 1) = '\0';
  154.     memcpy(pstr, phead, sizeof(char)*(ptail - phead + 2));
  155.     return pstr;
  156. }

  157. /***********************************************************
  158. * 函 数 名:GetDateTime()
  159. * 功能描述: 分别获取系统日期时间
  160. * 输入参数:
  161. *            cDate-日期接收缓冲区;
  162. *            cTime-日期接收缓冲区;
  163. * 输出参数:cDate
  164. * 返 回:0 成功
  165. * 流程描述:
  166. * 说明: 返回日期格式:YYYYMMDD
  167. * 修改记录:
  168. * [修改人] [日期] - [描述]
  169. ***********************************************************/
  170. int    GetDateTime(char * cDate, char * cTime)
  171. {
  172.     struct tm *    cur;
  173.     time_t    timep;

  174.     struct timeb time_msec;
  175.     unsigned short msec;

  176.     time(&timep);
  177.     cur = localtime(&timep);

  178.     ftime(&time_msec);
  179.     msec=time_msec.millitm;

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

  184.     return 0;
  185. }

  186. void SipWriteLog(const long _line_,const char *_file_,char flag,const char *strFmt, ...)
  187. {
  188.     char sdate[25];
  189.     va_list ap;
  190.     FILE *fp;
  191.     int filelen;
  192.     
  193.      fp=fopen(sDebugFile,"a");
  194.      if(fp==NULL) return;
  195.     memset(sdate,0,sizeof(sdate));
  196.     GetDateTime(sdate,sdate+9);
  197.     sdate[8]=' ';

  198.     fprintf(fp, "[%s] [%s][%d] >> ",sdate,_file_,_line_);
  199.     va_start(ap,strFmt);
  200.     vfprintf(fp, strFmt, ap);
  201.     va_end(ap);
  202.     fprintf(fp,"\n");
  203.     fflush(NULL);
  204.     fclose(fp);
  205.     return ;
  206. }



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

上一篇:sigprocmask函数

下一篇:C中bool类型变量

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