LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2011, 10:10 PM   #1
david1820
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Rep: Reputation: 0
Signals and settimer


I need use signal and settimer, but my doubt is, if it only send me a single signal at the end of the time of the timer it steel sending it for ever.
Also i need execute a function to recive the signal but it can be a little delayed and i need the timer ejecution simultaneously with the function, and not end the function and start to count the time to send again the signal, for this would be
necesary to use pthread?
 
Old 04-26-2011, 08:17 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
I think you may need to look at the manual pages for getitimer and setitimer (SetTimer is a Windows API function). Too, a quick Google search for "linux + setitimer + example" will offer up quite a few demonstration programs, one or more of which may get you going the the right direction.

Timers, whether the simple alarm() function or the more adaptable setitimer (and others -- look at the See Also manual pages) are intended to be used with signals functions.

Hope this helps some.
 
Old 04-26-2011, 04:01 PM   #3
david1820
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
I check the manuals, but my doubt its the timer execute when the function finished?, i need the timer execute togheter at the same time whit the function.
 
Old 04-27-2011, 08:56 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
I'm not totally clear about what it is you're trying to do; perhaps this simple example may help and, if it does not, try to explain in some detail just what you are trying to accomplish.

The alarm() function is used to set up receipt of a SIGALARM signal after a specified number of seconds. The pause() function is used to suspend execution (put a process to sleep). The pause() function can only be interrupted by a signal, and if the signal is caught by a function, pause() will return -1:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void    alrm_catch      (int);

int     main    (void)
{
        (void) fprintf (stdout, "one\n");
        /*
         *      call alrm_catch() when alarm goes off
         *      could use sigset and get same results
        */
        (void) signal (SIGALRM, alrm_catch);
        /*      schedule alarm for ten seconds from now and pause       */
        (void) alarm (10);
        (void) pause ();
        (void) fprintf (stdout, "two\n");
        exit (EXIT_SUCCESS);
}

void    alrm_catch      (int signo)
{
        return;
}
Save this a some filename.c and
Code:
make filename                    (or cc -o filename filename.c)
filename
one
two                              (ten seconds later)
This is a one-time process; i.e., you set the alarm for 10 seconds, pause, and then "catch" the signal when the alarm goes off (then this program displays "two" and exits).

A more complex version of this sort of thing would be this example:
Code:
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>

#define LOOP_LIMIT  1E12

volatile int sigcount=0;

void catcher( int sig ) {

    struct itimerval value;
    int which = ITIMER_REAL;

    printf( "Signal catcher called for signal %d\n", sig );
    sigcount++;

    if( sigcount > 1 ) {

        /*
         * Disable the real time interval timer
         */

        getitimer( which, &value );

        value.it_value.tv_sec = 0;
        value.it_value.tv_usec = 0;

        setitimer( which, &value, NULL );
    }
}

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

    int result = 0;

    struct itimerval value, ovalue, pvalue;
    int which = ITIMER_REAL;

    struct sigaction sact;
    volatile double count;
    time_t t;

    sigemptyset( &sact.sa_mask );
    sact.sa_flags = 0;
    sact.sa_handler = catcher;
    sigaction( SIGALRM, &sact, NULL );

    getitimer( which, &pvalue );

    /*
     * Set a real time interval timer to repeat every 200 milliseconds
     */

    value.it_interval.tv_sec = 0;        /* Zero seconds */
    value.it_interval.tv_usec = 200000;  /* Two hundred milliseconds */
    value.it_value.tv_sec = 0;           /* Zero seconds */
    value.it_value.tv_usec = 500000;     /* Five hundred milliseconds */

    result = setitimer( which, &value, &ovalue );

    /*
     * The interval timer value returned by setitimer() should be
     * identical to the timer value returned by getitimer().
     */

    if( ovalue.it_interval.tv_sec != pvalue.it_interval.tv_sec  ||
        ovalue.it_interval.tv_usec != pvalue.it_interval.tv_usec ||
        ovalue.it_value.tv_sec != pvalue.it_value.tv_sec ||
        ovalue.it_value.tv_usec != pvalue.it_value.tv_usec ) {
        printf( "Real time interval timer mismatch\n" );
        result = -1;
    }


    time( &t );
    printf( "Before loop, time is %s", ctime(&t) );

    for( count=0; ((count<LOOP_LIMIT) && (sigcount<2)); count++ );

    time( &t );
    printf( "After loop, time is %s\n", ctime(&t) );


    if( sigcount == 0 )
        printf( "The signal catcher never gained control\n" );
    else
        printf( "The signal catcher gained control\n" );

    printf( "The value of count is %.0f\n", count );

    return( result );
}
When this is compiled and executed (I save this as getitimer.c)
Code:
make getitimer
getitimer
Before loop, time is Wed Apr 27 09:47:38 2011
Signal catcher called for signal 14
Signal catcher called for signal 14
After loop, time is Wed Apr 27 09:47:39 2011

The signal catcher gained control
The value of count is 212618176
If you try this one, the count will most likely not be the same as shown here (I'm on a 3.1GHz dual-core 64-bit machine, you mileage may vary).

In either case, you set up a signal catcher and use a function that will generate a signal when it completes; in the first example, the alarm() function does that and the signal is caught.

Hope this helps some.
 
Old 04-27-2011, 01:29 PM   #5
david1820
LQ Newbie
 
Registered: Apr 2011
Posts: 8

Original Poster
Rep: Reputation: 0
I need a timer to call one function every 10ms (max 10ms) but the function maybe can take for example 5ms, so the timer start count after this 5ms, the function its call again after 10ms + 5ms(time of the function its running) so was 15 ms, i need the timer and function run at the same time (pthread).

Thank for your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
signals sx6 Programming 2 09-12-2009 03:10 PM
!!! about signals !!! b2na Programming 4 02-04-2005 12:34 AM
!! about signals !!! b2na General 1 01-03-2005 04:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration