This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathstatusCheck.cpp
More file actions
164 lines (136 loc) · 3.39 KB
/
Copy pathstatusCheck.cpp
File metadata and controls
164 lines (136 loc) · 3.39 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
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
// The sum of all factorials of n!
#include<iostream>
using namespace std;
void swap(int* a, int* b){
int t = *a;
*a = *b;
*b = t;
}
void swapc(char* a, char* b){
char t = *a;
*a = *b;
*b = t;
}
void heapify(int start[], int finish[], char status[], int n, int i){
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && finish[l] > finish[largest])
largest = l;
if (r < n && finish[r] > finish[largest])
largest = r;
if (largest != i){
swap(&finish[i], &finish[largest]);
swap(&start[i], &start[largest]);
swapc(&status[i], &status[largest]);
heapify(start, finish, status, n, largest);
}
}
void sort(int start[], int finish[], char status[], int n){
for (int i = n / 2 - 1; i >= 0; i--)
heapify(start, finish, status, n, i);
for (int i=n-1; i>=0; i--){
swap(&finish[0], &finish[i]);
swap(&start[0], &start[i]);
swapc(&status[0], &status[i]);
heapify(start, finish, status, i, 0);
}
}
int maxFinishTime(int finish[], int n){
int max = 0;
for(int i=0;i<n;i++)
if(finish[i] > max)
max = finish[i];
return max;
}
int status_check(int s[], int f[], char st[], int n) {
//find the maximum finish time among all process
int N = maxFinishTime(f, n) + 1;
//count stores whether status_check() is invoked, if yes count[i] = 1, else count[i] = 0
int count[N];
//initialize count to 0 initially
for(int i=0;i<N;i++)
count[i] = 0;
int i, j;
//keep track of each process for status_check()
int pid[n];
// Initialize all pid to 0
for(int i=0;i<n;i++)
pid[i] = 0;
i = 0;
int timeF = 0;;
// Consider rest of the activities
for (j = 1; j < n; j++){
// If this activity has start time greater than or
// equal to the finish time of previously selected
// activity, then select it
if(s[j] == f[i]){
//last status check
int lastStatusCheck = 0;
for(int k=N-1;k>=0;k--){
if(count[k] == 1){
lastStatusCheck = k;
break;
}
}
if(pid[i] == 0){
count[f[i]] = 1;
timeF = f[i];
pid[i] = 1;
pid[j] = 1;
}
else if(pid[j] == 0 && timeF != lastStatusCheck){
count[f[j]] = 1;
timeF = f[j];
pid[j] = 1;
}
}
//overlap
else if(f[i] >= s[j] && s[i] != s[j]){
//if status is S i.e. 'Sensitive' apply status_check()
if(timeF < f[i]){
count[f[i]] = 1;
timeF = f[j];
pid[i] = 1;
pid[j] = 1;
}
}
//no overlap
else{
if(timeF < f[i] && s[i] != s[j]){
count[f[i]] = 1;
timeF = f[i];
pid[i] = 1;
pid[j] = 1;
}
}
i++;
}
cout<<endl;
//count the number of status_check()
int ch = 0;
for(int i=0;i<N;i++)
ch+=count[i];
for(int i=0;i<N;i++)
cout<< count[i]<< " ";
cout<<endl;
return ch;
}
int main(){
int n;
cout <<"Enter the number of process"<<endl;
cin>>n;
int start[n];
int finish[n];
char status[n];
for(int i=0;i<n;i++){
cout<<"Enter the start time, finish time, and status of process %d"<<i<<endl;
cin>>start[i] >> finish[i] >> status[i];
}
//sort in non-decreasing order of finish time
sort(start, finish, status, n);
//check for conflict
int check = status_check(start, finish, status, n);
cout<<"Number of times status_check() is invoked is %d"<<check;
return 0;
}