Program runs choppy

This is a discussion on Program runs choppy within the C Programming forums, part of the General Programming Boards category; I have a piece of code which is just a simple printf statement followed by a usleep command. Then it ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    10

    Program runs choppy

    I have a piece of code which is just a simple printf statement followed by a usleep command. Then it is just looped.

    I am running this on solaris 8, and what happens is a bunch of output will come up like 15 at a time, then it will pause for a moment (longer than the usleep) and then spit out another block of output?

    If anyone could help me that would be awesome.

    Nick

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    If anyone could post the code that you're talking about, that would be awesome.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Code:
    #include <unistd.h>
    #include <stdio.h>
    
    #define MAX 250
    
    int main()
    {
        int a=0;
        while(a<=MAX)
        {printf("2\n");
        usleep(500);
        a++;}
        
        return 0;
    }

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    The program isn't flushing the output before each sleep. Try putting an fflush(stdout); after the print statement.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    It is still doing the same thing, I think it may have something to do with the scheduling on the unix box, since things are being run in round robin format, could it have to do with the shares, it runs all it can in its time quantum, and then if forced to wait.

    The only thing that causes me to doubt this theory is that it is the only process running on the box, but the pause could come from it waiting to see if its turn has come up again.
    Last edited by Smartiepants586; 06-17-2005 at 08:48 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Examing it more carefully and experimenting with the the value of the usleep, I have found that changing the value changes the amount of times it runs through before a longer pause

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well what on earth did you think usleep did?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    10
    Haha, that came out wrong.

    It means instead of

    1
    2
    3
    4
    5
    Long Pause (longer than usleep)

    its
    1
    2
    3
    Long Pause

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    usleep() has a granularity of 18.2 milliseconds. (It calls time().) So sometimes the paused time is rounded.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    17
    try system(sleep x); (stdlib.h)

    or the regular sleep(); (cant remember which header)
    but on non win systems... you can take advantage of the build in sleep function.

  11. #11
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Quote Originally Posted by faze
    try system(sleep x);
    You mean, of course, system ("sleep x"); But I don't know if that would help, unless Smartiepants586 wants to have a really large pause. Sleep can only "sleep" for one second or more, while he is experimenting with milliseconds. But, anyway, it's worth a shot.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  12. #12
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    All 'sleep's are voluntary requests to the system, the system is in no way obligated to ensure that the program wakes up after the given amount of time. If the system is busy doing something else (perhaps something with a higher priority) then the program can be on hold for substantially longer than the requested amount. The only obligation I am aware of is that the system will sleep the program at least as long as the requested amount, rounded up to the nearest timeslice.

    There are a few schedulers that automatically 'nice' programs once in a while to give lower priority programs a chance (typically the scheduler does round robin WITHIN the same priority), you may be experiencing that.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Get an Error as soon as the program runs.
    By sara.stanley in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2006, 11:47 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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