Like Tree2Likes

C guru help needed URGENT

This is a discussion on C guru help needed URGENT within the C Programming forums, part of the General Programming Boards category; I have a program , custom , thats eating cpu cycles by running getttimeofday i searched around and found a ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    C guru help needed URGENT

    I have a program , custom , thats eating cpu cycles by running
    getttimeofday
    i searched around and found a way to load my own library with empty gettimeofday ()
    it seemed to help as the process is accessing my version
    but now the program hangs and strace shows

    Code:
    poll  ([{fd=4, events=POLLIN}, {fd=0, events=POLLIN}], 2, 4) = 0 (Timeout)
    is there a way someone can help me with this ?
    can send full strace if that helps you figure out what its doing ?
    sorry i dont have source code for it


    main point .. if i run one copy of the program it works but 2 or more copies it displays this behaviour..if i let it use the regular gettimeofday then it crashes with segmentation fault: resource temporarily available
    Salem likes this.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sounds like your real problem is that you are calling gettimeofday(), WAY too often. Your last word in the above post WAS meant to be "unavailable", right?

    That's an old style crutch for a bad design aspect in your program. Don't make the crutch look better by giving it a paint job. Get rid of the crutch. Your program should not need the time of day, dozens or more times, per second.

    P.S. Welcome to the forum, NetworkLearning.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    yes , unavailable.
    unfortunately i dont have a choice and have to work with this.
    another way i thought was can i write my gettime to return time ? but sleep 2 seconds before returning ? maybe that will work ?
    i tried finding the source for gettimeofday but no luck so far or is it just whats in the man page

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    gettimeofday is not a standard C function (according to my compiler, which nevertheless includes it), so I'm not sure what your help files will show.

    If you want to time something maybe use clock() to set the start and stop variables, and then subtract start from stop?

    In any case, you can't call gettimeofday, dozens or hundreds of time per second - that's just the bottom line. Shouldn't be necessary either, but I don't know what you're trying to do here. Changing to a different library is not the answer, and I doubt using clock() will be it either (although it would help, imo). You have to do something where you are getting your time, with fewer calls per second/s.

    One way to do that is to use a loop, and have it call the gettimeofday, only if (i % 10000 == 0), but adjust the 10000 as needed. Depending on your program, I've found 6 time checks per second to give entirely adequate looking timers.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    adak can we chat on skype / gtalk etc
    i think i can explain it better ..
    would reallllly help me out

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Adak, you're not reading the posting correctly! He doesn't have the source code!!! He changed the implementation of gettimeofday in the library dynamically included by the program to make the change he described (which was to turn it into an empty function).

    Is that correct OP?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    yes yes
    now what i want to do is .. when gettimeofday is called , which will be my getttimeofday , i need it to sleep for 20 seconds, and return what gettimeofday original was going to return
    currently my gettimeofday just returns 0

    oogabooga .. you understand correctly

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have none of them.

    If you have proprietary code or whatever, it's OK, you don't have to show it. Just tell what it is that you're code is doing - and one important detail is how many times per second you want/feel you need/and are trying to, get the time from the system.

    AHA! I didn't catch the whole substitution of library to alter the code therein, idea. Sleeping for 20 seconds -- sounds like a LONG sleep!

    My problem is, I don't grok the overall of what you're doing here. To do what you describe, you could simply sleep 1 second, then call the gettimeofday(), which would give you a good result, and then sleep another 19 seconds, and continue on.

    Oogabooga is a better programmer than I, so prick up them ears!
    Last edited by Adak; 09-19-2012 at 12:01 AM.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    yes but when i load my own library then i cannot call gettime original can i ?

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Adak View Post
    Oogabooga is a better programmer than I
    Oh, I doubt that!

    OP, can you change the name of the original version to something else and call it from yours?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    yes but when i load my own library then i cannot call gettime original can i ?

    but how can i change the name in the original without source ?

    so this is where i am at now
    i have a lib created which has gettimeofday , which returns just 0
    Code:
    //#include <stdio.h>
    int gettimeofday(void *a,void *b)
    {
    return 0;
    }
    i would like to change it to return the values , ie the time in correct format , that the original gettime would have returned
    but only after sleeping X seconds ( i can play with X and see what works )



    or , just thought of this , write the time to a temp file the first time its called , and then keep returning that .. effectively time staying same ,
    i think this would be the way to go

    please please help guys
    thanx guys
    Last edited by NetworkLearning; 09-19-2012 at 12:26 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh, I know so.

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you're just looking for a simple implementation of gettimeofday, something like this might work:
    Code:
    #include <time.h>
    
    int gettimeofday(
        struct timeval *tv,
        struct timezone *tz  /* not used; should be NULL */
    ) {
        struct timespec ts;
        if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
            return -1;
        tv->tv_sec = ts.tv_sec;
        tv->tv_usec = ts.tv_nsec / 1000;
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    i tried compiling

    gcc -c ltimer.c -o ltimer.o

    ltimer.c:10:7: error: dereferencing pointer to incomplete type
    ltimer.c:11:7: error: dereferencing pointer to incomplete type
    Last edited by NetworkLearning; 09-19-2012 at 12:48 AM.

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try adding this just before the function:
    Code:
    struct timeval {
        time_t      tv_sec;
        suseconds_t tv_usec;
    };
    struct timezone {
        int tz_minuteswest;
        int tz_dsttime;
    };
    EDIT:
    Actually, you should probably just include <sys/time.h> instead.
    Last edited by oogabooga; 09-19-2012 at 01:02 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent help needed
    By bathulasubbu in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 10:33 AM
  2. Urgent, help needed.
    By sinnclaus in forum C Programming
    Replies: 4
    Last Post: 03-29-2010, 07:08 AM
  3. Urgent! Help Needed
    By DarkManiac in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2004, 08:16 PM
  4. Urgent: Help needed.
    By Cn00b in forum C Programming
    Replies: 12
    Last Post: 04-02-2003, 10:18 PM
  5. Urgent help needed!!!
    By gibs21 in forum C Programming
    Replies: 2
    Last Post: 09-26-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21