Linux下 sleep函数的注意事项
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秒未能执行;
Linux下 sleep函数的注意事项的更多相关文章
- linux select函数:Linux下select函数的使用详解【转】
本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...
- linux下syscall函数,SYS_gettid,SYS_tgkill
出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html linux下syscall函数,SYS_gettid,SYS_tgkill ...
- 对于linux下system()函数的深度理解(整理)
原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...
- Linux下c函数dlopen实现加载动态库so文件代码举例
dlopen()是一个强大的库函数.该函数将打开一个新库,并把它装入内存.该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的.这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了. ...
- 转:对于linux下system()函数的深度理解(整理)
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...
- 【C/C++】Linux下system()函数引发的错误
http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食 恋恋美食 发布时间: 2012/04/21 11:3 ...
- [转帖]Linux下fork函数及pthread函数的总结
Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...
- (笔记)Linux下system()函数的深度理解(整理)
注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...
- linux下sprintf_s函数的替代
error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...
随机推荐
- Spring依赖检查
在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...
- jquery.lazyload.js实现图片懒载入
个人理解:将须要延迟载入的图片的src属性所有设置为一张同样尽可能小(目的是尽可能的少占宽带,节省流量,因为缓存机制,当浏览器载入了一张图片之后,同样的图片就会在缓存中拿.不会又一次到server上拿 ...
- Google Chrome 39.0.2171.71 正式发布
Google Chrome,又称Google浏览器,是一个由Google(谷歌)公司开发的网页浏览器.该浏览器是基于其他开源软件所撰写,包括WebKit,目标是提升稳定性.速度和安全性,并创造出简单且 ...
- 【spring cloud】分布式ID,雪花算法
分布式ID生成服务 参考地址:https://blog.csdn.net/wangkang80/article/details/77914849 算法描述: 最高位是符号位,始终为0,不可用. 41位 ...
- git hub的GUI软件配置与使用
1. 安装两个软件 1. git的命令行程序--git for windows:http://git-scm.com/download/win 2. git的GUI程序--tortoisegit:ht ...
- thymleaf th:text 和 th:utext 之间的区别
1 th:text属性 可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本 文本连接:用“+”符号,若是变量表达式也可以用“|”符号 e.g. 若home.welc ...
- ECMAScript新特性【一】--Object.create
Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数: prototype 必需. 要用作原型的对象. 可以为 nu ...
- 数学图形(2.7)sphere sine wave
在球上以SIN曲线的轨迹游走. #http://www.mathcurve.com/courbes3d/couronnetangentoidale/couronnetangentoidale.shtm ...
- Informatica 常用组件Source Qualifier之四 SQL Query
源限定符转换提供 SQL 查询选项以覆盖默认的查询.您可以输入您的源数据库支持的 SQL 语句.输入查询之前,请连接您要在映射中使用的所有输入和输出端口. 编辑 SQL 查询时,您可以生成并编辑默认查 ...
- C#视频播放器【转】
1对于视频播放器来说,最重要的功能,莫过于播放视频文件了这就要用到VS自带的控件——Windows Media Player windows media player 将Windows Media P ...