sem_timedwait 会被signal的信号唤醒及处理办法

来源:互联网 发布:linux文件指针file 编辑:程序博客网 时间:2024/06/06 02:13

一直都在使用sem_timedwait 函数用来处理超时等待应答.但是突然发现 没有收到应答.竟然返回了.查了半天竟然是signal(SIGALRM, ....)的原因.man 了一下 sem_timedwait 

RETURN VALUE
       All of these functions return 0 on success; on error, the value of  the
       semaphore  is left unchanged, -1 is returned, and errno is set to indi‐
       cate the error.

ERRORS
       EINTR  The call was interrupted by a signal handler; see signal(7).这里说了.确实会被唤醒.没办法只能该程序忽略这个信号

int  _wait_sem( int  sec, char *pack_str_t){    struct timespec ts;    int sts;sem_t *wSem;wSem = &jf2414_acksem;p_ack_str = pack_str_t;    ts.tv_sec = time(0) + sec;   //important    ts.tv_nsec = 0;    /* Try to lock Semaphore */    sem_init (wSem, 0, 0);//set sem is 0while ( 1 ){    sts = sem_timedwait(wSem, &ts);    if(sts == 0){        printf("rec ack\n");        sem_destroy(wSem);        return 0;    }    else{if(errno == ETIMEDOUT){printf("sem timout\n");sem_destroy(wSem);return -1;}//else if(errno == EINTR)//{//continue;//忽略其它signal信号//}}}}



0 0
原创粉丝点击