Skip to content

Instantly share code, notes, and snippets.

@oriapp
Created January 15, 2023 10:33
Show Gist options
  • Save oriapp/e7f6e000309796f100a36fac3c4f319a to your computer and use it in GitHub Desktop.
Save oriapp/e7f6e000309796f100a36fac3c4f319a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_TASKS 4 // number of tasks
// structure to represent a task
typedef struct task {
int id; // task id
int burstTime; // time required to complete the task
int remainingTime; // remaining time to complete the task
} task_t;
// comparison function for qsort()
int cmp(const void *a, const void *b) {
task_t *x = (task_t *)a;
task_t *y = (task_t *)b;
return x->burstTime - y->burstTime;
}
int main() {
// array of tasks
task_t tasks[NUM_TASKS] = {
{0, 10, 10},
{1, 5, 5},
{2, 8, 8},
{3, 12, 12}
};
// sort tasks by burst time
qsort(tasks, NUM_TASKS, sizeof(task_t), cmp);
int elapsedTime = 0; // elapsed time since start of scheduling
for (int i = 0; i < NUM_TASKS; i++) {
task_t *t = &tasks[i];
elapsedTime += t->burstTime; // increment elapsed time
printf("Task %d completed.\n", t->id);
}
printf("Total elapsed time: %d milliseconds\n", elapsedTime);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment