On 02/27/2016 06:48 AM, Ranjan Maitra wrote:
On Sat, 27 Feb 2016 14:09:25 +0000 "Patrick O'Callaghan" 
<pocallag...@gmail.com> wrote:

On Sat, 2016-02-27 at 07:44 -0600, Ranjan Maitra wrote:
#define INTERVAL 1        /* number of milliseconds to go off */

int main() {
     double sum = 0;
     struct itimerval initial, updated;

     initial.it_value.tv_sec     = INTERVAL/1000000;
     initial.it_value.tv_usec    = (INTERVAL/1000000) * 1000000;

To start with, these are both integer values, so you're initializing
them to 0. There may be other bugs but I stopped looking when I saw
this.

poc
--

OK, thanks! So, I tried this:

#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

#define INTERVAL 1        /* number of milliseconds to go off */

int main() {
        double sum = 0;
        struct itimerval initial, updated;
        
        initial.it_value.tv_sec     = INTERVAL;
        initial.it_value.tv_usec    = INT_MAX;
        initial.it_interval = initial.it_value;

If you want the timer to go off every millisecond:

        initial.it_value.tv_sec = 0;
        initial.it_value.tv_usec = 1000;

The ".tv_usec" must fall in the range 0 <= .tv_usec <= 999999.


        printf("%ld\n", initial.it_value.tv_usec);
        
        if (setitimer(ITIMER_VIRTUAL, &initial, NULL) == -1) {
                perror("error calling setitimer()");
                exit(1);
        }

        for (unsigned int i; i < 100000; i++)
                sum += 1./i;
        
        if (getitimer(ITIMER_REAL, &updated) == -1) {

Uh, why are you setting ITIMER_VIRTUAL, then reading ITIMER_REAL?

                perror("error calling getitimer()");
                exit(1);
        }

        printf("Time started = %ld\n; Time taken = %ld\n: Time taken = %ld\n",
               initial.it_value.tv_usec, updated.it_value.tv_usec,
               initial.it_value.tv_usec - updated.it_value.tv_usec);
        return 0;
}


But now setitimer does not execute:-(

$gcc -o timer -std=c99 -Wall -pedantic getitimer.c -lrt -O3
$./timer
2147483647
error calling setitimer(): Invalid argument

That error message is pretty conclusive and indicates what I said at the
top.
----------------------------------------------------------------------
- Rick Stevens, Systems Engineer, AllDigital    ri...@alldigital.com -
- AIM/Skype: therps2        ICQ: 226437340           Yahoo: origrps2 -
-                                                                    -
-    Admitting you have a problem is the first step toward getting   -
-    medicated for it.      -- Jim Evarts (http://www.TopFive.com)   -
----------------------------------------------------------------------
--
users mailing list
users@lists.fedoraproject.org
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org

Reply via email to