c - 信号问题,定时器(SIGEV_SIGNAL)

标签 c linux timer signals

我有一小段代码,其中我使用了 POSIX 计时器:timer_create()。计时器是使用信号方法 (SIGEV_SIGNAL) 创建的——因为我们的平台不支持 SIGEV_THREAD。当计时器到期时,它会生成一个信号 SIGUSR1 来通知它到期,并且有一个相应的处理程序来捕获此信号,在此处理程序内部(在实际程序中,未在代码中显示)我有一个设置的标志,一旦定时器给出的信号被捕获。

到此为止,一切都很好:问题是,假设如果测试程序也生成与定时器相同的信号(在本例中为 SIGUSR1),则设置相同的标志,而不是由定时器设置。因此,无法区分信号处理程序接收到的信号是定时器信号还是任何其他测试程序的信号。

你能帮我解决这个问题吗?

提前致谢。

enter code here
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <string.h>


void sig_handlerTimer1(int);

time_t timerid;
int main()
{
     int i;
     static struct sigaction sa;


     static struct sigevent sevp;  // argument to timer_create
     static struct itimerspec its; // argument to timer_gettime

     memset (&sevp, 0, sizeof (struct sigevent));
     sevp.sigev_value.sival_ptr = &timerid;
     sevp.sigev_notify = SIGEV_SIGNAL;
     sevp.sigev_notify_attributes = NULL;
     sevp.sigev_signo = SIGUSR1;
     sevp.sigev_notify_function=sig_handlerTimer1;

     /* Setting timer interval */
     its.it_interval.tv_sec = 0;
     its.it_interval.tv_nsec = 0;

     /* Setting timer expiration */
     its.it_value.tv_sec = 2;  // First expiry after 1 sec
     its.it_value.tv_nsec = 0;

     /* Setting the signal handlers before invoking timer*/
     sa.sa_handler = sig_handlerTimer1;
     sa.sa_flags = 0;
     sigaction(SIGUSR1, &sa, NULL);

     if (timer_create(CLOCK_REALTIME, &sevp, &timerid) == -1)
     {
             fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_create failed   to create timer. " \
                    "Leak measurement will be for entire duration of the execution   period:%s \n", strerror(errno));
            return;

      }

     if (timer_settime(timerid, 0, &its, NULL) == -1)
     {
             fprintf(stderr, "LeakTracer (timer_trackStartTime): timer_settime failed  to set the timer. " \
                    "Leak measurement will be for entire duration of execution period:%s \n", strerror(errno));
             return;

      }

      for(i=0; i<10; i++)
      {
             printf("%d\n",i);
             if(i==3) {
                 kill(getpid(), SIGUSR1); // SIGUSR1 also generated by test program  which reaches same handler and sets flag (THIS IS UN-DESIRABLE)
              }
             sleep(1);
       }

  }

  void sig_handlerTimer1(int signum)
  {

     int flag = 1;
     printf("Caught signal: %d\n",signum); // How to understand this signal caught, is that of test program of timer expiry?
     if (timer_delete(timerid) < 0)
     {
             fprintf(stderr, "timer deletion failed. " \
                     "This may result in some memory leaks (sig_handlerTimer1):%s \n", strerror(errno));
      }
   }

添加:一旦捕获到信号,是否有任何方法可以在处理程序内部知道计时器是否真的过期?

最佳答案

您可以通过安装带有SA_SIGINFO标志的信号处理程序和struct sigactionsa_sigaction成员来区分信号的原因,然后使用传递给信号处理程序的 siginfo_t *si_code 元素。如果是 SI_TIMER,则信号是由计时器到期生成的。

关于c - 信号问题,定时器(SIGEV_SIGNAL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800342/

相关文章:

C++:如何将一个函数(不知道其参数)传递给另一个函数?

c# - c# 两台电脑之间的通信

c - 语法错误: '{'

c - 在 char 指针上使用 strcpy 时程序中止? (在 char 数组上工作正常)

linux - 清除文件而不更改其时间戳

linux - 在低权限用户上运行 shell 脚本

linux - 由于对 Nuget 的 SSL 身份验证,docker linux 容器中的 .NET Core 构建失败

android - Android 中的处理程序逻辑

c - OpenGL usampler2d值不能被除

swift - 我无法在 Swift 3 中使用计时器更改 label.text