Find the answer to your Linux question:
Results 1 to 8 of 8
Hi! I searched a lot the forums for some clues, but unfortunately nothing cleared it up for me. I am trying to program a timer and a function which checks ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    8

    Timer programing in C

    Hi!

    I searched a lot the forums for some clues, but unfortunately nothing cleared it up for me.

    I am trying to program a timer and a function which checks the input while the timer didn't expire. Something like:

    while (timer not expired)
    {
    do_action();
    }


    i already created my timer, but i don't manage to call any function only before this timer expired. If I call any function after I start the timer, I get an segmentation fault error.

    Could anybody suggest some solutions to my problem, please?

    Thanks!

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    If your code segfaults, you should post the relevant part here so it can be examined.
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    This is not enough information to go on. There are several types of timers available for C/C++ programming and their behavior is quite distinct. There can also be many reasons why your program segfaults that are totally irrelevant to your use of a timer. Please provide the source code that is causing (you thing) this problem.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    May 2009
    Posts
    8
    Here is the part of my code which deals the timer(s):


    //timer.c___________________________________________ ________
    #include "timer.h"

    timer_t Counter_t1, Timer_5s, Timer_4s;

    int i=0;

    timer_t CreateTimer(int signo, int freq, int choice)
    {
    struct sigevent signal_event;
    struct itimerspec its;
    timer_t timerid;

    signal_event.sigev_notify = SIGEV_SIGNAL;
    signal_event.sigev_signo = signo;
    signal_event.sigev_value.sival_ptr = &timerid;

    if (timer_create(CLOCKID, &signal_event, &timerid) == -1)
    errExit("timer_create");

    its.it_value.tv_sec = freq / 1000;
    its.it_value.tv_nsec = freq % 1000;

    if (choice == 1)
    {
    its.it_interval.tv_sec = its.it_value.tv_sec;
    its.it_interval.tv_nsec = its.it_value.tv_nsec;
    }
    else {
    its.it_interval.tv_sec = 0;
    its.it_interval.tv_nsec = 0;
    }

    if (timer_settime(timerid, 0, &its, NULL) == -1)
    errExit("timer_settime");


    return timerid;
    }

    static void SignalHandler(int signo, siginfo_t *info, void *context)
    {

    if (signo == TIMER5S)
    {
    // Send();
    // ... <-- here i initially implemented some other code which was sending some information. Idon't need it anymore, so the "if" condition is actually empty at the moment

    }

    else if (signo == SIGINT)
    {
    timer_delete(Timer_5s);
    errExit("CTRL + C pressed!");
    }
    }

    void StartTimer()
    {
    struct sigaction signal_action;

    sigemptyset(&signal_action.sa_mask);
    signal_action.sa_flags = SA_SIGINFO;
    signal_action.sa_sigaction = SignalHandler;

    if (sigaction(TIMER1S, &signal_action, NULL) == -1)
    errExit("sigaction");

    sigaction(SIGINT, &signal_action, NULL);

    Timer_5s = CreateTimer (TIMER5S, 5000, 0);

    printf("timer_1s ID is 0x%lx\n", (long) Timer_5s);
    }


    // timer.h___________________________________________ ___

    #ifndef _TIMER_H_
    #define _TIMER_H_

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


    #define CLOCKID CLOCK_REALTIME
    #define TIMER5S SIGRTMAX

    #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
    } while (0)

    timer_t CreateTimer(int signo, int freq, int choice);

    void StartTimer();

    #endif /* _TIMER_H_ */


    xyz.c_________________________________
    #include "timer.h"

    int abc()
    {
    // some other code <--everything what belongs to this part is executed properly
    StartTimer();

    // if I add some other code here, I get the error. Anyway, what I'm trying to do is to check for some action while the timer is still running. If the action was executed, i want my timer to stop and reset. This what i have here at the moment helped me by doing something when the timer expired.

    return 0;
    }

    int main()
    {
    abc();

    for(;
    ;

    return 0;
    }



    ______________________________________________

    It's still in development phase, so many things are missing and many are probably not right!

    Thanks!

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I'll look at this code as soon as I have a little time - probably later tonight.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The likely cause of your core dump. In your CreateTimer() function you are declaring timer_t and struct sigevent variables as automatics (on the stack), yet you are passing them to a function that has to live outside of the scope of CreateTimer(). So, either you need to declare them static, either in the translation unit - the source file, or in the CreateTimer() function itself, or allocate them on the heap. I suggest that you declare them as static variables at the top of the source file. You should also not be returning the timer by value. Remember, you have registered the timer by address with the timer_create() function. So, after CreateTimer() returns, it is no longer a valid address, so the entire timer sub-system is now fubar (at least in your program)!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    May 2009
    Posts
    8
    Thanks for your reply!

    I still have some questions which haven't been answered.
    How can I check for a function so long as the timer hasn't expired? Can I use all of this in doing such a function? Something like:

    while (timer not expired)
    {
    do_action();
    }

  8. #8
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Sure. In your timer handler function, just set a variable that your loop can see, such as timer_not_expired perhaps?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...