You are on page 1of 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ProductorConsumidor
{
class Program
{
int N = 0;
static int[] numeros = new int[100];
static Thread[] proceso = new Thread[2];
static Semaphore mutex = new Semaphore(1, 100);
static Semaphore empty = new Semaphore(100, 100);
static Semaphore full = new Semaphore(0,100);
string getName;
public Program(string str)
{
getName = str;
Console.WriteLine("El entrado fue: " + getName);
}
static void Main(string[] args)
{
Program objeto1 = new Program("Productor_");
Program objeto2 = new Program("Consumidor_");
Thread hilo1 = new Thread(new ThreadStart(objeto1.producer));
Thread hilo2 = new Thread(new ThreadStart(objeto2.consumer));
hilo1.Start();
hilo2.Start();
Console.Read();
}
public void producer()
{
int item;
while (true)
{
item = producer_item();
empty.WaitOne();
mutex.WaitOne();
insert_item(item);
N++;
Thread.Sleep(2000);
mutex.Release();
full.Release();
if (N > 99)
break;
}
}
public void consumer()
{
int item;
while (true)
{
full.WaitOne();
mutex.WaitOne();
item = remove_item();
mutex.Release();
empty.Release();
consumer_item(item);
}
}
public int producer_item()
{
Console.WriteLine("Productor " + N);
return N;
}
public void insert_item(int item)
{
numeros[N] = item;
Console.WriteLine(" ingresando " + numeros[N]);
}
public int remove_item()
{
Console.WriteLine(" Consumiendo " + N);
return N;
}
public void consumer_item(int item)
{
item = numeros[N];
Console.WriteLine(" Consumidor " + item);
}
}
}

You might also like