Write a program to read a file which has an algorithm name (FCFS or RR), a set o
ID: 3588958 • Letter: W
Question
Write a program to read a file which has an algorithm name (FCFS or RR), a set of processes with running times and arrival times: Please in C++ and can be used/compiled/ran in Ubuntu, if that makes a difference.
– Determine Schedule
– Calculate Total, Average Waiting Time
– Calculate Total, Average Turnaround time
– Calculate Throughput
Here is a sample input file. The file can have different number of processes, the example shows only 5.
Name: FCFS
Q: 10
NumofProc: 5
Proc_name Proc_time Arrival_time
P1 20 0
P2 29 3
P3 7 3
P4 15 10
P5 58 15
Explanation / Answer
#include <iostream>
#include <fstream>
#include<cstdlib>
using namespace std;
void findWaitingTime(int processes[], int n,
int bt[], int wt[],int arrival[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1]-arrival[i] ;
}
// Function to calculate turn around time
void findTurnAroundTime( int processes[], int n,
int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int n, int bt[],int arrival[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt,arrival);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time ";
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << " " << bt[i] <<" "
<< wt[i] <<" " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << " Average turn around time = "
<< (float)total_tat / (float)n;
}
int toint(int i,char str[])
{
int c=0;
while(str[i]!=' ')
{
c=c*10+(int)str[i]-48;
i++;
}
//cout<<"C:"<<c<<endl;
return c;
}
int main()
{
ifstream in("C:/Users/Surya/Desktop/input.txt");//change this path ...according to ur file name..
if(!in) {
cout << "Cannot open input file. ";
return 1;
}
char str[255];
int algo=1;
int k=0;
int quantum;
int n_p;
int l=0;
int arrival[100],burst[100];//arrays to store arrival and burst,,,
while(in) {
in.getline(str, 255); // delim defaults to ' '
// if(in) cout << str << endl;
int i=0;
if(k==0)
{
while(str[i]!='')
{
if(str[i]==' '&&str[i+1]=='F')//finding which algorithm it is
{
cout<<"Algorithm : FCFS ";
algo=0;
}
i++;
}
k++;
}
else if(k==1)
{
while(str[i]!='')//reading quanta
{
if(str[i]==' ')
{
quantum = atoi(&str[i+1]);
cout<<"Quantum is :"<<quantum<<endl;
break;
}
i++;
}
k++;
}
else if(k==2)
{
while(str[i]!='')//reading number of processes
{
if(str[i]==' ')
{
n_p = atoi(&str[i+1]);
cout<<"Number of processes :"<<n_p<<endl;
break;
}
i++;
}
k++;
}
else if(k==3)
{
k++;
}
else//reading values,,
{
int val=0;
while(str[i]!='')
{
if(str[i]==' '&&str[i+1]!=' ')
{
if(val==0)
{burst[l] = toint(i+1,str);
val=1;
}
else
{
cout<<str[i+1]<<endl;
arrival[l]=atoi(&str[i+1]);
}
}
i++;
}
l++;
}
}
//printing readedvalues,,,
cout<<"Process arrival burst ";
int i=0;
while(i<n_p)
{
int a=arrival[i];
int b=burst[i];
cout<<"P"<<(i+1)<<" "<<a<<" "<<b<<endl;
i++;
}
int p[n_p];//processes...
in.close();
if(algo==0)//printint schedule of processss
{
cout<<"Schedule:";
i=0;
while(i<n_p)
{
p[i]=i+1;
cout<<"P"<<i+1<<" ";
i++;
}
cout<<" ";
//calling findavgtime
findavgTime(p, n_p, burst,arrival);
}
return 0;
}
output:
Algorithm : FCFS
Quantum is :10
Number of processes :5
0
3
3
1
1
Process arrival burst
P1 0 20
P2 3 29
P3 3 7
P4 10 25
P5 15 58
Schedule:P1 P2 P3 P4 P5
Processes Burst time Waiting time Turn around time
1 20 0 20
2 29 17 46
3 7 43 50
4 25 40 65
5 58 50 108
Average waiting time = 30
Average turn around time = 57.8
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.