1. 休眠sleep(unsigned int)为线程内操作 
  所以如果不同线程,信号量SIGALRM是不能中断sleep(); 
  编写程序进行测试

  1. //timercreate_demo.cpp
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <time.h>
  6. #include <pthread.h>
  7.  
  8. void SignHandler(int iSignNo);
  9. void testTimerSign();
  10. void printTime();
  11. void *function(void *arg);
  12.  
  13. int main() {
  14. pthread_t thread1;
  15. pthread_create(&thread1,NULL,function,(char*)"");
  16. testTimerSign();
  17. while(true);
  18. return ;
  19. }
  20.  
  21. void SignHandler(int iSignNo){
  22. if(iSignNo == SIGUSR1){
  23. printf("Capture sign no : SIGUSR1\n");
  24. }else if(SIGALRM == iSignNo){
  25. //printf("Capture sign no : SIGALRM\n");
  26. }else{
  27. printf("Capture sign no:%d\n",iSignNo);
  28. }
  29. }
  30.  
  31. void testTimerSign(){
  32. struct sigevent evp;
  33. struct itimerspec ts;
  34. timer_t timer;
  35. int ret;
  36. evp.sigev_value.sival_ptr = &timer;
  37. evp.sigev_notify = SIGEV_SIGNAL;
  38. evp.sigev_signo = SIGALRM;
  39. signal(evp.sigev_signo, SignHandler);
  40. ret = timer_create(CLOCK_REALTIME, &evp, &timer);
  41. if(ret) {
  42. perror("timer_create");
  43. }
  44. ts.it_interval.tv_sec = ;
  45. ts.it_interval.tv_nsec = ;
  46. ts.it_value.tv_sec = ;
  47. ts.it_value.tv_nsec = ;
  48. printTime();
  49. printf("start\n");
  50. ret = timer_settime(timer, , &ts, NULL);
  51. if(ret) {
  52. perror("timer_settime");
  53. }
  54. }
  55.  
  56. void printTime(){
  57. struct tm *cursystem;
  58. time_t tm_t;
  59. time(&tm_t);
  60. cursystem = localtime(&tm_t);
  61. char tszInfo[] ;
  62. sprintf(tszInfo, "%02d:%02d:%02d",
  63. cursystem->tm_hour,
  64. cursystem->tm_min,
  65. cursystem->tm_sec);
  66. printf("[%s]",tszInfo);
  67. }
  68.  
  69. void *function(void *arg){
  70. char *m;
  71. m = (char *)arg;
  72. while(true) {
  73. while(true){
  74. int left = sleep();
  75. printTime();
  76. printf("sleep(3)(left=%d)\n", left);
  77. }
  78. }
  79. }

 
可以看出,在主线程的定时器中的信号量SIGALRM是无法中断子线程thread1的休眠;

在同一线程中, sleep()函数会被SIGALARM信号中断

使用SIGALRM信号量定时

上面程序中使用了信号量SIGUSR1; 
如果使用信号量SIGALRM; 
(对 CLOCK_REALTIMER来说,默认信号就是SIGALRM) 
sleep()函数使用的就是实时时钟CLOCK_REALTIMER 
所以使用信号值SIGALRM会中断sleep(int second)函数的休眠;

  1. //timercreate_demo.cpp
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <time.h>
  6.  
  7. void SignHandler(int iSignNo);
  8. void testTimerSign();
  9. void printTime();
  10.  
  11. int main() {
  12. testTimerSign();
  13. while(true){
  14. int left = sleep();
  15. printTime();
  16. printf("sleep(5)(left=%d)\n", left);
  17. }
  18. return ;
  19. }
  20.  
  21. void SignHandler(int iSignNo){
  22. //printTime();
  23. if(iSignNo == SIGUSR1){
  24. printf("Capture sign no : SIGUSR1\n");
  25. }else if(SIGALRM == iSignNo){
  26. //printf("Capture sign no : SIGALRM\n");
  27. }else{
  28. printf("Capture sign no:%d\n",iSignNo);
  29. }
  30. }
  31.  
  32. void testTimerSign(){
  33. struct sigevent evp;
  34. struct itimerspec ts;
  35. timer_t timer;
  36. int ret;
  37. evp.sigev_value.sival_ptr = &timer;
  38. evp.sigev_notify = SIGEV_SIGNAL;
  39. evp.sigev_signo = SIGALRM;
  40. signal(evp.sigev_signo, SignHandler);
  41. ret = timer_create(CLOCK_REALTIME, &evp, &timer);
  42. if(ret) {
  43. perror("timer_create");
  44. }
  45. ts.it_interval.tv_sec = ;
  46. ts.it_interval.tv_nsec = ;
  47. ts.it_value.tv_sec = ;
  48. ts.it_value.tv_nsec = ;
  49. printTime();
  50. printf("start\n");
  51. ret = timer_settime(timer, , &ts, NULL);
  52. if(ret) {
  53. perror("timer_settime");
  54. }
  55. }
  56.  
  57. void printTime(){
  58. struct tm *cursystem;
  59. time_t tm_t;
  60. time(&tm_t);
  61. cursystem = localtime(&tm_t);
  62. char tszInfo[] ;
  63. sprintf(tszInfo, "%02d:%02d:%02d",
  64. cursystem->tm_hour,
  65. cursystem->tm_min,
  66. cursystem->tm_sec);
  67. printf("[%s]",tszInfo);
  68. }

 
因为timer_settime()中定时器间隔时间为1秒 
于是sleep(5)每次都被打断不能按时休眠,剩余4秒未能执行;

Linux下 sleep函数的注意事项的更多相关文章

  1. linux select函数:Linux下select函数的使用详解【转】

    本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...

  2. linux下syscall函数,SYS_gettid,SYS_tgkill

    出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html     linux下syscall函数,SYS_gettid,SYS_tgkill  ...

  3. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  4. Linux下c函数dlopen实现加载动态库so文件代码举例

    dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...

  5. 转:对于linux下system()函数的深度理解(整理)

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  6. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  7. [转帖]Linux下fork函数及pthread函数的总结

    Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...

  8. (笔记)Linux下system()函数的深度理解(整理)

    注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...

  9. linux下sprintf_s函数的替代

    error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...

随机推荐

  1. Eclipse寻找JVM(JRE)的顺序机制

    http://developer.51cto.com/art/200907/135271.htm Eclipse也是一个普通的Java程序,因此必须有一个JRE做为运行环境.本文将简单谈谈Eclips ...

  2. The Struts dispatcher cannot be found. This is usually caused by using Struts ta

    HTTP Status 500 - type Exception report message description The server encountered an internal error ...

  3. 全面的framebuffer详解二

    转:http://blog.chinaunix.net/uid-28297667-id-3773729.html (二)一个LCD显示芯片的驱动实例 以Skeleton LCD 控制器驱动为例,在LI ...

  4. 【docker】【redis】1.docker安装redis【单点redis服务】

    1.首先确定 需要在docker上拉取redis的哪个版本的镜像 [由于使用了aliyun的源,并且加速器也是用的阿里云的加速器,所以直接在阿里云开发者平台上找redis的镜像有哪些版本是再好不过了, ...

  5. python接口自动化23-token参数关联登录(登录拉勾网)

    前言 登录网站的时候,经常会遇到传token参数,token关联并不难,难的是找出服务器第一次返回token的值所在的位置,取出来后就可以动态关联了 登录拉勾网 1.先找到登录首页https://pa ...

  6. Alert Messager

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. iOS中 imageNamed方法 非常多图片占用大量内存问题

    当我们须要载入非常多图片(相冊)的时候我们通常会用[UIimage  imageNamed:imageName]; 实际上[UIimage  imageNamed:imageName]这种方法在图片使 ...

  8. Linux进程间通信—套接字

    六.套接字(socket) socket也是一种进程间的通信机制,不过它与其他通信方式主要的区别是:它可以实现不同主机间的进程通信.一个套接口可以看做是进程间通信的端点(endpoint),每个套接口 ...

  9. BeagleBone折腾记(一):连接你的狗板

    BeagleBone折腾记一连接你的狗板 准备 了解BeagleBone BeagleBone社区 所需软硬件 USB连接 TTL连接 结语 准备 了解BeagleBone BeagleBone可能一 ...

  10. Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space

    在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...