Here is the code: I can't get it to work. I need a function that finds the maximum of two integers.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* declare global variables including a table structure to hold scheduling
information */
/* optional: define a function that finds the maximum of two integers */
int total_processes = 0;
struct node{
int id;
int arrival;
int total_cycles;
int total_remaining;
int done;
int start;
int already_started;
int end;
int turnaround;
}* table = NULL;
typedef struct node table_type;
/***************************************************************/
void print_table() {
/*
1.declare local variables
2. print table header
3. for each process
4. print the contents (id, arrival time, total_cycles) of
each field of the table's index
5. if process has been scheduled ("done" field is 1,
6. print other contents (start time, end time, turnaround time)
*/
printf("ID/tArrival/tTotal/tStart/tEnd/tTurnaround/n");
printf("--------------------------------------------\n");
int i;
for(i=0; i < total_processes; i++){
printf("%d\t", table[i].id);
printf("%d\t", table[i].arrival);
printf("%d\t", table[i].total_cycles);
printf("%d\t", table[i].start);
printf("%d\t", table[i].end);
printf("%d\t", table[i].turnaround);
}
return;
}
/***************************************************************/
void procedure_1() {
/*"PROCEDURE FOR OPTION #1"
/* declare local variables */
/* prompt for total number of processes */
/* allocate memory for table to hold process parameters */
/* for each process */
/* prompt for process id, arrival time, and total cycle
time */
/* print contents of table */
int i;
printf("Enter total number of processes: ");
scanf("%d", &table[i].arrival);
table = (table_type *)malloc(total_processes * sizeof(table_type));
for(i = 0; i < total_processes; i++){
int arrival = -1;
int total_cycles = -1;
printf("Enter process id: ");
scanf("%d", &table[i].id);
}
printf("Enter arrival cycle for process P[%d]: ", i);
scanf("%d", &table[i].total_cycles);
/*
table[i].id = i;
table[i].arrival = -1;
table[i].total_cycles = -1;
table[i].start = -1;
table[i].end = -1;
table[i].turnaround = -1;
*/
print_table();
return;
}
/***************************************************************/
void procedure_2() {
/* "PROCEDURE FOR OPTION #2"
/* declare (and initilize when appropriate) local variables */
/* for each process, reset "done" field to 0 */
/* while there are still processes to schedule */
/* initilize the earliest arrival time to INT_MAX
(largest integer value) */
/* for each process not yet scheduled */
/* check if process has earlier arrival time
than current earliest and update */
/* set start time, end time, turnaround time, done fields
for unscheduled process with earliest arrival time */
/* update current cycle time and increment number of
processes scheduled */
/* print contents of table */
int i;
int done;
int min_value;
int current_cycle = 0;
int max;
int min_index;
table[i].done = 0;
while(done < 1){
min.
Here is the code- I can't get it to work- I need a function that finds.pdf
1. Here is the code: I can't get it to work. I need a function that finds the maximum of two integers.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* declare global variables including a table structure to hold scheduling
information */
/* optional: define a function that finds the maximum of two integers */
int total_processes = 0;
struct node{
int id;
int arrival;
int total_cycles;
int total_remaining;
int done;
int start;
int already_started;
int end;
int turnaround;
}* table = NULL;
typedef struct node table_type;
/***************************************************************/
void print_table() {
/*
1.declare local variables
2. print table header
3. for each process
4. print the contents (id, arrival time, total_cycles) of
each field of the table's index
5. if process has been scheduled ("done" field is 1,
6. print other contents (start time, end time, turnaround time)
*/
printf("ID/tArrival/tTotal/tStart/tEnd/tTurnaround/n");
printf("--------------------------------------------n");
int i;
for(i=0; i < total_processes; i++){
printf("%dt", table[i].id);
printf("%dt", table[i].arrival);
printf("%dt", table[i].total_cycles);
2. printf("%dt", table[i].start);
printf("%dt", table[i].end);
printf("%dt", table[i].turnaround);
}
return;
}
/***************************************************************/
void procedure_1() {
/*"PROCEDURE FOR OPTION #1"
/* declare local variables */
/* prompt for total number of processes */
/* allocate memory for table to hold process parameters */
/* for each process */
/* prompt for process id, arrival time, and total cycle
time */
/* print contents of table */
int i;
printf("Enter total number of processes: ");
scanf("%d", &table[i].arrival);
table = (table_type *)malloc(total_processes * sizeof(table_type));
for(i = 0; i < total_processes; i++){
int arrival = -1;
int total_cycles = -1;
printf("Enter process id: ");
scanf("%d", &table[i].id);
}
printf("Enter arrival cycle for process P[%d]: ", i);
scanf("%d", &table[i].total_cycles);
/*
table[i].id = i;
table[i].arrival = -1;
table[i].total_cycles = -1;
table[i].start = -1;
table[i].end = -1;
table[i].turnaround = -1;
*/
print_table();
return;
3. }
/***************************************************************/
void procedure_2() {
/* "PROCEDURE FOR OPTION #2"
/* declare (and initilize when appropriate) local variables */
/* for each process, reset "done" field to 0 */
/* while there are still processes to schedule */
/* initilize the earliest arrival time to INT_MAX
(largest integer value) */
/* for each process not yet scheduled */
/* check if process has earlier arrival time
than current earliest and update */
/* set start time, end time, turnaround time, done fields
for unscheduled process with earliest arrival time */
/* update current cycle time and increment number of
processes scheduled */
/* print contents of table */
int i;
int done;
int min_value;
int current_cycle = 0;
int max;
int min_index;
table[i].done = 0;
while(done < 1){
min_value = INT_MAX;
if(table[i].done){
if(table[i].arrival < min_value){
min_value = 0;
min_index = 0;
}
} /*for loop*/
table[min_index].start = max(current_cycle, table[min_index].arrival);
table[min_index].end = table[min_index].start + table[min_index].total_cycles;
table[min_index].turnaround = table[min_index].end - table[min_index].total_cycles;
table[min_index].done = 1;
current_cycle = table[min_index].end;
done++;
4. }
print_table();
return;
}
/***************************************************************/
void procedure_3() {
/* "PROCEDURE FOR OPTION #3"
/* declare (and initilize when appropriate) local variables */
/* for each process, reset "done" field to 0 */
/* while there are still processes to schedule */
/* initilize the lowest total cycle time to INT_MAX
(largest integer value) */
/* for each process not yet scheduled */
/* check if process has lower total cycle
time than current lowest and has arrival time less than current cycle time
and update */
/* set start time, end time, turnaround time, done fields
for unscheduled process with lowest (and available) total cycle time */
/* update current cycle time and increment number of
processes scheduled */
/* print contents of table */
int done = 0;
int min_value;
int current_cycle = 0;
int min_index;
int i;
table[i].done = 0;
while(done < 1){
min_value = INT_MAX;
if(table[i].done == 0){
if((table[i].arrival < min_value) && (table[i].arrival <= current_cycle)){
min_value = 0;
min_index = 0;
}
}
table[min_index].start = 0;
table[min_index].end = 0;
table[min_index].turnaround = 0;
table[min_index].done = 0;
5. }
done++;
print_table();
return;
}
/***************************************************************/
void procedure_4 () {
/* "PROCEDURE FOR OPTION #4"
/* declare (and initilize when appropriate) local variables */
/* for each process, reset "done", "total_remaining" and
"already_started" fields to 0 */
/* while there are still processes to schedule */
/* initilize the lowest total remaining time to INT_MAX
(largest integer value) */
/* for each process not yet scheduled */
/* check if process has lower total remaining
time than current lowest and has arrival time less than current cycle time
and update */
/* check if process already partially-scheduled */
/* if so, set "start time", "already_started"
fields of process with lowest (and available) total remaining cycle time
*/
/* set end time, turnaround time of process with lowest
(and available) total remaining cycle time */
/* decrement total remaining time of process with lowest
(and available) total remaining cycle time */
/* if remaining time is 0, set done field to 1, increment
cycle time and number of scheduled processes*/
/* print contents of table */
int i;
int done = 0;
int min_value;
int min_index;
int current_cycle = 0;
table[i].done = 0;
table[i].total_remaining = 0;
table[i].already_started = 0;
while(done < 1){
min_value = INT_MAX;
if(table[i].done){
6. if((table[i].total_remaining < min_value) && (table[i].arrival <= current_cycle)){
min_value = 0;
min_index = 0;
}
}
}
if(table[min_index].already_started == 0){
table[min_index].start = 1;
table[min_index].start = 1;
table[min_index].end = current_cycle + 1;
table[min_index].turnaround = 1;
table[min_index].already_started = 1;
}
else{
table[min_index].end = current_cycle + 1;
table[min_index].turnaround = 1;
}
table[i].total_remaining = 1;
if(total_remaining == 0 && done == 1 ){
table[min_index].done = 1;
done++;
}
current_cycle++;
print_table();
return;
}
/***************************************************************/
void procedure_5() {
/* free the schedule table if not NULL */
if(table != NULL){
free(table);
}
return;
}
/***************************************************************/
int main() {
/* declare local vars */
/* while user has not chosen to quit */
/* print menu of options */
/* prompt for menu selection */
7. /* call appropriate procedure based on choice--use switch
statement or series of if, else if, else statements */
int choice;
while(choice != 5){
printf("Batching Schedulingn");
printf("--------------------------n");
printf("1) Enter parametersn");
printf("2) Schedule processses with FIFO algorithmn");
printf("3) Schedule processes with SJF algorithmn");
printf("4) Schdule processes with SRT algorithmn");
printf("5) Quit and free memoryn");
printf("n");
printf("Enter selection: ");
scanf("%d", &choice);
printf("n");
switch(choice){
case 1:
procedure_1();
break;
case 2:
procedure_2();
break;
case 3:
procedure_3();
break;
case 4:
procedure_4();
break;
case 5:
procedure_5();
break;
}
}
return 0;
}
COMP 322/L-Introduction to Operating Systems and System Architecture Assignment #2-Batch
Process Scheduling Objective: To calculate the timing parameters of batch processes based on
different scheduling algorithms. 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 parameters 2) Schedule processes
8. with FIFO algorithm 3) Schedule processes with SJF algorithm (non-preemptive) 4) Schedule
processes with SRT algorithm (preemptive version of SJF) 5) Quit program and free memory
Assignment: - Create a table to record the status of different processes based on the following
parameters: id: the unique identifier of the process - arrival: the point in time when the process
enters the ready list to be executed by the CPU - total_cpu: the amount of CPU time the process
will consume between arrival and departure - total_remaining: the amount of CPU time
remaining for a process to complete {used for SRT scheduling only - done: a flag that indicates
whether the process has been successfully completed (1) or not ( 0 ) - start time: the time when
the process has begun being executed by the CPU - already_started: a flag that indicated whether
the process has already begun (1) or not (0) {used for SRT scheduling only} - end_time: the
time when the process has been completed by the CPU - turnaround_time: the sum of the total
CPU time and the waiting time (alternatively: the difference between the end time and the arrival
time) - Calculate the values for the start_time, end_time, and turnaround_time for each process
based on the selected scheduling algorithm. Batch scheduling 1) Enter parameters 2) Schedule
processes with FIFO algorithm 3) Schedule processes with SJF algorithm 4) Schedule processes
with SRT algorithm 5) Quit and free memory Enter selection: 1 Enter total number of processes:
3 Enter process id: 1 Enter arrival cycle for process P [ 1 ] : 0 Enter total cycles for process P [ 1
] : 6 Enter process id: 2 Enter arrival cycle for process P [ 2 ] : 1 Enter total cycles for process P [
2 ] : 3 Enter process id: 3 Enter arrival cycle for process P [ 3 ] : 3 Enter total cycles for process
P [ 3 ] : 2 Batch scheduling 1) Enter parameters 2) Schedule processes with FIFO algorithm 3)
Schedule processes with SJF algorithm 4) Schedule processes with SRT algorithm 5) Quit and
free memory Enter selection: 2 Batch scheduling 1) Enter parameters 2) Schedule processes with
FIFO algorithm 3) Schedule processes with SJF algorithm 4) Schedule processes with SRT
algorithm 5) Quit and free memory Enter selection: 3 Batch scheduling 1) Enter parameters 2)
Schedule processes with FIFO algorithm 3) Schedule processes with SJF algorithm 4) Schedule
processes with SRT algorithm 5) Quit and free memory Enter selection: 4 Batch scheduling 1)
Enter parameters 2) Schedule processes with FIFO algorithm 3) Schedule processes with SJF
algorithm 4) Schedule processes with SRT algorithm 5) Quit and free memory Enter selection: 5
Quitting program...