You are on page 1of 5

SEMFOROS EN PROCESOS

Semforo es una VARIABLE DE tipo abstracto de datos) que constituye el mtodo clsico para restringir o permitir el acceso a recursos compartidos (por ejemplo, un recurso de almacenamiento del sistema o variables del cdigo fuente) en un entorno de multiprocesamiento (en el que se ejecutarn varios procesos concurrentemente). Fueron inventados por Edsger Dijkstra en 1965 y se usaron por primera vez en el sistema operativoTHEOS PROGRAMA DE PROCESOS DE SEMAFOROS (PROBLEMA DE LOS FILOSOSFOS)

#include<iostream> #define N 5 #define IZQ (i+N-1)%N #define DER (i+1)%N #define pensando 0 #define hambre 1 #define comiendo 2 typedef int semaphore; int estado(N); semaforo mutex=1; semaforo s[N]; void filosofo(int i) { while(true){ pensar(); tomar_tenedor(i); comer(); dejar_tenedor(i); } } void tomar_tenedor(int i) { dow(&mutex); estado[i]=hambre; probar(i); up(&mutex); down(&s[i]); } void dejar_tenedor(int i) { down(&mutex);

estado[i]=pensando; probar(IZQ); probar(DER); up(&mutex); } void probar(i) { if(estado[i]==hambre&&estado[IZQ]!=comiendo&&estado[DER]!=co miendo){ estado[i]=comiendo; up(&s[i]); } }

El problema Productor/Consumidor El problema Productor/Consumidor es uno de los ejemplos clasicos de acceso a recursos compartidos que debe arbitrarse mediante algun mecanismo de concurrencia que implemente la exclusion mutua. A continuacion se proporcionan versiones que implementan la exclusion mutua mediante la utilizacion de: Monitores Java: monitores restringidos a una unica variable de condicion implcita. Monitores Signal and Continue: modelo general de monitores con mas de una variable de condicion. El problema Productor/Consumidor consiste en el acceso concurrente por parte de procesos productores y procesos consumidores sobre un recurso comun que resulta ser un buer de elementos. Los productores tratan de introducir elementos en el buer de uno en uno, y los consumidores tratan de extraer elementos de uno en uno

ALGORITIMO QUE EJECUTA UN PRODUCTOR Y CONSUMIDOR EN C++ #include <agents.h> #include <iostream> using namespace concurrency; using namespace std; // Demonstrates a basic agent that produces values. class producer_agent : public agent { public: explicit producer_agent(ITarget<int>& target, unsigned int count, int sentinel) : _target(target) , _count(count) , _sentinel(sentinel) { } protected: void run() { // Send the value of each loop iteration to the target buffer. while (_count > 0) { send(_target, static_cast<int>(_count)); --_count; } // Send the sentinel value. send(_target, _sentinel); // Set the agent to the finished state. done(); } private: // The target buffer to write to. ITarget<int>& _target; // The number of values to send. unsigned int _count;

// The sentinel value, which informs the consumer agent to stop processing. int _sentinel; }; // Demonstrates a basic agent that consumes values. class consumer_agent : public agent { public: explicit consumer_agent(ISource<int>& source, int sentinel) : _source(source) , _sentinel(sentinel) { } // Retrieves the average of all received values. int average() { return receive(_average); } protected: void run() { // The sum of all values. int sum = 0; // The count of values received. int count = 0; // Read from the source block until we receive the // sentinel value. int n; while ((n = receive(_source)) != _sentinel) { sum += n; ++count; } // Write the average to the message buffer. send(_average, sum / count); // Set the agent to the finished state.

done(); } private: // The source buffer to read from. ISource<int>& _source; // The sentinel value, which informs the agent to stop processing. int _sentinel; // Holds the average of all received values. single_assignment<int> _average; }; int wmain() { // Informs the consumer agent to stop processing. const int sentinel = 0; // The number of values for the producer agent to send. const unsigned int count = 100; // A message buffer that is shared by the agents. unbounded_buffer<int> buffer; // Create and start the producer and consumer agents. producer_agent producer(buffer, count, sentinel); consumer_agent consumer(buffer, sentinel); producer.start(); consumer.start(); // Wait for the agents to finish. agent::wait(&producer); agent::wait(&consumer); // Print the average. wcout << L"The average is " << consumer.average() << L'.' << endl; }

You might also like