GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2015, 11:16
AsmaNew AsmaNew is offline
New Member
 
Join Date: Nov 2015
Posts: 1
AsmaNew is on a distinguished road

Absolute time


I have this program that implements a counter down using a relative time.
The output my be the following. depends on the input:

Code:
Enter down-counter value: 3 Enter time-out interval: value:sec nsec: 1 1 counter: 2 counter: 1 counter: 0 stop - start = 3.000865 seconds time drift: 0.000865 seconds press Ctrl-c to terminiate it "

Now I want to modify this program to use absolute time.

I have an idea :
first add clock_gettime () to "the time-out interval: values"
and then save it as T.
Then Set the timer to expire at time T. (Use the TIMER_ABSOLUTE flag.)
Then In the timer signal handler, add "the time-out interval: values" to T and set the timer to expire at time T.

But I am not so sure and I am looking to hint or some coding to achieve what I want.
This is my program:


CPP / C++ / C Code:
/*  file: timer.c
    function: show POSIX interval timer and signal handling
    run:  ./timer
 */

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <linux/limits.h>
#include <time.h>
#include <signal.h>

int counter, counter_tmp;    /* counter value*/
timer_t timer_id;
struct itimerspec time_out;  /* time_out settings*/
struct timespec start, stop; /* start and stop time for the counter*/

void timer_handler( int signo);
void time_drift ( void );

int main(){


        struct sigaction sig_act;
        struct sigevent  evp;
        int status;
        sigset_t  mask;

        /* create a relative timer*/
        sigemptyset(&mask);
        sig_act.sa_handler =  timer_handler;
        sig_act.sa_flags = 0;
        sigemptyset(&sig_act.sa_mask);
        sigaction(SIGUSR1, & sig_act, NULL);

        /* create a relative timer, using SIGUSR1*/
        evp.sigev_signo = SIGUSR1;
        evp.sigev_notify = SIGEV_SIGNAL;        
        status = timer_create(CLOCK_REALTIME, &evp, &timer_id);
        if (status == -1) {
           fprintf(stderr, "Error in timer_create\n");
           //exit(1);
        }
        
       /* read timer settings */
       printf("Enter down-counter value: "); 
       scanf("%d", & counter); 
       counter_tmp = counter; /* counter_tmp is counted down*/
       printf("Enter time-out interval: value:sec nsec:  ");
       scanf("%d %d", & time_out.it_value.tv_sec, &time_out.it_value.tv_nsec);
       time_out.it_interval = time_out.it_value;
       /* record start time*/
       clock_gettime( CLOCK_REALTIME, & start);

        /* set timers */
        status = timer_settime(timer_id, 0, &time_out, NULL);
        if (status == -1) {
           fprintf(stderr, "timer_settime failed \n");
           //exit(1);
        }
        
        /*
         * Loop forever.  
         * press CRTL/C to terminate.
         */
        for(;;)   pause();
}



/* signal hander for SIGUSR1 generated by relative timer */
void timer_handler( int signo ){

      counter_tmp --;
      printf("counter: %d\n", counter_tmp);
      if (counter_tmp == 0 ){
         clock_gettime( CLOCK_REALTIME, &stop);
         timer_delete( timer_id );
         time_drift( );
         printf("press Ctrl-c to terminiate it\n");
      }
}

/* compute time drift and print*/
void time_drift (void){
  float drift;

  drift = stop.tv_sec - start.tv_sec;
  drift += (stop.tv_nsec - start.tv_nsec) / 1000000000.0;
  printf("stop - start = %f seconds\n", drift);
  drift -= counter * (time_out.it_interval.tv_sec + time_out.it_interval.tv_nsec/1000000000.0);
  printf("time drift: %f seconds\n", drift);
}



Thanks in advance
  #2  
Old 11-Nov-2015, 10:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: absolute time


1. Your "idea" is, in fact, the problem statement for Exercise 9.22 in "Unix Systems Programming:Communication, Concurrency, and Threads" by Robbins&Robbins, right? See Footnote.

2. Do you understand everything in the example program? Have you read and understood the material leading up to this exercise?

3. What have you tried so far? (Show some code.)


Regards,

Dave

Footnote: If this did not come from the textbook that I referred to, and if you don't have that textbook, there are places on the web that have free downloads.
__________________
Sometimes I just can't help myself...
Last edited by davekw7x : 11-Nov-2015 at 11:06.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
★Get 20% One Time OFF + Extra 34% Recurring Discount On Yearly Billing Cycle ★ Greg-Netost Web Hosting Advertisements & Offers 0 11-Nov-2014 00:26
Write a C++ program to accurately determine completion times... kishore ksingam C++ Forum 2 18-Feb-2011 14:03
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
time Problem zuzupus MySQL / PHP Forum 9 24-Jul-2003 07:02

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:02.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.