Ejemplo n.º 1
0
void SimuladorSalida::liberarLugar(int numeroPosicion)
{
    stringstream mensajeLog;
    MemoriaCompartida<Posicion> memoria;
    Posicion posicion;

    //Bloqueo la posicion
    semaforos[numeroPosicion].p();
    mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Salida " << this->getNumeroSalida() << " - Tome el semaforo de posicion " << numeroPosicion;
    Log::getInstance()->loguear(mensajeLog.str());

    memoria = this->vectorMemoriaPosiciones[numeroPosicion];
    posicion = (Posicion) memoria.leer();
    posicion.setEstadoOcupado(false);

    memoria.escribir(posicion);

    this->vectorMemoriaPosiciones[numeroPosicion] = memoria;

    mensajeLog.str("");
    mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Salida " << this->getNumeroSalida() << " - Libero el semaforo de posicion " << numeroPosicion;
    Log::getInstance()->loguear(mensajeLog.str());

    //Desbloqueo la posicion
    semaforos[numeroPosicion].v();

    mensajeLog.str("");
    mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Salida " << this->getNumeroSalida() << " - Modifico la posicion " << numeroPosicion <<" poniendola como ocupada en el vector de posiciones.";
    Log::getInstance()->loguear(mensajeLog.str());
}
Ejemplo n.º 2
0
void Lanzador::lanzarCalesita() {
	//Memoria compartida para la calesita
	MemoriaCompartida<int> continua;
	int res = continua.crear(ARCH_SEM, MEM_CONTINUA, PERMISOS_USER_RDWR);
	if (controlErrores1(res, logger, info) == MUERTE_POR_ERROR) {raise(SIGINT);}

	continua.escribir(0);

	string arg1 = toString(auxLugares);
	string arg2 = toString(lugaresCalesita);
	string arg3 = toString(tiempoVuelta);

	pidCal = fork();
	if (pidCal == -1){
		logger->log("Error: Falla en fork de la calesita. Strerr: " + toString(strerror(errno)), info);
		raise (SIGINT);
	}
	if (pidCal == 0) {
		res = execl("Calesita", "Calesita", arg1.c_str(), arg2.c_str(), arg3.c_str(),
				(char*) 0);
		if (res != RES_OK){
			logger->log("Error: Falla en exec de la calesita. Strerr: " + toString(strerror(errno)), info);
			raise (SIGINT);
		}
	}

	logger->log("Se lanzó la calesita", info);

	//libero para que ya la maneje solo la calesita (y mas tarde el fantasma)
	res = continua.liberar();
	if (controlErrores1(res, logger, info) == MUERTE_POR_ERROR) {raise(SIGINT);}

}
int Lanzador::tomarIPC() {
	LOG("LANZADOR: CREANDO RECURSOS");
	// Creo la MC mcMuseoAbierto
	shmMuseoAbierto.crear(recShmMuseoAbierto, 'm');
	shmMuseoAbierto.escribir(false);
	LOG("LANZADOR: shmMuseoAbierto CREADA");
	
	// Creo  la MC mcContadorMuseo
	shmContadorMuseo.crear(recShmContMuseo, 'm');
	shmContadorMuseo.escribir(0);
	LOG("LANZADOR: shmContadorMuseo CREADA");
	
	// Creo el mutex mutexContadorMuseo
	mtxContMuseo = new Semaforo(recMtxContMuseo.c_str(), 'a', 1);
	LOG("LANZADOR: mtxContMuseo CREADO");
	
	// Creo el mutex mutexMuseoAbierto
	mtxMuseoAbierto= new Semaforo(recMtxMuseoAbierto.c_str(), 'a', 1);
	LOG("LANZADOR: mtxMuseoAbierto CREADO");
	
	colaNorm = new Cola<persona_t>(recColaNorm, 'a');
	LOG("LANZADOR: colaNorm CREADA");
	colaEsp = new Cola<persona_t>(recColaEsp, 'a');
	LOG("LANZADOR: colaEsp CREADA");

	ParserConfiguracion parser;
	parser.cargarConfiguracion(configuracion);

	LOG("LANZADOR: CREANDO IPC's exclusivos de las puertas");

	for(int i = 1; i <= Utils::string2int(parser.getValor("puertas")); ++i) {
		LOG("LANZADOR: CREANDO IPC's exclusivos de la puerta %d", i);
		Semaforo *mtxPuerta = new Semaforo(recMtxPuerta, i, 0);
		LOG("LANZADOR: CREANDO mtxPuerta");
		Semaforo *mtxVisitante = new Semaforo(recMtxVisitante, i, 0);
		LOG("LANZADOR: CREANDO mtxVisitante");
		Semaforo *mtxContNorm = new Semaforo(recMtxContNorm, i, 1);
		LOG("LANZADOR: CREANDO mtxContNorm");
		Semaforo *mtxContEsp = new Semaforo(recMtxContEsp, i, 1);
		LOG("LANZADOR: CREANDO mtxContEsp");
		MemoriaCompartida<int> *shmContNorm = new MemoriaCompartida<int>();
		shmContNorm->crear(recShmContNorm, i);
		LOG("LANZADOR: CREANDO shmContNorm");
		MemoriaCompartida<int> *shmContEsp = new MemoriaCompartida<int>();
		shmContEsp->crear(recShmContEsp, i);
		LOG("LANZADOR: CREANDO shmContEsp");

		colaMtx.push(mtxPuerta);
		colaMtx.push(mtxVisitante);
		colaMtx.push(mtxContNorm);
		colaMtx.push(mtxContEsp);

		colaShm.push(shmContEsp);
		colaShm.push(shmContNorm);
	}

	return 0;
}
Ejemplo n.º 4
0
bool SimuladorEntrada::modificarPosicionCompartida(int numeroPosicion)
{
	stringstream mensajeLog;

	bool pude = false;
	MemoriaCompartida<Posicion> memoria;
	Posicion posicion;

	//Bloqueo la posicion
	semaforos[numeroPosicion].p();

	mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Entrada " << this->getNumeroEntrada() << " - Tome el semaforo de posicion " << numeroPosicion;
	Log::getInstance()->loguear(mensajeLog.str());

	memoria = this->vectorMemoriaPosiciones[numeroPosicion];
	posicion = (Posicion)memoria.leer();

	//Si la posicion ya no fue ocupada
	if(!posicion.getEstadoOcupado())
	{
		posicion.setEstadoOcupado(true);
		memoria.escribir(posicion);
		this->vectorMemoriaPosiciones[numeroPosicion] = memoria;
		pude = true;
	}

	mensajeLog.str("");
	mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Entrada " << this->getNumeroEntrada() << " - Libero el semaforo de posicion " << numeroPosicion;
	Log::getInstance()->loguear(mensajeLog.str());

	//Desbloqueo la posicion
	semaforos[numeroPosicion].v();

	if(pude)
	{
		mensajeLog.str("");
		mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Entrada " << this->getNumeroEntrada() << " - Modifico la posicion " << numeroPosicion <<" poniendola como ocupada en el vector de posiciones.";
		Log::getInstance()->loguear(mensajeLog.str());
	}

	//Si devuelve true es que pudo ocupar la posicion, sino no pudo y devuelve false.
	return pude;

}
Ejemplo n.º 5
0
void Simulador::inicializarMemoriaCompartidaVectorPosiciones(int cantidadPosiciones)
{
	int i;
	Posicion posicion;
	MemoriaCompartida<Posicion> memoria;

	for (i=0;i<cantidadPosiciones;i++)
	{
		stringstream nombreArchivo;
		stringstream mensajeLog;

		// Creo archivo temporal
		nombreArchivo << ARCHIVO_POSICIONES;
		nombreArchivo << i;
		nombreArchivo<<ESTACIONAMIENTO;
		nombreArchivo<<this->numeroEstacionamiento;

		// Creo la memoria asociada al archivo temporal
		int estadoMemoria = memoria.crear ( (char*)nombreArchivo.str().c_str(),'R' );

		if ( estadoMemoria == SHM_OK )
		{
			this->vectorMemoriaPosiciones.push_back(memoria);

			//Creo un semaforo por cada posicion inicializada
			Semaforo semaforo((char*)nombreArchivo.str().c_str(), 1, (char) i);
			this->semaforos.push_back(semaforo);

			mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Creo semaforo id: " << semaforo.getId();
			Log::getInstance()->loguear(mensajeLog.str());
			mensajeLog.flush();
		}
		else
		{
			mensajeLog << "Estacionamiento " << this->numeroEstacionamiento << " - Simulador - Error al inicializar el vector de memoria compartida en la posicion " << i << " en el simulador";
			Log::getInstance()->loguear(mensajeLog.str());
			mensajeLog.flush();

		}

		nombreArchivo.flush();

	}
}
Ejemplo n.º 6
0
void Lanzador::lanzarFilasYNinios() {
	Pipe pipeEntrePuertas; // entre la fila de boletos y la de la calesita
	int res = pipeEntrePuertas.crear();
	if (controlErrores1(res, logger, info) == MUERTE_POR_ERROR) {raise(SIGINT);}

	int fdRdPuerta2 = pipeEntrePuertas.getFdLectura(); // puerta de la calesita
	int fdWrPuerta2 = pipeEntrePuertas.getFdEscritura(); //para escribirle a la puerta 2

	pidPuerta2 = fork();
	if (pidPuerta2 == -1) {
		logger->log("Error: Falla en fork de la fila de la calesita. Strerr: " + toString(strerror(errno)), info);
		raise(SIGINT);
	}
	if (pidPuerta2 == 0) {
		res = execl("FilaCalesita", "Puerta2", toString(fdRdPuerta2).c_str(), toString(fdWrPuerta2).c_str(), (char*) 0);
		if (res != RES_OK) {
			logger->log("Error: Falla en exec de la fila de la calesita. Strerr: " + toString(strerror(errno)), info);
			raise(SIGINT);
		}
	}

	logger->log("Se lanzó la fila para subir a la calesita", info);

	//pipe para que los ninios se encolen para boleto
	pipeAKids = new Pipe();
	res = pipeAKids->crear();
	if (controlErrores1(res, logger, info) == MUERTE_POR_ERROR) {raise(SIGINT);}

	int fdRdPuerta1 = pipeAKids->getFdLectura();
	fdWrPuerta1 = pipeAKids->getFdEscritura(); //para escribirle a la puerta 1

	pidPuerta1 = fork();
	if (pidPuerta1 == -1) {
		logger->log("Error: Falla en fork de la fila de boletos. Strerr: " + toString(strerror(errno)), info);
		raise(SIGINT);
	}

	if (pidPuerta1 == 0) {
		res = execl("FilaBoleto", "Fila1", toString(fdRdPuerta1).c_str(),
				toString(fdWrPuerta1).c_str(), toString(fdRdPuerta2).c_str(),
				toString(fdWrPuerta2).c_str(), (char*) 0);
		if (res != RES_OK) {
			logger->log("Error: Falla en exec de la fila de boletos. Strerr: " + toString(strerror(errno)), info);
			raise(SIGINT);
		}
	}

	logger->log("Se lanzó la fila para comprar boleto", info);

	pipeEntrePuertas.cerrar();

	MemoriaCompartida<int> kidsInPark;
	res = kidsInPark.crear(ARCH_SEM, MEM_KIDS, PERMISOS_USER_RDWR);
	if (controlErrores1(res, logger, info) == MUERTE_POR_ERROR) {raise(SIGINT);}

	//Memoria compartida de cantidad de chicos en parque
	kidsInPark.escribir(0);

	//Memoria compartida para cada lugar

	VectorMemoCompartida<bool> lugares;

	res = lugares.crear(ARCH_SEM,INICIO_CLAVES_LUGARES,PERMISOS_USER_RDWR,this->lugaresCalesita);
	if ( controlErrores1(res, logger, info) == MUERTE_POR_ERROR ) { raise (SIGINT);}

	//lanza niños
	for (int i = 0; i < cantNinios; i++) {

		pid_t pid = fork();
		if (pid == -1){
			logger->log("Error: Falla en fork de un niño. Strerr: " + toString(strerror(errno)), info);
			raise (SIGINT);
		}

		if (pid == 0) {
			res = execl("Kid", "Kid", toString(fdRdPuerta1).c_str(),
					toString(fdWrPuerta1).c_str(), toString(lugaresCalesita).c_str(), (char*) 0);
			if (res != RES_OK){
				logger->log("Error: Falla en exec de un niño. Strerr: " + toString(strerror(errno)), info);
				raise (SIGINT);
			}
		}

		logger->log("Se lanzó un niño", info);
		sleep((int) exponentialTime(mediaKid));
	}

	//se desattachea de estas shared mem. despues de crear los hijos, asi siempre hay alguien usandola.
	if ( controlErrores1(res, logger, info) == MUERTE_POR_ERROR ) { raise (SIGINT);}
	res = kidsInPark.liberar();

	res = lugares.liberar();
	if ( controlErrores1(res, logger, info) == MUERTE_POR_ERROR ) { raise (SIGINT);}
}