Skip to content

Instantly share code, notes, and snippets.

@ashraf267
Created March 1, 2023 04:08
Show Gist options
  • Select an option

  • Save ashraf267/146c504811ce848f84ad7950b37ef7bd to your computer and use it in GitHub Desktop.

Select an option

Save ashraf267/146c504811ce848f84ad7950b37ef7bd to your computer and use it in GitHub Desktop.
first-come-first-serve scheduling algorithm v1
void main() {
// tasks
// task a
// prints even nos between 1 - 100000
// exec. time: to be calculated
// arrival time: roughly 5s
// task b
// prints even nos between 1 - 1000000
// exec. time: to be calculated
// arrival time: 0s
// task c
// prints even nos between 1 - 10000000
// exec. time: to be calculated
// arrival time: roughly 10s
// call fcfs function
fcfs([
Task('a', 5),
Task('b', 0),
Task('c', 10),
]);
}
// first-come-first-serve function
void fcfs(List<Task> tasks) {
// do magic!
// tasks is an array of task
final sortedArrTimes = List.filled(3, 0, growable: true);
for(int i = 0; i < tasks.length; i++) {
sortedArrTimes[i] = tasks[i].arrivalTime;
sortedArrTimes.sort((a, b) => a.compareTo(b)); // ascending order
tasks[i].doTask(sortedArrTimes[i]);
}
}
// Task class
class Task {
String taskName; // a or b or c
int arrivalTime; // in seconds
// constructor
Task(this.taskName, this.arrivalTime);
// doTask method
// this should be replaced to support polymorphism
void doTask(int taskId) {
if (taskId == 5) {
// do task a
print("Task a completed");
} else if(taskId == 0) {
// do task b
print("Task b completed");
} else {
// do task c
print("Task c completed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment