-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMonitor_Sleeping_Barbar.cs
168 lines (147 loc) · 5.16 KB
/
Monitor_Sleeping_Barbar.cs
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
using System;
using System.IO;
using System.Threading;
class BarberShop
{
// Initializing variables
private static readonly object _shopLock = new object();
private static readonly object _cashierLock = new object();
private static readonly int MAX_CAPACITY = 10;
private static readonly int WAITING_ROOM_CAPACITY = 4;
private static readonly int BARBER_SEATS_CAPACITY = 3;
private static int waitingCustomers = 0;
private static int barberSeatsOccupied = 0;
private static bool cashierFree = true;
private static string to_write = "";
private static readonly object _fileLock = new object();
public static void main()
{
// Main function of program --> 20 clients
for (int i = 1; i <= 20; i++)
{
Thread t = new Thread(new ParameterizedThreadStart(Customer));
t.Start(i);
}
}
private static void Customer(object id)
{
lock (_shopLock)
{
int customerId = (int)id;
if (customerId >= 11 && customerId <= 20)
{
while (waitingCustomers + barberSeatsOccupied == MAX_CAPACITY || waitingCustomers == WAITING_ROOM_CAPACITY)
{
Console.WriteLine("Customer {0} is waiting outside the shop", id);
to_write = $"Customer {id} is waiting outside the shop";
WriteToFile(to_write);
to_write = "";
Monitor.Wait(_shopLock);
}
}
// Capacity in the store has reached
if (waitingCustomers + barberSeatsOccupied == MAX_CAPACITY)
{
Console.WriteLine("Customer {0} left the shop - capacity reached", id);
to_write += $"Customer {id} left the shop - capacity reached";
WriteToFile(to_write);
to_write = "";
return;
}
// 3 people seating at barber seat
if (barberSeatsOccupied == BARBER_SEATS_CAPACITY || waitingCustomers < WAITING_ROOM_CAPACITY)
{
//while (waitingCustomers == WAITING_ROOM_CAPACITY)
//{
// Console.WriteLine("Customer {0} is waiting outside the shop", id);
// to_write += $"Customer {id} is waiting outside the shop";
// WriteToFile(to_write);
// to_write = "";
// Monitor.Wait(_shopLock);
//}
waitingCustomers++;
Console.WriteLine("Customer {0} entered the waiting room", id);
Monitor.Wait(_shopLock);
to_write = $"Customer {id} entered the waiting room";
WriteToFile(to_write);
to_write = "";
waitingCustomers--;
}
barberSeatsOccupied++;
Console.WriteLine("Customer {0} is getting a haircut", id);
to_write = $"Customer {id} is getting a haircut";
WriteToFile(to_write);
to_write = "";
}
// Simulating haircut process
Thread.Sleep(1000);
Console.WriteLine("Customer {0} finished getting a haircut", id);
//to_write += $"Customer {id} is getting a haircut";
//WriteToFile(to_write);
//to_write = "";
lock (_cashierLock)
{
if (!cashierFree)
{
Console.WriteLine("Customer {0} is waiting for cashier", id);
to_write = $"Customer {id} is waiting for cashier";
WriteToFile(to_write);
to_write = "";
Monitor.Wait(_cashierLock);
}
Console.WriteLine("Customer {0} is paying", id);
to_write = $"Customer {id} is paying";
WriteToFile(to_write);
to_write = "";
cashierFree = false;
}
// Simulating payment process
Thread.Sleep(1000);
Console.WriteLine("Customer {0} finished paying", id);
to_write = $"Customer {id} finished paying";
WriteToFile(to_write);
to_write = "";
lock (_cashierLock)
{
cashierFree = true;
Monitor.Pulse(_cashierLock); // Releasing the cashier lock
}
// Checking shop lock
lock (_shopLock)
{
barberSeatsOccupied--;
if (waitingCustomers > 0)
{
Monitor.Pulse(_shopLock);
}
if (waitingCustomers + barberSeatsOccupied < MAX_CAPACITY && waitingCustomers < WAITING_ROOM_CAPACITY)
{
Monitor.PulseAll(_shopLock);
}
}
}
private static void WriteToFile(string text)
{
lock (_fileLock)
{
text += "\n";
try
{
File.AppendAllText("Barber Day.txt", text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
class Program
{
public static void Main(string[] args)
{
BarberShop.main();
Console.ReadKey();
Console.WriteLine("\nDone !!!");
}
}