Search code examples
coperating-systemscheduling

Suggestion for Implementing Scheduling algorithms in c


Given n processes with their burst times and arrival times, the task is to find average waiting time and average turn around time using scheduling algorithms like FCFS, Round Robin, Shortest remaining time, etc.,

I am very much confused in selecting a Data Structure to implement these algorithms. I implemented using a separate array for each attribute, but the thing it is tedious and we need to write a lot of statements in c. I am thinking of a linked list which each node represents the all attributes. Is it a efficient one. Can you please suggest me a efficient data structure so that search and sorting will be easier.


Solution

  • First, create a data structure to contain the "impossible to know in practice" information about each process (start time, burst time, etc), add a "process state" field (to keep track of "not started, started or stopped"), and add a single "next" field to the structure; then create an array of these structures and fill in the information.

    Next create a general purpose function to find the "not started" process that has the earliest start time.

    For round robin; find the "not started" process that has the earliest start time, set "current time = first process start time", and set the process' "next" field to point to itself so that you have a circular linked list of one entry, and change the state of the process from "not started" to "started". Then find out which event happens next (will another process start and get added to the circular linked list before a task switch happens?) and handle that event (while advancing your "current time").

    For FCFS it's similar to round robin, except you don't bother having task switches until the currently running task stops (and could use a non-circular list instead of a circular list).

    For shortest remaining time, it's the same as FCFS except that when a process is started you use "remaining time" to figure out where to insert it into the list.