-
Notifications
You must be signed in to change notification settings - Fork 0
/
rr.cpp
193 lines (175 loc) · 4.55 KB
/
rr.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* rboin.cpp
*
* Created on: 09-Aug-2018
* Author: sunil
*/
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
using namespace std;
int processcount=0;//counts total no of process created.
int finishcount=0;//counts total no of process finished.
struct node
{
int time;
char c[5];
node *next;
node *prev;
};
node *insertion(node *start)
{
static int i=1;
processcount++;
node *n=new node;
n->next=NULL;
n->prev=NULL;
n->time=rand()%20+1;
n->c[0]='P';
int rev=0,j=i;
int fc=0;
//below two loops are used to reverse the ll time so we can easily enter in char array;
while(j)//1st loop
{
rev=rev*10+j%10;
if(j%10==0)
fc++;
j=j/10;
}
if(rev<10 && fc==0)
{
rev=rev*10;
}
j=1;
while(rev)//2nd loop
{
n->c[j]=(rev%10+'0');
rev=rev/10;
j++;
}
while(fc--)
{
n->c[j]='0';
j++;
}
n->c[j]='\0';
i++;
// cout<<n->c<<" "<<n->time<<endl;
if(start==NULL)
{
start=n;
n->next=start;
n->prev=start;
}
else
{
node *t=start;
while(t->next!=start)
{
t=t->next;
}
t->next=n;
n->prev=t;
n->next=start;
start->prev=n;
}
return start;
}
int delTime(node *start,int time,char **arr,int *val)
{
int operationtime=rand()%100;
while(operationtime<20)
{
operationtime=rand()%100;
}
cout<<"Total Scheduling Time="<<operationtime<<endl;
int i=0;
int ttime=0;
//the down while loop will run until there in only one process in linked list and its value should also be less than 0 to stop;
while(operationtime>0 && (start->next!=start || start->time>0))
{
int generation=rand()%10;
if(generation<5)
{
start=insertion(start);//used to insert process at random time
}
int ctime=0;//ctime will store the time for which the process gets executed and and subtract it from total operation time;
if(start->time-time>0)//is process time is greater then quantam time
{
if(operationtime<time)//if oeration time < than quantime time, do not run exit the loop;;
{
ctime=operationtime;
}
else
{
start->time=start->time-time;
ctime=time;
ttime+=time;
char *p=new char[10];
p=start->c;
arr[i]=p;
val[i]=ttime;
i++;
start=start->next;
}
}
else if(start->time-time<=0)//if process time is equal to or less than quantam time
{
if(operationtime<start->time)// if operation time is < than process time exit the loop
{
ctime=start->time;
}
else
{
ttime+=start->time;
ctime=start->time;
start->time=start->time-time;
node *t=start;
start=start->next;
t->prev->next=t->next;;
start->prev=t->prev;
char *p=new char[10];
p=t->c;
arr[i]=p;
val[i]=ttime;
i++;
finishcount++;
}
}
operationtime-=ctime;
}
return i;
}
void printGang(char ** arr,int i,int *val)
{
cout<<endl<<"Gang Chart Of Process Which Gets Executed"<<endl;
cout<<"----------------------"<<endl;
cout<<"| Process | Time |"<<endl;
cout<<"----------------------"<<endl;
for(int j=0; j<i; j++)
{
cout<<"| "<<arr[j]<<" | ";
cout<<setw(3) << std::setfill(' ')<<val[j]<<" |"<<endl;
}
cout<<"----------------------"<<endl;
}
int main()
{
srand(time(0));
node *start=NULL;
for(int i=0; i<3; i++)
start=insertion(start);
int delt=0;
while(delt<5)
{
delt=rand()%10+1;
}
cout<<"Quantam Time = "<<delt<<endl;
char **arr=(char **)malloc(100*sizeof(int*));
int *val=(int *)malloc(100*sizeof(int));
int i=delTime(start,delt,arr,val);
cout<<"Total Process In The Queue = "<<processcount<<endl;
printGang(arr,i,val);
cout<<"Total Process Completed = "<<finishcount<<endl;
}