Thread: c posix timer

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    3

    Cool c posix timer

    1. in case of posix timers, could the next fire cause interfere with function that has not been exited yet?. if timr period is short and before function exit, another fire occurs, what would happen?. they get queued?. could cause congestion?.


    2.
    Code:
    struct itimerval t;
    t.it_interval.tv_sec=0;
    t.it_interval.tv_usec=500000;
    t.it_value=t.it_inerval;
    raise(SIGALRM);
    settiner(TIMER_REAL,&t, NULL);
    raise(SIGALRM) is called to start the timer immediately.


    how to stop a timer immediately?. How to restart the timer after stopping?.
    how to delete a timer?. How to restart the timer after deleting?.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > if timr period is short and before function exit, another fire occurs, what would happen?.
    Signals are not queued.

    Generally speaking, you should not be doing a lot of work inside signal handlers.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/signal.h>
    #include <sys/time.h>
    
    // https://en.wikipedia.org/wiki/Ackermann_function#Table_of_values
    // https://www.geeksforgeeks.org/ackermann-function/
    int ack(int m, int n)
    {
        if (m == 0){
            return n+1;
        }
        else if((m > 0) && (n == 0)){
            return ack(m-1, 1);
        }
        else if((m > 0) && (n > 0)){
            return ack(m-1, ack(m, n-1));
        }
    }
    
    // Only call things on this list from a signal handler
    // https://man7.org/linux/man-pages/man7/signal-safety.7.html
    void fire(int sig) {
        static char in[] = "@-in\n";
        static char out[] = "@-out\n";
        write(1,in,strlen(in));
        if ( ++in[0] > '~' ) in[0] = '@';
        // Uncomment the next line to cause many events to be missed.
        //ack(3,11);  // burn way too much time in signal handler
        write(1,out,strlen(out));
        if ( ++out[0] > '~' ) out[0] = '@';
    }
    
    int main ( ) {
        signal(SIGALRM,fire);
        struct itimerval t;
        t.it_interval.tv_sec=0;
        t.it_interval.tv_usec=500000;
        t.it_value = t.it_interval;
        raise(SIGALRM);
        setitimer(ITIMER_REAL, &t, NULL);
        printf("Ack(3,11)=%d\n",ack(3,11));
        printf("Ack(4,1)=%d\n",ack(4,1));
        unsigned int u = sleep(5);
        printf("Sleep returned %u\n", u);
        return 0;
    }
    Uncomment line 30 for a totally different experience.


    > how to stop a timer immediately?. How to restart the timer after stopping?.
    > how to delete a timer?. How to restart the timer after deleting?.
    The manual page has all these answers.
    getitimer(3p) - Linux manual page
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    3
    this is what i'm trying to do.
    1. as soon as a flag is raised , need as instantaneously as possible to poll a status byte from serial port, then set timer off till next cycle. I found the fast timers suitable as it seems to be deterministic. But in my case when i make it too fast, the serial IO at low level crashes. I read handling the timers through threads is better than the signal handlers in these cases. I am not familiar with this yet.

    2. replaced the timer with with a thread and works but pthread_create() may not be as instantaneous and deterministic as timers. I was thinking of calling pthread_create once and use some flag for stop/restart cycle. This has not worked satisfactorily yet.
    any ...
    Last edited by cprogg; 08-09-2022 at 01:38 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So why don't you make your serial line unbuffered and call read() with a single char buffer from a thread?

    The thread will block until there is a char available, and because it's unbuffered, it will return immediately with the char.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with posix semaphores.
    By Dimes in forum C Programming
    Replies: 2
    Last Post: 11-16-2010, 07:53 AM
  2. POSIX compliance
    By MK27 in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 02:57 PM
  3. POSIX library is not being used
    By redruby147 in forum C Programming
    Replies: 5
    Last Post: 01-08-2009, 12:00 AM
  4. POSIX-Threads
    By posixunil in forum Linux Programming
    Replies: 2
    Last Post: 04-17-2007, 06:43 AM
  5. posix for visual c++ 6
    By xErath in forum Tech Board
    Replies: 4
    Last Post: 05-20-2005, 12:46 AM

Tags for this Thread