Prabhjot Singh
Prabhjot Singh

Reputation: 55

I am trying to implement timer using c in ubuntu, but receiving errors of declaration

please help me, i want to implement timer using c in ubunto. i have the written the code but it is giving two errors. I am compiling it using -lrt option of gcc. Errors i am getting is: timer1.c: In function ‘main’: timer1.c:18:52: error: ‘SIG’ undeclared (first use in this function) timer1.c:18:52: note: each undeclared identifier is reported only once for each function it appears in timer1.c:21:23: error: ‘handler’ undeclared (first use in this function)

My code is:

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>

timer_t timerid;

int main(int argc, char *argv[])
{


           struct sigevent sev;
           struct itimerspec its;
           long long freq_nanosecs;
           sigset_t mask;
           struct sigaction sa;
    printf("Establishing handler for signal %d\n", SIG);

    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = handler;
    sigemptyset(&sa.sa_mask);


    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIG;
    sev.sigev_value.sival_ptr = &timerid;

 printf("timer ID is 0x%lx\n", (long) timerid);
//    timer_create(CLOCKID, &sev, &timerid);
    /* Start the timer */

    its.it_value.tv_sec = 1000;
    its.it_value.tv_nsec =0;
    its.it_interval.tv_sec = its.it_value.tv_sec;
    its.it_interval.tv_nsec = its.it_value.tv_nsec;

    timer_settime(timerid,0, &its, NULL);
    sleep(10);


}


 static void handler(int sig, siginfo_t *si, void *uc)
{
   if(si->si_value.sival_ptr != &timerid)
    {
        printf("Stray signal\n");
        } 
  else 
    {
        printf("Caught signal from timer\n");
        }


}

Upvotes: 0

Views: 941

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

SIG is undeclared because you never declare it, and we can't tell you how to fix it since we don't know what it's supposed to be. handler is undeclared because you forgot the forward declaration. Put a copy of the function signature followed by a semicolon before the function where it's used.

static void handler(int sig, siginfo_t *si, void *uc);

int main(int argc, char *argv[])
{
   ...

Upvotes: 1

Related Questions