I'm sure there are many bugs (starting with not caring if the user wants to enter 11 or more processes even though your array of processes is limited to 10).
However; I spent 10 minutes trying to decipher your code and still don't really know what it thinks it's doing - there's no comments at all and the variable names and function names don't help (e.g. no
is not a boolean "yes/no" variable, checkprocess()
doesn't check one process but checks all processes to see if all processes have finished, etc). Mostly, if I were being paid to fix this code I'd simple throw it out and rewrite it from scratch to save time. I thought about rewriting it from scratch and just posting the resulting code; but that's not going to help you with your homework.
My advice is, rewrite it from scratch instead of fixing it.
It should have a global currently_running_process
variable, a global current_time
variable, one function to increase the current time, and one function for the scheduler itself.
The function to increase the current time would:
- for each process on the scheduler's linked list, increase the waiting time
- do
current_time++
- find any processes that should be started (
current_time == arrival_time
) and append any started processes to the end of the scheduler's linked list
The scheduler function should:
- remove the first process from the scheduler's linked list
- determine how much time that process should use (the time slice length or the process' remaining time, whichever is lower)
- subtract that amount of time from the process' remaining time
- call the
increase_time()
function in a loop, until that amount of time has passed - if the process' remaining time is not zero; put the process back onto the end of the linked list
- if the process` remaining time was zero, check if the scheduler's linked list is empty and exit the program if it is
Note: I'd start with current_time = -1;
and call the function to increase the current time once before calling the scheduler function; so that any processes with arrival_time == 0
would be added to the scheduler's linked list before the scheduler starts working (and so that the scheduler function doesn't see an empty list as soon as it's started).