0% found this document useful (0 votes)
37 views3 pages

OS Lab 2

The document contains code for calculating waiting time, turnaround time, and average times for various processes. It includes functions for finding waiting time, turnaround time, and printing a Gantt chart. The main function calls the other functions to calculate and print these metrics for sample processes.

Uploaded by

Oda's Defender
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views3 pages

OS Lab 2

The document contains code for calculating waiting time, turnaround time, and average times for various processes. It includes functions for finding waiting time, turnaround time, and printing a Gantt chart. The main function calls the other functions to calculate and print these metrics for sample processes.

Uploaded by

Oda's Defender
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std;


void findWaitingTime(int processes[], int n, int bt[],
int wt[], int at[])
{
int service_time[n];
service_time[0] = at[0];
wt[0] = 0;

for (int i = 1; i < n ; i++)


{
service_time[i] = service_time[i-1] + bt[i-1];

wt[i] = service_time[i] - at[i];

if (wt[i] < 0)
wt[i] = 0;
}
}

void findTurnAroundTime(int processes[], int n, int bt[],


int wt[], int tat[])
{
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
void print_gantt_chart(int p[], int n, int bt[], int tat[])
{
int i, j;
cout<<" ";
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) cout<<"--";
cout<<" ";
}
cout<<"\n|";

for(i=0; i<n; i++) {


for(j=0; j<bt[i] - 1; j++) cout<<" ";
printf("P%d", p[i]);
for(j=0; j<bt[i] - 1; j++) cout<<" ";
cout<<"|";
}
cout<<"\n ";
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) cout<<"--";
cout<<" ";
}
cout<<"\n";

cout<<"0";
for(i=0; i<n; i++) {
for(j=0; j<bt[i]; j++) cout<<" ";
if(tat[i] > 9) cout<<"\b";
cout<< tat[i];

}
}

void findavgTime(int processes[], int n, int bt[], int at[])


{
int wt[n], tat[n];
findWaitingTime(processes, n, bt, wt, at);
findTurnAroundTime(processes, n, bt, wt, tat);

cout << "Processes " << " Burst Time " << " Arrival Time "
<< " Waiting Time " << " Turn-Around Time "
<< " Completion Time \n";
int total_wt = 0, total_tat = 0;
for (int i = 0 ; i < n ; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
int compl_time = tat[i] + at[i];
cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"
<< at[i] << "\t\t" << wt[i] << "\t\t "
<< tat[i] << "\t\t " << compl_time << endl;
}

cout << "Total waiting time = "


<< (float)total_wt;
cout << "\nAverage waiting time = "
<< (float)total_wt / (float)n;
cout << "\nTotal turn around time = "
<< (float)total_tat;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
cout<<"\n";
print_gantt_chart(processes,n,bt,tat);
}
int main()
{
int processes[] = {1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int i;
cout<<"enter number of processes\n";
cin>>n;
int burst_time[] = {5, 9, 6};
cout<<"enter burst times\n";
for(i=0;i<n;i++){
cin>>burst_time[i];
}
int arrival_time[] = {0, 3, 6};
cout<<"enter arrival times\n";
for(i=0;i<n;i++){
cin>>arrival_time[i];
}
findavgTime(processes, n, burst_time, arrival_time);

return 0;
}

You might also like