In: Computer Science
Please write in C using linux or unix.
Write a program that will simulate non - preemptive process scheduling algorithm:
First Come – First Serve
Your program should input the information necessary for the calculation of average turnaround time including:
The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time.
Step 1: generate the input data (totally 10 jobs) and save it in the array of structure composing the arrival time, service time, termination time, turnaround time. The service time follows the uniform distribution in the range of [5, 25], and the arrival time is generated by uniform distribution in the range of [0,10] accumulated based on the previous arrival time.
Step 2: program FCFS algorithm.
Note: be careful about the situation that one job is finished while the next job is not arrived yet, so you have the idle time between them.
Step 3: output
print out one line for each job with arrival time, start time, service, termination time, turnaround time, finally average turnaround time in the last line.
ANSWER:
#include<iostream>
using namespace std;
// Function to find the waiting time
void Waitingtime(int processes[], int n,
int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time for other processes
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
// Function to calculate turn around time for each process
void TAtime( int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding burstTime with waitingTime
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void AVGtime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
Waitingtime(processes, n, bt, wt);
TAtime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
int main()
{
//process id's
int processes[] = { 1, 2, 3,4};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {10, 5, 8,6};
AVGtime(processes, n, burst_time);
return 0;
}