mlfq.c // Note: Necessary header files are included #define GNUSOURCE #include <stdio.h>…
- User
mlfq.c
// Note: Necessary header files are included #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
// Define MAX_NUM_PROCESS // For simplicity, assume that we have at most 10 processes #define MAX_NUM_PROCESS 10
#define MAX_PROCESS_NAME 5 #define MAX_GANTT_CHART 300
// N-level Feedback Queue (N=1,2,3,4) #define MAX_NUM_QUEUE 4
// Keywords (to be used when parsing the input) #define KEYWORD_QUEUE_NUMBER "queue_num" #define KEYWORD_TQ "time_quantum" #define KEYWORD_PROCESS_TABLE_SIZE "process_table_size" #define KEYWORD_PROCESS_TABLE "process_table"
// Assume that we only need to support 2 types of space characters: // " " (space), "\t" (tab) #define SPACE_CHARS " \t"
// Process data structure // Helper functions: // process_init: initialize a process entry // process_table_print: Display the process table struct Process { char name[MAX_PROCESS_NAME]; int arrival_time ; int burst_time; int remain_time; // remain_time is needed in the intermediate steps of MLFQ }; void process_init(struct Process* p, char name[MAX_PROCESS_NAME], int arrival_time, int burst_time) { strcpy(p->name, name); p->arrival_time = arrival_time; p->burst_time = burst_time; p->remain_time = 0; } void process_table_print(struct Process* p, int size) { int i; printf("Process\tArrival\tBurst\n"); for (i=0; i<size; i++) { printf("%s\t%d\t%d\n", p[i].name, p[i].arrival_time, p[i].burst_time); } }
// A simple GanttChart structure // Helper functions: // gantt_chart_print: display the current chart struct GanttChartItem { char name[MAX_PROCESS_NAME]; int duration; };
void gantt_chart_print(struct GanttChartItem chart[MAX_GANTT_CHART], int n) { int t = 0; int i = 0; printf("Gantt Chart = "); printf("%d ", t); for (i=0; i<n; i++) { t = t + chart[i].duration;
printf("%s %d ", chart[i].name, t); } printf("\n"); }// Global variables int queue_num = 0; int process_table_size = 0; struct Process process_table[MAX_NUM_PROCESS]; int time_quantum[MAX_NUM_QUEUE];
// Helper function: Check whether the line is a blank line (for input parsing) int is_blank(char *line) { char *ch = line; while ( *ch != '\0' ) { if ( !isspace(*ch) ) return 0; ch++; } return 1; } // Helper function: Check whether the input line should be skipped int is_skip(char *line) { if ( is_blank(line) ) return 1; char *ch = line ; while ( *ch != '\0' ) { if ( !isspace(*ch) && *ch == '#') return 1; ch++; } return 0; } // Helper: parse_tokens function void parse_tokens(char **argv, char *line, int *numTokens, char *delimiter) { int argc = 0; char *token = strtok(line, delimiter); while (token != NULL) { argv[argc++] = token; token = strtok(NULL, delimiter); } *numTokens = argc; }
// Helper: parse the input file void parse_input() { FILE *fp = stdin; char *line = NULL; ssize_t nread; size_t len = 0;
code - Assistant