-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (178 loc) · 4.43 KB
/
main.cpp
File metadata and controls
192 lines (178 loc) · 4.43 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
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
#include<pthread.h>
#include<iostream>
#include<time.h>
#include<unistd.h>
#include<iomanip>
#include<stdlib.h>
#include<string.h>
using namespace std;
void* ATB(void*);
void* BTA(void*);
pthread_mutex_t mutex,atb,bta,mutex1;
time_t start;
int atbcnt,btacnt;
int atbcyc,btacyc; //to check for starvation
int onatb,onbta; //to check if a villager is on the bridge
int VILLCNT,STARVCNT;
void display(int id,string str="")
{
pthread_mutex_lock(&mutex);
for(int i=0;i<VILLCNT;i++)
{
if(id==-1) cout<<setw(7)<<"Vill. "<<i+1<<'|';
else
{
if(i==id-1) cout<<setw(8)<<str<<'|';
else cout<<setw(9)<<'|';
}
}
cout<<time(NULL)-start<<"sec"<<endl;
pthread_mutex_unlock(&mutex);
}
void* ATB(void* id)
{
display(*(int*)id,"Appeared");
display(*(int*)id,"A to B");
display(*(int*)id,"Waiting"); //stage 1
pthread_mutex_lock(&atb); //lock the bridge
display(*(int*)id,"Got Perm"); //stage 2
pthread_mutex_lock(&mutex); //lock mutex
::atbcnt++;
::atbcyc++; //increment count of villagers
if(::atbcnt==1) // if the 1st villager close the other bridge entry
pthread_mutex_trylock(&bta);
if(::atbcyc!=STARVCNT) //if count is less than starvation unlock our bridge
pthread_mutex_unlock(&atb);
else
::atbcyc=0; //else again count for starvation is zero
pthread_mutex_unlock(&mutex); //unlock mutex
display(*(int*)id,"Crossing"); //stage 2
pthread_mutex_lock(&mutex1);
::onatb++;
while(::onatb>1); //a villager may have to travel behind another villager
pthread_mutex_unlock(&mutex1);
sleep((rand()%5)+2); //travelling
display(*(int*)id,"Crossed!"); //stage 3
pthread_mutex_lock(&mutex); //locking mutex to decrement
::atbcnt--;
::onatb--;
if(::atbcnt==0) //if villager is the last person
//then open the opposite bridge
pthread_mutex_unlock(&bta);
pthread_mutex_unlock(&mutex);
sleep(2);
if(!pthread_mutex_trylock(&bta)) //if opposite village bridge is locked
{
pthread_mutex_unlock(&atb); //open both the village bridges
pthread_mutex_unlock(&bta);
}
pthread_exit(NULL);
}
void* BTA(void* id)
{
display(*(int*)id,"Appeared");
display(*(int*)id,"B to A");
display(*(int*)id,"Waiting");
pthread_mutex_lock(&bta);
display(*(int*)id,"Got Perm");
pthread_mutex_lock(&mutex);
::btacnt++;
::btacyc++;
if(::btacnt==1)
pthread_mutex_trylock(&atb);
if(::btacyc!=STARVCNT)
pthread_mutex_unlock(&bta);
else
::btacyc=0;
pthread_mutex_unlock(&mutex);
display(*(int*)id,"Crossing");
pthread_mutex_lock(&mutex1);
::onbta++;
while(::onbta>1); //a villager may have to travel behind another villager
pthread_mutex_unlock(&mutex1);
sleep((rand()%5)+2);
display(*(int*)id,"Crossed!");
pthread_mutex_lock(&mutex);
::btacnt--;
::onbta--;
if(::btacnt==0)
pthread_mutex_unlock(&atb);
pthread_mutex_unlock(&mutex);
sleep(2);
if(!pthread_mutex_trylock(&atb))
{
pthread_mutex_unlock(&bta);
pthread_mutex_unlock(&atb);
}
pthread_exit(NULL);
}
int main(int a, char *b[10])
{
int m,n,STARVCNT=3;
// m- North to South villagers
// n- South to North villagers
::VILLCNT=(rand()%5)+4;
if(a==4)
{
m=atoi(b[1]);
n=atoi(b[2]);
::STARVCNT=atoi(b[3]);
::VILLCNT=m+n;
}
else if(a==3)
{
m=atoi(b[1]);
n=atoi(b[2]);
::VILLCNT=m+n;
}
else if(a==2)
::STARVCNT=atoi(b[1]);
else if(a>4)
{
cout<<"Invalid number of arguments\n";
exit(0);
}
::start=time(NULL);
pthread_t vill[VILLCNT];
::atbcnt=0;::btacnt=0;
::atbcyc=0;::btacyc=0;
::onatb=0;::onbta=0;
int id[VILLCNT];
int t;
pthread_mutex_init(&mutex,NULL);
pthread_mutex_init(&atb,NULL);
pthread_mutex_init(&bta,NULL);
pthread_mutex_init(&mutex1,NULL);
display(-1);
srand(time(NULL));
for(int i=0;i<VILLCNT;i++)
{
void* (*villgen)(void*);
id[i]=i+1;
t=rand()%2;
if(t==1 && m!=0) //villager creation random village
{
villgen=&ATB;
m--;
}
else if(t==0 && n!=0)
{
villgen=&BTA;
n--;
}
else if(t==0 && m!=0)
{
villgen=&ATB;
m--;
}
else if(t==1 && n!=0)
{
villgen=&BTA;
n--;
}
sleep(rand()%4);
pthread_create(&vill[i],NULL,villgen,(&id[i]));
}
pthread_exit(NULL);
return 0;
}