Write a program in C that simulates a CPU scheduler. Implement a CPU scheduling
ID: 3750021 • Letter: W
Question
Write a program in C that simulates a CPU scheduler.
Implement a CPU scheduling simulator. The program must read from a file the following information: number of processes, process arrival order, CPU burst per process, and priority per process. Command line parameters will select which scheduling algorithm to use and the quantum value (if required). The following command line arguments can occur in any order on the command line. All other command line options should be considered file inputs. Your program needs to only handle processing the first file encountered.
The scheduling simulator must be implemented in C and use each of the following algorithms: FCFS, SJF, Priority and Round-Robin.
The quantum for Round-Robin is required, and only for that algorithm.
The file format provides all of the information for the processes that will need to be scheduled. The first line of the file contains a single number identifying the number of processes in the file that you will be scheduling (4 in the example below). The number of processes will range from 1 to an undefined upper bound (your program will need to handle a large number of processes). You may assume that the file is formatted correctly. The subsequent lines in the file each describe one process indicating the process identifier, the CPU burst length, and the priority of the process. For example, the 4 3 7, in the example below, indicates that process 4 has a CPU burst of 3 and priority 7. A lower number means higher priority. You may assume that the process identifiers are integers, but do not assume that the process identifiers are contiguous starting at 1. For example, you may be processing a file with the following set of five processes: 12, 24, 23, 103, 99. The order of the processes in the file is their arrival order. In the example below, the processes arrive in the following order: 4, 2, 3, 1.
Example Output:
4
4 3 7
2 3 10
3 5 7
1 7 1
Explanation / Answer
FCFS
#include<stdio.h>
// Function to calculate the waiting time for all processes
void calculateWaitingTime(int processes[], int num, int burstTime[], int waitingTime[])
{
int counter;
// waiting time for first process is 0
waitingTime[0] = 0;
// Loops till number of processes
for (counter = 1; counter < num; counter++ )
// Calculate waiting time for each process
waitingTime[counter] = burstTime[counter - 1] + waitingTime[counter - 1] ;
}// End of function
// Function to calculate turn around time
void calculateTurnAroundTime( int processes[], int num, int burstTime[], int waitingTime[], int turnAroundTime[])
{
int counter;
// Loops till number of processes
for (counter = 0; counter < num; counter++)
// Calculate turnaround time by adding burstTime[counter] + waitingTime[counter]
turnAroundTime[counter] = burstTime[counter] + waitingTime[counter];
}// End of function
// Function to calculate average time
void calculateAvgTime( int processes[], int num, int burstTime[])
{
int waitingTime[num], turnAroundTime[num], totalWaitingTime = 0, totalTAT = 0;
int counter;
// Calls the function to calculate waiting time of all processes
calculateWaitingTime(processes, num, burstTime, waitingTime);
// Calls the function to calculate turn around time for all processes
calculateTurnAroundTime(processes, num, burstTime, waitingTime, turnAroundTime);
// Display heading
printf("Processes Burst time Waiting time Turn around time");
// Loops till number of processes to calculate total waiting time and total turn around time
for (counter = 0; counter < num; counter++)
{
totalWaitingTime += waitingTime[counter];
totalTAT += turnAroundTime[counter];
printf(" %d %d %d %d", counter + 1, burstTime[counter], waitingTime[counter], turnAroundTime[counter]);
}// End of for loop
// Displays avg waiting and TAT
printf(" Average waiting time = %.2f", (float)totalWaitingTime / (float)num);
printf(" Average turn around time = %.2f", (float)totalTAT / (float)num);
}// End of function
// main function definition
int main()
{
// Creates an array for process ID
int processes[] = {4, 3, 2, 1};
// Calculates number of processes
int numberOfProcess = sizeof processes / sizeof processes[0];
// Creates an array for burst time
int burstTime[] = {3, 3, 5, 7};
// Calls the function to calculate average time
calculateAvgTime(processes, numberOfProcess, burstTime);
return 0;
}// End of main function
Sample Output:
Processes Burst time Waiting time Turn around time
1 3 0 3
2 3 3 6
3 5 6 11
4 7 11 18
Average waiting time = 5.00
Average turn around time = 9.50
SJF
#include<stdio.h>
// Defines a structure for process
struct ProcessData
{
// To store process ID
int processID;
// To store burst time
int burstTime;
};// End of structure
// Function to calculate the waiting time for all processes
void calculateWaitingTime(struct ProcessData process[], int num, int waitingTime[])
{
int counter;
// Store waiting time for first process is 0
waitingTime[0] = 0;
// Loops till number of processes
for (counter = 1; counter < num ; counter++ )
// Calculating waiting time for each process
waitingTime[counter] = process[counter - 1].burstTime + waitingTime[counter - 1] ;
}// End of function
// Function to calculate turn around time
void calculateTurnAroundTime(struct ProcessData process[], int num, int waitingTime[], int turnAroundTime[])
{
int counter;
// Loops till number of process
for (counter = 0; counter < num; counter++)
// Calculates turnaround time by adding burstTime[counter] + waitingTime[counter]
turnAroundTime[counter] = process[counter].burstTime + waitingTime[counter];
}// End of function
// Function to calculate average time
void calculateAvgTime(struct ProcessData process[], int num)
{
int counter;
int waitingTime[num], turnAroundTime[num], totalWaitingTime = 0, totalTAT = 0;
// Calls the function to calculate waiting time of all processes
calculateWaitingTime(process, num, waitingTime);
// Calls the function to calculate turn around time for all processes
calculateTurnAroundTime(process, num, waitingTime, turnAroundTime);
// Display the heading
printf(" Processes Burst time Waiting time Turn around time");
// Loops till number of process
for (counter = 0; counter < num; counter++)
{
// Calculate total waiting time and total turn around time
totalWaitingTime += waitingTime[counter];
totalTAT += turnAroundTime[counter];
// Displays each process information
printf(" %d %d %d %d", process[counter].processID, process[counter].burstTime, waitingTime[counter], turnAroundTime[counter]);
}// End of for loop
// Displays average waiting time and turn around time
printf(" Average waiting time = %.2f", (float)totalWaitingTime / (float)num);
printf(" Average turn around time = %.2f", (float)totalTAT / (float)num);
}// End of function
// Function to sort process based on burst time
void sortBurstTime(struct ProcessData process[], int num)
{
// Declares a temporary object to swap
struct ProcessData temp;
int r, c;
// Loops till number of process minus one times
for(r = 0; r < num -1; r++)
{
// Loops till number of process minus outer loop value minus one times
for(c = 0; c < num - r -1; c++)
{
// Checks if current process burst time is greater than the next process burst time
if(process[c].burstTime > process[c+1].burstTime)
{
// Swapping process
temp = process[c];
process[c] = process[c+1];
process[c+1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function
// main function definition
int main()
{
int counter;
// Creates an array of objects for process id and burst time
struct ProcessData process[] = {{4, 3}, {2, 3}, {3, 5}, {1, 7}};
// Calculates number of process
int numberOfProcess = sizeof process / sizeof process[0];
// Calls the function to sort the processes by burst time.
sortBurstTime(process, numberOfProcess);
// Display the order or process
printf(" Order in which process gets executed ");
// Loops till number of process
for (counter = 0; counter < numberOfProcess; counter++)
// Displays the process id
printf("%5d", process[counter].processID);
// Calls the function to calculate and display average
calculateAvgTime(process, numberOfProcess);
return 0;
}// End of main function
Sample Output:
Order in which process gets executed
4 2 3 1
Processes Burst time Waiting time Turn around time
4 3 0 3
2 3 3 6
3 5 6 11
1 7 11 18
Average waiting time = 5.00
Average turn around time = 9.50
Priority
#include <stdio.h>
using namespace std;
// Defines a structure for process
struct ProcessData
{
// To store process id
int processID;
// To store burst time
int burstTime;
// To store priority
int priority;
};// End of structure
// Function to calculate the waiting time for all processes
void calculateWaitingTime(struct ProcessData process[], int num, int waitingTime[])
{
int counter;
// Stores waiting time for first process is 0
waitingTime[0] = 0;
// Loops till number of process
for (counter = 1; counter < num; counter++)
// Calculates waiting time
waitingTime[counter] = process[counter - 1].burstTime + waitingTime[counter - 1];
}// End of function
// Function to calculate turn around time
void calculateTurnAroundTime(struct ProcessData process[], int num, int waitingTime[], int turnAroundTime[])
{
int counter;
// Loops till number of process
for (counter = 0; counter < num; counter++)
// Calculates turnaround time by adding burstTime[counter] + waitingTime[counter]
turnAroundTime[counter] = process[counter].burstTime + waitingTime[counter];
}// End of function
// Function to calculate average time
void calculateAvgTime(struct ProcessData process[], int num)
{
int counter;
int waitingTime[num], turnAroundTime[num], totalWaitingTime = 0, totalTAT = 0;
// Calls the function to calculate waiting time of all processes
calculateWaitingTime(process, num, waitingTime);
// Calls the function to calculate turn around time for all processes
calculateTurnAroundTime(process, num, waitingTime, turnAroundTime);
// Display heading
printf(" Processes Burst time Waiting time Turn around time");
// Loops till number of process
for (counter = 0; counter < num; counter ++)
{
// Calculates total waiting time and total turn around time
totalWaitingTime += waitingTime[counter];
totalTAT += turnAroundTime[counter];
// Displays each process information
printf(" %d %d %d %d", process[counter].processID, process[counter].burstTime, waitingTime[counter], turnAroundTime[counter]);
}// End of for loop
// Displays the average information
printf(" Average waiting time = %.2f", (float)totalWaitingTime / (float)num);
printf(" Average turn around time = %.2f", (float)totalTAT / (float)num);
}// End of function
// Function to sort process based on priority
void sortPriority(struct ProcessData process[], int num)
{
// Declares a temporary object to swap
struct ProcessData temp;
int r, c;
// Loops till number of process minus one times
for(r = 0; r < num -1; r++)
{
// Loops till number of process minus outer loop value minus one times
for(c = 0; c < num - r -1; c++)
{
// Checks if current process priority is greater than the next process priority
if(process[c].priority > process[c+1].priority)
{
// Swapping process
temp = process[c];
process[c] = process[c+1];
process[c+1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function
// Function performs priority scheduling
void priorityScheduling(struct ProcessData process[], int num)
{
int counter;
// Sort processes by priority
sortPriority(process, num);
printf(" Order in which processes gets executed");
// Loops till number of process
for (counter = 0; counter < num; counter++)
printf("%5d", process[counter].processID);
// Calls the function to calculate and display averages
calculateAvgTime(process, num);
}// End of function
// main function definition
int main()
{
// Creates an array of objects to store process id, burst time and priority
struct ProcessData process[] = {{4, 3, 7}, {2, 3, 10}, {3, 5, 7}, {1, 7, 1}};
// Calculates number of process
int numberOfProcess = sizeof process / sizeof process[0];
// Calls the function to perform scheduling
priorityScheduling(process, numberOfProcess);
return 0;
}// End of main function
Sample Output:
Order in which processes gets executed 1 4 3 2
Processes Burst time Waiting time Turn around time
1 7 0 7
4 3 7 10
3 5 10 15
2 3 15 18
Average waiting time = 8.00
Average turn around time = 12.50
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.