int main(int argc, char** argv) {

	char* processName = argv[0];
	string id_str;

	// Si se ingresan menos argumentos de los necesarios
	if (argc < 2) {
		string msg = "Usage ";
		msg = msg + processName + " <-amountOfItemsToProduce> \n";
		Colors::writeerr(msg, RED);
		exit(EXIT_FAILURE);
	}

	int id = getpid();
	Process::announce(PRODUCER_PROCESS_NAME, id, GREEN, "initializing.");

	srand(time(NULL) * id);

	int amountOfItemsToProduce = atoi(argv[1]);


	Store* store = (Store*) SharedMemory::get(SHARED_MEMORY_ID, sizeof(Store));
	SemaphoreArray* producersSemaphore = SemaphoreArray::get(PRODUCERS_SEMAPHORE_ID);
	SemaphoreArray* consumersSemaphore = SemaphoreArray::get(CONSUMERS_SEMAPHORE_ID);


	for(int i = 0; i < amountOfItemsToProduce; i++){

		consumersSemaphore->wait();
		producersSemaphore->wait();

		sleep(Process::sleepTime());

		int newItem = Utils::generateRandomNumberBetween(1,5);
		store->item = newItem;
		if(i == amountOfItemsToProduce - 1) store->productionFinished = true;

		std::string newItemAnnouncement = "New Item: "+ Utils::intToString(newItem);
		Process::announce(processName, id, GREEN, newItemAnnouncement.c_str());

		producersSemaphore->post();
		consumersSemaphore->post();

	}

	Process::announce(PRODUCER_PROCESS_NAME, id, UNDERLINEDGREEN, "finished.");
	return 0;
}
Пример #2
0
int main()
{
    SemaphoreArray * s;
    Queue<struct iMessage> * qi;
    Queue<struct personMessage> * qp;
    SharedMemory<struct registro> * shm;
    struct registro * registro;

    qi = new Queue<struct iMessage>(PATH, Q_SALA_TO_INTERFACE, "iniciador");
    qi->create();
    qi = new Queue<struct iMessage>(PATH, Q_SALA_FROM_INTERFACE, "iniciador");
    qi->create();
    qi = new Queue<struct iMessage>(PATH, Q_CC_TO_INTERFACE, "iniciador");
    qi->create();
    qi = new Queue<struct iMessage>(PATH, Q_CC_FROM_INTERFACE, "iniciador");
    qi->create();
    qp = new Queue<struct personMessage>(PATH, Q_PERSONA, "iniciador");
    qp->create();
    qp = new Queue<struct personMessage>(PATH, Q_SALA_ABAJO, "iniciador");
    qp->create();
    qp = new Queue<struct personMessage>(PATH, Q_SALA_ARRIBA, "iniciador");
    qp->create();
    qp = new Queue<struct personMessage>(PATH, Q_CC, "iniciador");
    qp->create();
    shm = new SharedMemory<struct registro>(PATH, SHM_CC, "iniciador");
    shm->create();
    registro = shm->attach();

    registro->cc.estado = WORKING;
    registro->cc.ubicacion = BOTTOM;

    registro->abajo.estado = WORKING;
    registro->abajo.cantidad = 0;
    registro->abajo.pRead = 0;
    registro->abajo.pWrite = 0;
    for (int i = 0; i < ROOM_SIZE; i++)
    {
        registro->abajo.personas[i] = 0;
    }

    registro->arriba.estado = WORKING;
    registro->arriba.cantidad = 0;
    registro->arriba.pRead = 0;
    registro->arriba.pWrite = 0;
    for (int i = 0; i < ROOM_SIZE; i++)
    {
        registro->arriba.personas[i] = 0;
    }

    s = new SemaphoreArray(PATH, SEM_MUTEX, 1, "iniciador");
    s->create();
    s->post(0);
    s = new SemaphoreArray(PATH, SEM_FULL, 2, "iniciador");
    s->create();
    s = new SemaphoreArray(PATH, SEM_EMPTY, 1, "iniciador");
    s->create();


    std::stringstream ss;
    pid_t pid;
    pid = fork();
    if (pid == 0)
    {
        execlp("./comp-sala", "comp-sala", NULL);
        perror("Componente Sala - execlp: ");
        exit(EXIT_FAILURE);
    } else if (pid < 0)
    {
        perror("Componente Sala - fork: ");
    }
    pid = fork();
    if (pid == 0)
    {
        execlp("./comp-cablecarril", "comp-cablecarril", NULL);
        perror("Componente CableCarril - execlp: ");
        exit(EXIT_FAILURE);
    } else if (pid < 0)
    {
        perror("Componente CableCarril - fork: ");
    }

    pid = fork();
    if (pid == 0)
    {
        execlp("./sala", "sala", "-abajo", NULL);
        perror("Sala Abajo - execlp: ");
        exit(EXIT_FAILURE);
    } else if (pid < 0)
    {
        perror("Sala Abajo - fork: ");
    }
    pid = fork();
    if (pid == 0)
    {
        execlp("./sala", "sala", "-arriba", NULL);
        perror("Sala Arriba - execlp: ");
        exit(EXIT_FAILURE);
    } else if (pid < 0)
    {
        perror("Sala Arriba - fork: ");
    }
    pid = fork();
    if (pid == 0)
    {
        execlp("./cablecarril", "cablecarril", NULL);
        perror("CableCarril - execlp: ");
        exit(EXIT_FAILURE);
    } else if (pid < 0)
    {
        perror("CableCarril - fork: ");
    }
}
Пример #3
0
int main() {
	std::string owner = "terminador";

	Queue<ColaPlataforma::message> * pIn, *pOut;
	Queue<ColaExclusion::message> * eIn, *eOut;
	Queue<ColaActivado::message> * act;
	Queue<ColaArmado::message> * arm;
	Queue<ColaDispositivo::message> * disp;
	Queue<ColaSalida::message> * sal;

	Semaphore * mutexPlat;
	SemaphoreArray * semEspera;

	SharedMemory<ColaPlataforma::shared> * shmP;

	Semaphore * mutexExcl;
	SemaphoreArray * semExcl;

	SharedMemory<ColaExclusion::shared> * shmE;

	pIn = new Queue<ColaPlataforma::message>(path, (long) QueueIdentifier::PLATAFORMA_FROM_INTERFACE, owner, false);
	pIn->get();
	pIn->remove();
	pOut = new Queue<ColaPlataforma::message>(path, (long) QueueIdentifier::PLATAFORMA_TO_INTERFACE, owner, false);
	pOut->get();
	pOut->remove();
	eIn = new Queue<ColaExclusion::message>(path, (long) QueueIdentifier::EXCLUSION_FROM_INTERFACE, owner, false);
	eIn->get();
	eIn->remove();
	eOut = new Queue<ColaExclusion::message>(path, (long) QueueIdentifier::EXCLUSION_TO_INTERFACE, owner, false);
	eOut->get();
	eOut->remove();
	act = new Queue<ColaActivado::message>(path, (long) QueueIdentifier::ACTIVADO, owner, false);
	act->get();
	act->remove();
	arm = new Queue<ColaArmado::message>(path, (long) QueueIdentifier::ARMADO, owner, false);
	arm->get();
	arm->remove();
	disp = new Queue<ColaDispositivo::message>(path, (long) QueueIdentifier::DISPOSITIVOS, owner, false);
	disp->get();
	disp->remove();
	sal = new Queue<ColaSalida::message>(path, (long) QueueIdentifier::SALIDA, owner, false);
	sal->get();
	sal->remove();

	mutexPlat = new Semaphore(path, (int) (long) SemaphoreIdentifier::MUTEX_PLATAFORMA, owner, false);
	mutexPlat->get();
	mutexPlat->remove();
	mutexPlat->post();
	semEspera = new SemaphoreArray(path, (int) (long) SemaphoreIdentifier::SEM_ESPERA, ROBOT_AMOUNT, owner, false);
	semEspera->get();
	semEspera->remove();
	shmP = new SharedMemory<ColaPlataforma::shared>(path, (int) (long) SharedMemoryIdentifier::PLATAFORMA, owner, false);
	shmP->get();
	shmP->remove();

	mutexExcl = new Semaphore(path, (int) (long) SemaphoreIdentifier::MUTEX_EXCLUSION, owner, false);
	mutexExcl->get();
	mutexExcl->remove();
	mutexExcl->post();
	semExcl = new SemaphoreArray(path, (int) (long) SemaphoreIdentifier::SEM_EXCLUSION, ROBOT_AMOUNT, owner, false);
	semExcl->get();
	semExcl->remove();
	shmE = new SharedMemory<ColaExclusion::shared>(path, (int) (long) SharedMemoryIdentifier::EXCLUSION, owner, false);
	shmE->get();
	shmE->remove();
}