#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    3
    Rep Power
    0

    Paused messages to the screen


    Hi!
    Is it possible to output messages to the screen, using some defined interval? I need to read elements from a file (this I can do easily) and then output them in a set number of seconds. Is this done using the time function?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Running on what system?

    If in Windows, then the WM_TIMER message handler could be used.

    If in a Win32 console app (AKA "the DOS window"), then the Sleep() function could be used to time the output. However, this would probably need to be done in another thread (ie, you would need to use multithreading) so that the other threads in the program can do their things, like read the file. Or you could do the read-an-element-process-it-and-output-it block for each element after Sleep() times out.

    If in Linux, then the description of the Win32 console app could also apply, though there the function is sleep(). Or you could make use of the SIGALRM signal and the alarm() function.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    3
    Rep Power
    0
    It is for running in Linux. A friend of mine said to use setitimer, is he right?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,162
    Rep Power
    2222
    Originally posted by morti
    It is for running in Linux. A friend of mine said to use setitimer, is he right?
    I had not heard of it before, but, after having quickly read its man page, it looks like it would do the job.

    It appears to do much the same thing as alarm() except it is more versatile. For example, you can only set the seconds of the timer with alarm(), whereas with setitimer() you can set the timeout down to milliseconds. They both generate a SIGALRM signal, so you will still need to write a signal handler. Plus, your program can still run between the signals.

    If you're not quite sure how to use setitimer(), you might want to Google for examples.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    3
    Rep Power
    0
    I saw an example. To set the timer I understand. What I don't understand is why they do:

    void handler(){
    /*do something*/
    signal(SIGALRM,handler); /*why call the handler inside the handler????*/
    }

    main(){
    struct itimerval val, curval;

    signal(SIGALRM,handler); /*this receives a signal and calls the handler, but who sends the signal???*
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Let's take the second question first.
    Code:
    main(){
    struct itimerval val, curval;
    
    signal(SIGALRM,handler); /*this receives a signal and calls the handler, but who sends the signal???*/
    The OS sends the signal. The signal() function tells the OS to send the SIGALRM signal to the handler() function. It does NOT send the signal by itself.

    Code:
    void handler(){
    /*do something*/
    signal(SIGALRM,handler); /*why call the handler inside the handler????*/
    }
    When the signal is sent to the custom signal handler, the OS handles it as follows:
    1. Reset the signal handler to the default handler
    2. Call the custom signal handler function (in your case, handler())
    So, from step 1, the SIGALRM handler is no longer pointing to your function (this behavior seems to have changed recently according to latest BSD documentation, but some *NIXes might still have it. Check the man page for signal for more). This means that the next time a SIGALRM is sent, it won't go to your handler() function. If you want your handler() function to receive the signal once again, you'll need to point SIGALRM to handler() once again.

IMN logo majestic logo threadwatch logo seochat tools logo