首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sigaction和SIGALRM

Sigaction和SIGALRM
EN

Stack Overflow用户
提问于 2017-05-24 01:23:50
回答 1查看 2.1K关注 0票数 0

嗨,我正在努力学习更多关于信号的知识,我写了一个简单的代码,应该只需打印警报信号所发送的所有内容。我正在使用sigaction来设置这个。但是,我在错误检查中一直返回NULL,有人能告诉我我做错了什么吗?提前谢谢!

代码语言:javascript
复制
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    #include <sys/time.h>       /* for setitimer */
    #include <unistd.h>     /* for pause */
    #include <signal.h>     /* for signal */
    #define INTERVAL 500        /* number of milliseconds to go off */

    /* function prototype */
    void DoStuff();

    int main(int argc, char *argv[]) {
      struct itimerval it_val;  /* for setting itimer */
      struct sigaction sa;
      sa.sa_handler = &DoStuff;

      /* Upon SIGALRM, call DoStuff().
      * Set interval timer.  We want frequency in ms, 
      * but the setitimer call needs seconds and useconds. */
      if (sigaction(SIGALRM,&sa,NULL) < 0) { /*set the signal to be enabled if this action occurs*/
        perror("Unable to catch SIGALRM");
        exit(1);
      }

      it_val.it_interval = it_val.it_value;
      it_val.it_value.tv_sec =     INTERVAL/1000;
      it_val.it_value.tv_usec =    (INTERVAL*1000) % 1000000;   
      it_val.it_interval = it_val.it_value;
      if (setitimer(ITIMER_REAL, &it_val, NULL) == -1) { /*set the timer to send the alarm command*/
        perror("error calling setitimer()");
        exit(1);
      }
    while(1)
    {
          pause();
    }


    }

    void DoStuff() {
      printf("bye\n");
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-24 02:52:16

sigaction不能返回null,因为它返回一个整数。我猜它还在-1。您没有正确地初始化sigaction结构。它有许多字段,但是您允许它们是未定义的。修复结构定义,然后重试。请参见:

http://man7.org/linux/man-pages/man2/sigaction.2.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44147464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档