Question
#include #include /* Declare global variables & struct to hold process scheduling information */ struct node{ int id; int arrival; int total_cpu; int done; int
#include
/* Declare global variables & struct to hold process scheduling information */ struct node{ int id; int arrival; int total_cpu; int done; int start; int end; int turnaround; int sort_field; } *sched_table = NULL;
typedef struct node table_type;
int num_processes;
/*************************************************************************/ void print_table(table_type *table){ /* Declare local variables */ int i;
/* Print table header row */ printf("ID\tArrival\t... "); printf("---...");
/* Print valid contents of each row in a for-loop */ for (i = 0; i < num_processes; i++){ printf("%d\t%d\t... ", sched_table[i].id, ...); } /* for-loop */
return; }
/***************************************************************************/ void "PROCEDURE FOR OPTION #1"(){ /* Decalre local variables */ int i; ... /* Prompt for total number of processes */ printf("..."); scanf("%d", &num_processes);
/* Allocate memory for scheduling table */ sched_table(table_type*)calloc(num_processes, sizeof(table_type));
/* For each process */ for (i=...){ /* Prompt for process id, arrival time, and total CPU time */ printf("..."); scanf("%d", &...); ... }
/* Print tabel contents */ print_table(sched_table);
return; }
/******************************************************************************/ table_type *sort_table(table_type *local_table){ /* Selection sort, insertion sort, quick sort, buble sort, ... */ /* Google search on "Selection sort in C" */
/* Declare local variables */ int i, j; table_type temp;
/* Sort rows of table in increasing order based on formal parameter using some sorting algorithm */ for(i = 0; i < num_processes -1; i++){ for(j=i+1; j < num_processes; j++) if(local_table[i].sort_field > local_table[j].sort_field){ temp = local_table[i]; local_table[i] = local_table[j]; local_table[j] = temp; } /* if */
return local_table; } }
/******************************************************************************/ void "PROCEDURE FOR OPTION #2--FIFO"(){
/* Declare local variables */ int i; table_type *sorted_table;
/* Allocate memory for copy of table for sorting */ sort_table = calloc(num_processes, sizeof(table_type));
/* For each row in table, set sorting field to arrival field */ for(i=0...){ sched_table[i].sort_field = sched_table[i].arrival; }
/* Call "sort" function on original table and assign to table copy */ sorted_table = sort_table(sched_table);
/* Set field values for index 0 of sorted tables based on user input values */ sorted_table[0].start = sorted_table[0].arrival; sorted_table[0].end = sorted_table[0].start + sorted_table[0].total_cpu; sorted_table[0].turnaround = sorted_table[0].total_cpu; /* For each additional row of sorted table, set fields appropriately */ for(i = 1; i < num_processes; i++){ sorted_table[i].start = ...; /* max of the ending time ofr index i-1 and arrival time for in */ sorted_table[i].end = ...; /* sum of the start time and the total cpu time */ sorted_table[i].turnaround = ...; /* sum of the total cpu time + (difference between the start tiem and teh arrival time)*/ } /* for-loop */
/* Print sorted table and free memory */ print_tab(sorted_table); free(sorted_table);
return; }
/******************************************************************************/ void "PROCEDURE FOR OPTION #3--SJF"(){
/* Declare local variables */ int current_cycle = 0; table_type *sorted_table; int atleast_one = 0;
/* Allocate memory for copy of table for sorting */
/* Copy from option #2 up to here */
/* For each row in table, set sorting field to total CPU field, initialize other fields appropriately */ for(i = 0...){ sched_table[i].sort_field = sched_table[i].total_cpu; /* Call "sort" function on original table and assign to table copu */ ... /* Loop until all processes have been scheduled */ while(num_processed < num_processes){ atleast_one = 0;
/* For each process */ for(i = 0; ...){ /* If not already scheduled and arrival time is <= current cycle */ if(!sorted_table[i].done && (...)){ atleast_one =1; /* at least 1 process will be scheduled in for-loop */ /* Set sorted table fields appropiately */ sorted_table[i].done = 1; sort_table[i].start = ...; /* based on the current cycle */ sort_table[i].end = ...; /* sum of the start time and the total cpu time */ sort_table[i].turnaround = ...; /* sum of the total cpu time + (difference between the start time and the arrival time)*/
/* Update current cycle and increment number of processes completed */ current_cycle = ...; /* based on current index end time */ num_processed++; } /* if */ } /* for-loop */ if(atleast_one == 0) current_cycle++; } /* while-loop */
/* Print sorted table and free memory */ ... return; }
/******************************************************************************/ void "PROCEDURE FOR OPTION #4"(){ /* If the table is not NULL, free the meory */
return; }
/******************************************************************************/ int main(){ /* Declare local variables */
/* Until the user quits, print the menu, prompt for the menu choice, call the appropriate procedure */
return 1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Specification:The program mimics the execution of different processes under different scheduling algorithms. The simulation maintains a table that reflects the current state of the system,based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: 1) Enter parameters2) Schedule processes with the FIFO algorithm3) Schedule processes with the SJF algorithm4) Quit program and free memoryAssignment:Create a table to recordthe status of different processesbased on the following parameters:oid:the unique identifier of the processoarrival:the point in timewhen the process enters the ready list to be executed by the CPUototal_cpu:the amount of CPU time the process will consumebetween arrival and departureodone:a flag that indicates whether the process has been successfully completed (1) or not (0)ostart_time:the time when the process has begun being executed by the CPUoend_time:the time when the process has been completed by the CPUoturnaround_time:the sum of the total CPU time and the waiting timeCalculate the values for thestart_time, end_time, and turnaround_timefor each process based on the selected scheduling algorithm.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started