Starting with your code from the last message:
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);
}
}
}
I changed the indentation to make it readable to me. You did not have a consistent indentation style. You may not like mine; fine. But you need a consistent one. With a new indentation it looks like:
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);
}
}
}
So you have four layers of loop structures. Most people cannot manage code with four layers of loops.
But lets see what we can learn from some simple restructuring. I am NOT trying to change the function of the code. I am going to show you my thinking as we go.
I am NOT testing these changes so I could easily make an error.
I am not trying to fix this code, because I do not understand what you are trying to do. I do not think you have given a clear explanation of what you are trying to do.
Code:
while(needToContinue)
{
needToContinue = false;
.
.
.
}
OK, this is outter most loop. Unless, I missed something, needToContinue is not set anywhere else inside the loop. So this loop will execute 0 or 1 times depending on the initial value of needToContinue. Well, 0 or 1 times sounds like an if statement.
Code:
if (needToContinue)
{
.
.
.
}
Inside the if statement, we have this for loop.
Code:
for(num_procs=0;num_procs<199;++num_procs)//loop through the array
{
.
.
.
}
It contains a while loop and an if statement. That for loop seems straight forward. Let's look at the while loop.
Code:
while (proc[num_procs].priority) {
.
.
.
}
This while loop executes 0 times if proc[num_procs].priority is 0. Since this value is not changed inside the loop, if it is not zero this will be an infinite loop unless the value of the .priority changes during the loop or if there is a break or goto statement in the loop.
Since it is an infinite loop, the if statement that follows it will never be executed.
Inside the probably infinite while loop, we have a do ... while loop. And inside it is a while loop. It is only inside that inner while loop that we decrement the proc[num_procs].quantum which controls this loop. Since this is a do ... while loop we will execute at least one time.
Code:
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
Now to look at that inner while loop. It executes 0 times if proc[num_procs].priority is not 0 and probably infinite times if it is zero. But from the outer while loop that we looked at earlier (the one that probably is infinite), we know that proc[num_procs].priority is not zero. So the contents of this while loop will not execute. That would explain why the decrement statement that you put the comment on saying "this seems to be doing nothing" is correct because it never gets executed.
I hope this helps you to read your own code. And it provides a beginning to restructuring your code to do what you really want (whatever that is).