Like Tree4Likes

I need help with looping lowest priority values first

This is a discussion on I need help with looping lowest priority values first within the C Programming forums, part of the General Programming Boards category; Hi there, i am writting a process scheduler in a round robin fashion and I want to process all the ...

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

    Question I need help with looping lowest priority values first

    Hi there, i am writting a process scheduler in a round robin fashion and I want to process all the processes from a input file. I have got all the processes from the file and ordered the priority using qsort with 0 as the highest priority and 20 as the lowest.

    I want to know how I can alter my loops so that it processes all the priority members that have a value of 0 first then the 1's thens 2's etc. I can roughly think of one way which may be a bit lengthily, which I don't mind as long as it gets the job done, but as a newbie to C I need help implementing it.

    The file layout is like so:

    ProcessID Quantum Priority
    Process0 50 18
    Process1 3 3
    Process2 99 0


    Code:
     
    /* order process priority from 0 to 20 */
    qsort(proc, num_procs, sizeof(*proc), comp);
    
    bool needToContinue = 1;
    //keep processing until all have been processed (ie all quanta == 0)
    while(needToContinue)
    {    
        //reset the flag each loop of the array
        needToContinue = false;
        //loop thru the entire array
        for(num_procs=0;num_procs<199;++num_procs)//loop through the array
        {    
            
                //check to see if this element is finished
                if(proc[num_procs].quantum > 0) //not finished so process
                {
                proc[num_procs].state = WORKING;
                
                    if ( proc[num_procs].state == WORKING ) //Process being executed ...
                    { 
                    proc[num_procs].quantum--;
                    printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
                    }
                }
            }
            //now check to see if we have finished processing this element 
            //or need to keep going next loop
            if(proc[num_procs].quantum > 0)
            {
                //we found an element that will need to be processed in the next loop, 
                //so set the flag to continue looping
                needToContinue = true;
            }    
    }
    //show processing is finished
    Using this code here does execute all the processes but only decrements the quantum from the 0's to 20's once each time it passes through the loop. But I want all the 0 priorities quantum to reach 0 and then move onto the next highest priority which is 1 ...

    I was thinking something along the lines of :
    Code:
     while ( proc[num_procs].priority == 0) { 
    The Code Above to be in here  
    printf("End of Process Scheduling\n");
    Thanks for any feedback

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    Don't you need a
    for ( priority = 0 ; priority < 20 ; priority++ )
    loop?
    quzah likes this.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
                //check to see if this element is finished
                if(proc[num_procs].quantum > 0) //not finished so process
                {
                proc[num_procs].state = WORKING;
                 
                    if ( proc[num_procs].state == WORKING ) //Process being executed ...
                    {
                    proc[num_procs].quantum--;
                    printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
                    }
                }
    You can just rewrite this to:
    Code:
               //check to see if this element is finished
                if(proc[num_procs].quantum > 0) //not finished so process
                {
                    proc[num_procs].state = WORKING;
                    proc[num_procs].quantum--;
                    printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
                }
    There's no need for an if check that will always be true.


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

  4. #4
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by Salem View Post
    Don't you need a
    for ( priority = 0 ; priority < 20 ; priority++ )
    loop?
    Hi, I don't see where this would fit in my program? Although the priority member is the variable I am trying to control, it doesn't help me to process the data entries with a priority of 0 all together.

  5. #5
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by quzah View Post
    Code:
                //check to see if this element is finished
                if(proc[num_procs].quantum > 0) //not finished so process
                {
                proc[num_procs].state = WORKING;
                 
                    if ( proc[num_procs].state == WORKING ) //Process being executed ...
                    {
                    proc[num_procs].quantum--;
                    printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
                    }
                }
    You can just rewrite this to:
    Code:
               //check to see if this element is finished
                if(proc[num_procs].quantum > 0) //not finished so process
                {
                    proc[num_procs].state = WORKING;
                    proc[num_procs].quantum--;
                    printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
                }
    There's no need for an if check that will always be true.


    Quzah.
    Thanks Quzah, any help on this issue

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd probably do this as an array of linked lists, with enough buckets in my array for each priority level.
    Code:
    read line from file
    add to buckets[ priority ] this process.
    for each bucket
        process all processes at this priority level
    Honestly I'm not sure why you are even having a problem. If you've already sorted your list of processes based on priority level, what's the problem? Just run through the whole thing in order from lowest to highest.


    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
    Quote Originally Posted by quzah View Post
    I'd probably do this as an array of linked lists, with enough buckets in my array for each priority level.
    Code:
    read line from file
    add to buckets[ priority ] this process.
    for each bucket
        process all processes at this priority level
    Honestly I'm not sure why you are even having a problem. If you've already sorted your list of processes based on priority level, what's the problem? Just run through the whole thing in order from lowest to highest.


    Quzah.
    Yeah I mean it executes fine, its just my algorith is a bit off. For what I need to do I am require to do the round robin scheduling slightly different. I have to decrement all the quantum from the processes that have a priority of 0 until the qauntum reach 0. And then move onto the next highest priority remaining which would be 1 then 2 then 3 up to 20 .

    But the code I have decrements all the priority members once starting from priority 0 and ending at 20 until the all quantum is 0. This is not what I want

    I want my output to be something like this:

    Process0 4 0
    Process1 5 0
    Process0 3 0
    Process1 4 0
    Process0 2 0
    Process1 3 0
    Process0 1 0
    Process1 2 0
    Process0 0 0
    Process1 1 0
    Process1 0 0
    And then move onto the processes that have a priority of 1 ....

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Keep track of where you started in the array. While this array's spot is the same priority level as what you are doing, do something with it. If it's not, you've gone too far, so jump back to the start.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    what about something like that:
    Code:
    int main(void) { while(any_working_process()) run_all_working_procs_at_prio(find_lowest_working_prio()); }
    You just need to split your initial code into 2 functions and write the last one (find_lowest_working_prio). Of course, run_all_working_procs_at_prio must loop over the proc array.

  10. #10
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by quzah View Post
    Keep track of where you started in the array. While this array's spot is the same priority level as what you are doing, do something with it. If it's not, you've gone too far, so jump back to the start.


    Quzah.
    Yes this is exactly what I wanted to implement, but I have no idea how to do so... I have altered my code as below, but it only decrements the first process continously until it reach zero rather than all the the process of that priority. So it got it right by decrementing the priority value 0 but doesn't increment the array element onto the next process. And then once thats done, i have no idea how i am suppose to return to the start of the array element :S

    Code:
     
    bool needToContinue = 1;
    while(needToContinue)
    {
    //keep processing until all have been processed (ie all quanta == 0)
    	//reset the flag each loop of the array
    	needToContinue = false;
    	//loop thru the entire array
    	for(num_procs=0;num_procs<199;++num_procs)//loop through the array
    	{	
    	while ( proc[num_procs].priority == 0 ) 
    	{		
    		//check to see if this element is finished
    		if(proc[num_procs].quantum > 0) //not finished so process
    		{
    		proc[num_procs].state = WORKING;
    		proc[num_procs].quantum--;
    		printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
    		}
    		if ( proc[num_procs].priority != 0 ) 
    		{
    		printf("Prefix: Priority has changed, need to return to the start \n");
    		}
    		//now check to see if we have finished processing this element 
    		//or need to keep going next loop
    		if(proc[num_procs].quantum > 0)
    		{
    			//we found an element that will need to be processed in the next loop, 
    			//so set the flag to continue looping
    			needToContinue = true;
    		}
    	}		
    	}
    }

  11. #11
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by root4 View Post
    what about something like that:
    Code:
    int main(void) { while(any_working_process()) run_all_working_procs_at_prio(find_lowest_working_prio()); }
    You just need to split your initial code into 2 functions and write the last one (find_lowest_working_prio). Of course, run_all_working_procs_at_prio must loop over the proc array.
    Hi

    I am trying to do this program without using any custom functions and just using the basic system functions

    I have already sorted all my priorities and ordered them from lowest to highest, and i can run them in that order, but i'm having trouble with the implementation of the algorithm i want to use

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,697
    I am trying to do this program without using any custom functions and just using the basic system functions
    "I am trying to sew a shirt without following any designs."
    quzah likes this.

  13. #13
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    "I am trying to sew a shirt without following any designs."
    Thanks for this analogy it really helps...

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Quote Originally Posted by spendotw View Post
    Hi

    I am trying to do this program without using any custom functions and just using the basic system functions

    I have already sorted all my priorities and ordered them from lowest to highest, and i can run them in that order, but i'm having trouble with the implementation of the algorithm i want to use
    I don't understand why you can't define new functions, but fine... You just need to add an outer loop like already said in a previous post, here's the outline:
    Code:
    for(each priority) {
      do {
        for(all process at this priority) {
          run(process)
        }
      } while(there is any process at this priority still alive);
    }
    Last edited by root4; 01-17-2012 at 02:57 AM.

  15. #15
    Registered User spendotw's Avatar
    Join Date
    Dec 2011
    Location
    England
    Posts
    40
    *sigh* as i said im a noob to C, I have tried to implement what has been said but this is still not working my awful code bellow decrements the priority member variable once but thats it, it doesn't loop back to the first element of that priority value.

    I'm sorry to those who think i should make my own function, your probably right but i cant for what i have to do, and plus ill be going on myself seeing as i have already sorted all my priorities out.

    I don't quit undestand how i am suppose to do it, the syntax of the example is incorrect and so I have to practically research everything myself. I know what i want to do myself as i said earlier, i just need someone to alter my loops so that it works
    Code:
    while(needToContinue)
    {
    needToContinue = false;
    
    	for(num_procs=0;num_procs<199;++num_procs)//loop through the array
    	{
    	while (proc[num_procs].priority) {
    	do { 
    		while (proc[num_procs].priority == 0 ) 
    		{
    			proc[num_procs].quantum--; // this seems to be doing nothing 
    			printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
    	 	} 
    	   } 	while (proc[num_procs].quantum > 0 ); // ensure theres quatum left
    	}	
    		if(proc[num_procs].quantum > 0)
    		{
    		proc[num_procs].quantum--;
    		printf("%s %d %d \n",proc[num_procs].id, proc[num_procs].quantum, proc[num_procs].priority);
    		}
    	}
    }
    And yes this code is probably wrong, as i guessed from the last if loop which repeats itself. But i have no idea what to do and this has given me the most ( even though its nothing ) progress

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating the lowest and highest values
    By Charak in forum C Programming
    Replies: 8
    Last Post: 01-30-2011, 08:57 PM
  2. Replies: 22
    Last Post: 11-13-2009, 04:51 PM
  3. Displaying highest and lowest values
    By beastofhonor in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2006, 07:24 PM
  4. getting two lowest values in array by index
    By danielC++ in forum C++ Programming
    Replies: 8
    Last Post: 03-09-2005, 06:30 PM
  5. Finding lowest value
    By titleist_03 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2004, 07:11 PM

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