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

//timercreate_demo.cpp
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <pthread.h> void SignHandler(int iSignNo);
void testTimerSign();
void printTime();
void *function(void *arg); int main() {
pthread_t thread1;
pthread_create(&thread1,NULL,function,(char*)"");
testTimerSign();
while(true);
return ;
} void SignHandler(int iSignNo){
if(iSignNo == SIGUSR1){
printf("Capture sign no : SIGUSR1\n");
}else if(SIGALRM == iSignNo){
//printf("Capture sign no : SIGALRM\n");
}else{
printf("Capture sign no:%d\n",iSignNo);
}
} void testTimerSign(){
struct sigevent evp;
struct itimerspec ts;
timer_t timer;
int ret;
evp.sigev_value.sival_ptr = &timer;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGALRM;
signal(evp.sigev_signo, SignHandler);
ret = timer_create(CLOCK_REALTIME, &evp, &timer);
if(ret) {
perror("timer_create");
}
ts.it_interval.tv_sec = ;
ts.it_interval.tv_nsec = ;
ts.it_value.tv_sec = ;
ts.it_value.tv_nsec = ;
printTime();
printf("start\n");
ret = timer_settime(timer, , &ts, NULL);
if(ret) {
perror("timer_settime");
}
} void printTime(){
struct tm *cursystem;
time_t tm_t;
time(&tm_t);
cursystem = localtime(&tm_t);
char tszInfo[] ;
sprintf(tszInfo, "%02d:%02d:%02d",
cursystem->tm_hour,
cursystem->tm_min,
cursystem->tm_sec);
printf("[%s]",tszInfo);
} void *function(void *arg){
char *m;
m = (char *)arg;
while(true) {
while(true){
int left = sleep();
printTime();
printf("sleep(3)(left=%d)\n", left);
}
}
}

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

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

使用SIGALRM信号量定时

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

//timercreate_demo.cpp
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h> void SignHandler(int iSignNo);
void testTimerSign();
void printTime(); int main() {
testTimerSign();
while(true){
int left = sleep();
printTime();
printf("sleep(5)(left=%d)\n", left);
}
return ;
} void SignHandler(int iSignNo){
//printTime();
if(iSignNo == SIGUSR1){
printf("Capture sign no : SIGUSR1\n");
}else if(SIGALRM == iSignNo){
//printf("Capture sign no : SIGALRM\n");
}else{
printf("Capture sign no:%d\n",iSignNo);
}
} void testTimerSign(){
struct sigevent evp;
struct itimerspec ts;
timer_t timer;
int ret;
evp.sigev_value.sival_ptr = &timer;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGALRM;
signal(evp.sigev_signo, SignHandler);
ret = timer_create(CLOCK_REALTIME, &evp, &timer);
if(ret) {
perror("timer_create");
}
ts.it_interval.tv_sec = ;
ts.it_interval.tv_nsec = ;
ts.it_value.tv_sec = ;
ts.it_value.tv_nsec = ;
printTime();
printf("start\n");
ret = timer_settime(timer, , &ts, NULL);
if(ret) {
perror("timer_settime");
}
} void printTime(){
struct tm *cursystem;
time_t tm_t;
time(&tm_t);
cursystem = localtime(&tm_t);
char tszInfo[] ;
sprintf(tszInfo, "%02d:%02d:%02d",
cursystem->tm_hour,
cursystem->tm_min,
cursystem->tm_sec);
printf("[%s]",tszInfo);
}

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

最新文章

  1. Android 之 ProgressDialog用法介绍
  2. 6个值得推荐的Android开源框架简介(转)
  3. 50 个最棒的 jQuery 日历插件,很齐全了!(转)
  4. ***iOS 项目的目录结构能看出你的开发经验
  5. 《C#并行编程高级教程》第7章 VS2010任务调试 笔记
  6. Windows10 删除已经保存的WIFI热点
  7. Thread Dump 和Java应用诊断(转)
  8. OCP-1Z0-051-名称解析-文章32称号
  9. C++ count_if/erase/remove_if 用法详解
  10. AbstractHandlerMapping解读
  11. 并发编程(五)——AbstractQueuedSynchronizer 之 ReentrantLock源码分析
  12. 如何正确使用Espresso来测试你的Android程序
  13. Easyui datagrid 数据表格 表格列头右键菜单选择展示列 JS
  14. PHP保留两位小数并且四舍五入及不四舍五入的方法
  15. 如何避免form提交进行页面跳转
  16. P2837晚餐队列安排
  17. hdu 3397 线段树
  18. python3【基础】-文件操作
  19. droppable放置组件
  20. Reveal CocoaPods的使用

热门文章

  1. MC34063组成DC-DC电路
  2. ida sdk add_struc_member array
  3. CMSIS-SVD Schema File Ver. 1.0
  4. 模板方法在Spring事务中的应用
  5. Unable to load DLL &#39;opencv_core290&#39;
  6. shiro+redis实现session共享
  7. iOS:切换视图时,反向传递数据方法二:代理
  8. DedeCms V5.6 plus/advancedsearch.php 任意sql语句执行漏洞
  9. GPGPU OpenCL Reduction操作与group同步
  10. [置顶] OpenJDK源码研究笔记(九)-可恨却又可亲的的异常(NullPointerException)