-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.cpp
81 lines (66 loc) · 1.74 KB
/
simulation.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
#include <iostream>
#include <pthread.h>
int Matrix[2][50] = {0};
int Occupied=0;
int Full=100;
int turn= 0;
void CriticalRegion(int id){
std::cout<<"Agency "<<id<<" Entered Critical Region"<<std::endl;
int check,row,column;
do{
check = rand()%100;
column = check%50;
row = check/50;
}while(Matrix[row][column]!=0);
Matrix[row][column]= id;
Occupied++;
std::cout<<"Seat Number "<<check<<" is reserved by Agency "<<id<<" OCCUPIED"<<Occupied<<"OCCUPIED "<<std::endl;
}
void* thread_Agency1(void * id){
while(true){
while(turn!=0){;}
if(Occupied==Full){
turn=1;
break;}
CriticalRegion(*((int*) id));
std::cout<<"Agency 1 Exit Critical Region"<<std::endl<<std::endl;
turn=1;
//NonCriticalRegion();
}
}
void* thread_Agency2(void* id){
while(true){
while(turn!=1){;}
if(Occupied==Full){
turn=0;
break;}
CriticalRegion(*((int*) id));
std::cout<<"Agency 2 Exit Critical Region"<<std::endl<<std::endl;
turn=0;
//NonCriticalRegion();
}
}
void PrintMatrix(){
for(int i=0;i<2;i++){
for(int j=0;j<50;j++){
std::cout<<Matrix[i][j]<<" ";
}
std::cout<<std::endl;
}
}
int main(){
srand(time(NULL));
PrintMatrix();
pthread_t Agency_1,Agency_2;
int id_1 =1, id_2=2;
pthread_create(&Agency_1, NULL, thread_Agency1, (void*) &id_1);
pthread_create(&Agency_2, NULL, thread_Agency2, (void*) &id_2);
while(true){
if(Occupied==Full){break;}
}
pthread_join(Agency_1,NULL);
pthread_join(Agency_2,NULL);
std::cout<<"No Seats Left\n\nPlane is Full:\n\n";
PrintMatrix();
return 0;
}