免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1593 | 回复: 0
打印 上一主题 下一主题

关于posix的定时器信号和sleep的唤醒信号冲突的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-16 10:49 |只看该作者 |倒序浏览
求救:我在程序里(arm+linux)必须要用sleep函数和比较多的定时器。
想了好多办法,总是会遇到信号的冲突问题,即定时到的时候,会同时把sleep唤醒,让sleep失去作用。
比如下面的例子程序,posix的定时器用的是SIGUSR2信号,而sleep用的是SIGALARM信号。
正常情况sleep在最后会做10s的循环,但是实际上因为定时器的干扰,sleep每次会被唤醒。
何解?

#include <stdio.h>      /*标准输入输出定义*/
#include <stdlib.h>     /*标准函数库定义*/
#include <unistd.h>     /*Unix 标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netinet/in.h>
#include <fcntl.h>      /*文件控制定义*/
#include <termios.h>    /*PPSIX 终端控制定义*/
#include <errno.h>      /*错误号定义*/
#include <string>
#include <string.h>
#include <iostream>
#include <map>
#include <set>
#include <semaphore.h>
#include <mqueue.h>
#include <signal.h>
using namespace std;

void handle(int i)
{
    time_t t;
    char p[32];

    time(&t);
    strftime(p, sizeof(p), "%T", localtime(&t));

    printf("time is %s\n", p);
}



int main()
{
    struct sigevent evp;
    struct itimerspec ts;
    timer_t timer;
    int ret;

    memset (&evp, 0, sizeof (evp));
    evp.sigev_value.sival_ptr = &timer;
    evp.sigev_notify = SIGEV_SIGNAL;

    evp.sigev_signo = SIGUSR2;
    signal(SIGUSR2, handle);

    ret = timer_create(CLOCK_REALTIME, &evp, &timer);
    if( ret )
        perror("timer_create");

    ts.it_value.tv_sec = 1;
    ts.it_value.tv_nsec = 0;
    ts.it_interval.tv_sec = 1;
    ts.it_interval.tv_nsec = 0;


    ret = timer_settime(timer, 0, &ts, NULL);
    if( ret )
    perror("timer_settime");

    while(1)
    {
            cout<<"-------------"<<endl;
            sleep(10);

    }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP