Loop doesnt continously execute

This is a discussion on Loop doesnt continously execute within the C Programming forums, part of the General Programming Boards category; Hi there i am writting a process scheduler and im confused at this point in my code below.... When i ...

  1. #1
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

    Question Loop doesnt continously execute

    Hi there i am writting a process scheduler and im confused at this point in my code below....

    When i compile the program, it compiles successfully and decrements the structure member qaunta by -1 continously. But what i want it to do is to decrement the qaunta variable by -1 and then print that variable and move onto the next variable do the same to that one and keep going round until qaunta reaches the value of 0 (round robin fashion). Thanks
    Code:
    int comp(const void *a, const void *b) {
        return ((struct process*)a)->priority - ((struct process*)b)->priority;
    }
    
    /* order the priority in the correct order */
        qsort(proc, 199, sizeof(*proc), comp);
    
    } 
    /* Decrement the process qaunta */ 
    for ( i = 0; i < 199; i++ ) {
        printf("%s %d %d\n", proc[i].id, proc[i].qaunta, proc[i].priority);
        while (proc[i].qaunta != 0 ) {        
            proc[i].qaunta = proc[i].qaunta - 1;
                printf("%s Qaunta: %d \n",proc[i].id, proc[i].qaunta);
                 if (proc[i].qaunta == 0 )
                    printf("Goodbye\n");
                }
    Heres the end of my output (its continous throughout):
    Process10 Qaunta: 150
    ..................................
    Process10 Qaunta: 0
    Last edited by spendotw; 01-04-2012 at 06:42 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,571
    Those are links; that he wants you to follow.
    He most likely thinks your question is bad (missing needed information) or some other violation of a smart question rules.
    The question is beyond my own knowledge.

    Note: Salem considers cross posting a wrong thing to do.

    NOTE: Obvious thing to try is printing to stderr instead of stdout.

    NOTE: Your posted code has no endless loop in it; is there some reason you think it does?

    Tim S.
    Last edited by stahta01; 01-04-2012 at 07:29 AM.

  5. #5
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by stahta01 View Post
    Those are links; that he wants you to follow.
    He most likely thinks your question is bad (missing needed information) or some other violation of a smart question rules.
    The question is beyond my own knowledge.

    Note: Salem considers cross posting a wrong thing to do.

    NOTE: Obvious thing to try is printing to stderr instead of stdout.

    NOTE: Your posted code has no endless loop in it; is there some reason you think it does?

    Tim S.
    Oh right sorry Salem I wasn't aware I couldn't post my question on other forums. I dont usually do this, it was just under the circumstances i am in.

    I though the loop was endless in the sense that qaunta is required to reach the value of 0 before exiting. Surely its endless if the qaunta reaches 0 each time.

    The problem is that i cant get it to process the other qaunta variables in a circular manner

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by spendotw View Post
    I though the loop was endless in the sense that qaunta is required to reach the value of 0 before exiting. Surely its endless if the qaunta reaches 0 each time.
    If you have a specific value that denotes the end, then by definition, it's not endless.


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

  7. #7
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40

    Question

    Quote Originally Posted by quzah View Post
    If you have a specific value that denotes the end, then by definition, it's not endless.


    Quzah.
    Okay, so like for an expression as such i > j
    when i is never going to be more than j

    Either way, if it is endless how will that help my variables to be processed in a rounded fashion rather than printing the whole all at once.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by spendotw View Post
    Okay, so like for an expression as such i > j
    when i is never going to be more than j
    In that case, the loop will never iterate even once.

    If you want to do this round robin, I think you want something more like
    Code:
        if (proc[i].qaunta > 0 ) proc[i].qaunta -= 1;
    in place of your inner while() loop. Then you would replace the outer for loop with something like:
    Code:
    int i = 0;
    while (1) {
    [...]
        if (++i > 199) i = 0;
    }
    Meaning you would require some kind of explicit break condition inside. You say "until qaunta reaches 0" but you do not say which qaunta: any of them? All of them? This determines the break condition.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using the while loop to execute sequences
    By jamesallen4u in forum C Programming
    Replies: 15
    Last Post: 10-03-2011, 04:42 AM
  2. why the first loop doesnt stop..
    By transgalactic2 in forum C Programming
    Replies: 1
    Last Post: 06-20-2009, 07:07 AM
  3. why this while condition doesnt stop the loop??
    By transgalactic2 in forum C Programming
    Replies: 40
    Last Post: 04-07-2009, 08:38 AM
  4. My loop doesnt work
    By Evandb in forum C Programming
    Replies: 3
    Last Post: 05-17-2004, 11:04 PM
  5. doesnt work while ..loop
    By condorx in forum C Programming
    Replies: 3
    Last Post: 02-24-2002, 11:53 AM

Tags for this Thread


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