-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
133 lines (116 loc) · 2.82 KB
/
code.cpp
File metadata and controls
133 lines (116 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<stdio.h>
#include<conio.h>
void rr(int no,int remt[10],int Cur_t,int arT[10], int bsT[10]);
main()
{
int Proc_no,j,no,CurT,RemProc,indicator,time_quan,wait,tut,arT[10],bsT[10],remt[10],x=1;
indicator = 0;
wait = 0;
tut = 0;
printf("Enter number of processes ");
scanf("%d",&no);
RemProc = no;
printf("\nEnter the arrival time and burst time of the processes\n");
for(Proc_no = 0;Proc_no < no;Proc_no++)
{
printf("\nProcess P%d\n",Proc_no+1);
printf("Arrival time = ");
scanf("%d",&arT[Proc_no]);
printf("Burst time = ");
scanf("%d",&bsT[Proc_no]);
remt[Proc_no]=bsT[Proc_no];
}
printf("The details of time quantum are as follows:\n");
printf("The time quantum for first round is 6.\n");
time_quan=6;
CurT=0;
for(Proc_no=0;RemProc!=0;)
{
if(remt[Proc_no]<=time_quan && remt[Proc_no]>0)
{
CurT+=remt[Proc_no];
remt[Proc_no]=0;
indicator=1;
}
else if(remt[Proc_no]>0)
{
remt[Proc_no]-=time_quan;
CurT+=time_quan;
}
if(remt[Proc_no]==0 && indicator==1)
{ printf("%d",Proc_no);
RemProc--;
printf("P %d",Proc_no+1);
printf("\t\t\t%d",CurT-arT[Proc_no]);
printf("\t\t\t%d\n",CurT-bsT[Proc_no]-arT[Proc_no]);
wait+=CurT-arT[Proc_no]-bsT[Proc_no];
tut+=CurT-arT[Proc_no];
indicator=0;
}
if(Proc_no==no-1){
x++;
if(x==2){
Proc_no=0;
time_quan=10;
printf("The time quantum for second round is 10. \n");
}
else{
break;
}
}
else if(CurT >= arT[Proc_no+1]){
Proc_no++;
}
else{
Proc_no=0;
}
}
rr(no,remt,CurT,arT,bsT);
return 0;
}
void rr(int no,int remt[10],int Cur_t,int arT[10], int bsT[10]){
float avg_wait,avg_tut;
int i,j,n=no,temp,btime[20],Proc_no[20],w_time[20],tut_t[20],total=0,loc;
printf("Third round with least burst time.\n");
for(i=0;i<n;i++)
{
btime[i]=remt[i];
w_time[i]=Cur_t-arT[i]-btime[i];
Proc_no[i]=i+1;
}
for(i=0;i<n;i++)
{
loc=i;
for(j=i+1;j<n;j++)
{
if(btime[j]<btime[loc]){
loc=j;
}
}
temp=btime[i];
btime[i]=btime[loc];
btime[loc]=temp;
temp=Proc_no[i];
Proc_no[i]=Proc_no[loc];
Proc_no[loc]=temp;
}
for(i=1;i<n;i++)
{
for(j=0;j<i;j++){
w_time[i]+=btime[j];
}
total+=w_time[i];
}
avg_wait=(float)total/n;
total=0;
printf("\nProcess\t\tBurst time\t\twaiting time\t\tTurnaround Time");
for(i=0;i<n;i++)
{
tut_t[i]=btime[i]+w_time[i];
total=total + tut_t[i];
printf("\nP%d\t\t\t%d\t\t\t%d\t\t\t%d",Proc_no[i],btime[i],w_time[i],tut_t[i]);
}
avg_tut=(float)total/n;
printf("\n\nAverage waiting time = %f",avg_wait);
printf("\n Average turnaround time = %f\n",avg_tut);
}