Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Sep 2nd, 2010 | Syntax: None | Size: 1.21 KB | Hits: 20 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace scratchpad
  8. {
  9.     class Customer
  10.     {
  11.  
  12.         static Semaphore chairs;
  13.         static Semaphore barber;
  14.         static Semaphore barberShop;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.  
  19.             string name = args[0];
  20.             int chairNum = int.Parse(args[1]);
  21.             int custNum = int.Parse(args[2]);
  22.  
  23.             chairs = new Semaphore(chairNum, chairNum, "chairs");
  24.             barber = new Semaphore(1, 1, "barber");
  25.  
  26.             customerStuff(name, chairNum, custNum);
  27.            
  28.         }
  29.  
  30.         static void customerStuff(string name, int chairsNum, int custNum)
  31.         {
  32.             Console.WriteLine("Customer "+name+" has arrived");
  33.             bool chairFree = chairs.WaitOne(1, false);
  34.             if (!chairFree)
  35.                 Console.WriteLine("Customer " + name + " could not find a chair and is leaving");
  36.             else
  37.                 Console.WriteLine("Customer " + name + " has arrived");
  38.             barber.WaitOne();
  39.             chairs.Release();
  40.             barberShop.Release();
  41.             barber.Release();
  42.         }
  43.     }
  44. }