Re: Interval timers on Fedora

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Douglas Phillipson wrote:


[snip]

My result on the timer issue is that a interval timer won't call its timeout function while msgrcv() is waiting for a message on a queue.

Regards

Doug P


Strange, it works for me. Try this!

Regards,

John


#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

static int signal_cnt;

//--------------------------------------------------------------------------------

void catch_signal ( int ignored )
{
    ++ signal_cnt;
}

//--------------------------------------------------------------------------------

int main ( int argc, char **argv )
{
    // create msg queue

    int mqid;

    if ( ( mqid = msgget ( (key_t) 12345, IPC_CREAT | 0600 ) ) == -1 )
    {
        exit ( 1 );
    }

    // set a timer

    struct itimerval it;
    struct sigaction sa;

    memset ( &sa, 0, sizeof ( sa ) );
    sa.sa_handler = catch_signal;
    sigemptyset ( &sa.sa_mask );
    sa.sa_flags = SA_RESTART;
    sigaction ( SIGALRM, &sa, NULL );

    memset ( &it, 0, sizeof ( it ) );
    it.it_interval.tv_sec = 5;
    it.it_value.tv_sec = 5;

    if ( setitimer ( ITIMER_REAL, &it, NULL ) )
    {
        exit ( 1 );
    }

    struct msgbuf *msgp; ssize_t msz;

    msgp = malloc ( 512 );

    for ( ;; )
    {
        // get a message

        if ( ( msz = msgrcv ( mqid, msgp, 256, 0, 0 ) ) == -1 )
        {
             printf ( "msgrcv returned errno [%d]\n", errno );
        }

        fprintf ( stderr, "signal count [%d]\n", signal_cnt );
    }

    return 0;
}


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux