#include #define NUM_GROUPS 3 #define NUM_TASKS 6 struct task_struct { int pid; int gid; int time_slice; struct task_struct *next; }; struct group_struct { int num_tasks; struct task_struct *head; struct task_struct *tail; }; struct task_struct task[NUM_TASKS]; // task array struct group_struct group[NUM_GROUPS]; // group queues int next_group = 0; // next group to be scheduled /* add a new task at the tail of the group queue */ void enqueue(struct task_struct *p) { // add your own code here } /* remove the task pointed by p from its group queue */ /* Note: p may not be the head of queue in sched_exit()! */ void dequeue(struct task_struct *p) { // add your own code here } /* move the task from the head to the tail of the group queue */ void requeue(struct task_struct *p) { // add your own code here } void print_stat(int round) { int i, j; struct task_struct *p; if (round) printf("\n\n"); printf("************Round %d************\n", round); printf("GROUP_ID TASK_ID TIME_LEFT\n"); for (i = 0; i < NUM_GROUPS; i++) { p = group[i].head; if (i && group[i].num_tasks) printf("-------------------------------\n"); for (j = 0; j < group[i].num_tasks; j++) { printf("%8d %7d %9d\n", p->gid, p->pid, p->time_slice); p = p->next; } } } /* return 1 if all group queues are empty */ int empty() { int i; for (i = 0; i < NUM_GROUPS; i++) { if (group[i].num_tasks) return 0; } return 1; } int main() { int run_time[NUM_TASKS] = {250, 100, 100, 260, 200, 180}; int group_id[NUM_TASKS] = {1024, 1024, 1024, 1025, 1026, 1026}; int i, round; struct task_struct *next_task; /* Initialize our group queues */ for (i = 0; i < NUM_GROUPS; i++) { group[i].num_tasks = 0; group[i].head = group[i].tail = NULL; } /* Initialize all the tasks and enqueue to different groups */ for (i = 0; i < NUM_TASKS; i++) { task[i].pid = i; task[i].gid = group_id[i]; task[i].time_slice = run_time[i]; task[i].next = NULL; enqueue(&task[i]); } /* Group-based Fair Share Scheduling */ round = 0; while (!empty()) { /* print scheduling information */ if (round % NUM_GROUPS == 0) { print_stat(round / NUM_GROUPS); printf("Tasks to run: "); } round++; /* if group queue is empty, skip it */ if (group[next_group].num_tasks) { /* choose the next runnable task (head of the next_group) */ next_task = group[next_group].head; printf("%d ", next_task->pid); next_task->time_slice -= 100; if (next_task->time_slice <= 0) dequeue(next_task); else requeue(next_task); } /* choose the next group queue to be scheduled */ next_group = (next_group + 1) % NUM_GROUPS; } printf("\n"); return 0; }