Sleeping barber problem – in C#

Waiting room of a barber shop, a barber chair and chair stand is composed of n units. If you are a barber shave the customer will be sleeping. If a customer arrives filled all the chairs in the waiting customer leaves the shop. If the customer when the barber shaving a customer to another, whereas if you have an empty chair in the waiting queue into the customer sits in a chair. Sleep when the customer raises barber barber. Critical areas to use the semaphore if necessary.






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
class Program
{
int i;
static Random Rand = new Random();
const int MaxCustomers = 25;
const int NumChairs = 5;
static Semaphore waitingRoom = new Semaphore(NumChairs, NumChairs);
static Semaphore barberChair = new Semaphore(1, 1);
static Semaphore barberPillow = new Semaphore(0, 1);
static Semaphore seatBelt = new Semaphore(0, 1);

static bool AllDone = false;
static void Barber()
{
while (!AllDone)
{
Console.WriteLine(“Berber uyuyor”);
barberPillow.WaitOne();
if (!AllDone)
{
//herhangi zamanda saç kesimi
Console.WriteLine(“Berber saç kesiyor”);
Thread.Sleep(Rand.Next(1, 3) * 1000);
Console.WriteLine(“Berber sac kesimini bitirdi.”);
seatBelt.Release();
}
else
{
Console.WriteLine(“berber gidiyor”);
}
}
return;
}

static void Customer(Object number)
{
int Number = (int)number;
Console.WriteLine(“Musteri {0} berberden ayrılıyor “, number);
Thread.Sleep(Rand.Next(1, 5) * 1000);
Console.WriteLine(“Musteri {0} berber dukkanina geldi “, number);
waitingRoom.WaitOne();
Console.WriteLine(“Musteri {0} berber dukkanina giriyor “, number);
barberChair.WaitOne();
waitingRoom.Release();
Console.WriteLine(“Musteri {0} berberin uyanmasını istiyor”, number);
barberPillow.Release();
seatBelt.WaitOne();
Console.WriteLine(“Musteri {0} berberden ayrılıyor “, number);

}

static void main()

{
Thread barberThread = new Thread(Barber);
barberThread.Start();
Thread[] Customers = new Thread[MaxCustomers];
for (int i = 0; i < MaxCustomers; i++)
{
Customers[i] = new Thread(new ParameterizedThreadStart(Customer
));

Customers[i].Start(i);
for (i =0; i < MaxCustomers; i++)
{
Customers[i].Join();
}
AllDone = true;
barberPillow.Release();
barberThread.Join();
Console.WriteLine(“Bütün işler bitti”);

}
}
}
}

VN:F [1.9.22_1171]
Rating: 3.5/5 (4 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 3 votes)
Sleeping barber problem - in C#, 3.5 out of 5 based on 4 ratings

1.457 Görüntülenme

 

Yoruma kapalı.

More in C#, PROGRAMLAMA (13 of 22 articles)