Beispiel #1
0
void*
hiloDestruccion()
{
  t_nodo_proceso * nodoARemover;
  while (1)
    {
      sem_wait(&sem_listaTerminados);
      pthread_mutex_lock(&mutex_listaTerminados);
      nodoARemover = queue_pop(listaTerminados);
      pthread_mutex_unlock(&mutex_listaTerminados);
      int pidARemover = nodoARemover->pcb.pid;
      log_info(log_kernel,"Sacando el proceso PID %d de Terminados",pidARemover);
      log_info(log_kernel,"Destruyendo segmentos del PID %d",pidARemover);
      t_datosEnviar * paquete = pedirPaquete((void*) &pidARemover,
          KERNEL_DESTRUIR_SEGMENTOS_PROGRAMA, sizeof(int));
      common_send(socketUMV, paquete, NULL );
      destruirPaquete(paquete);
      paquete = pedirPaquete("T", PROGRAMA_CERRAR, 2);
      common_send(nodoARemover->soquet_prog, paquete, NULL );
      destruirPaquete(paquete);
      free(nodoARemover);
    }
  return NULL ;

}
Beispiel #2
0
void*
hiloDestruccion()
{
  t_nodo_proceso * nodoARemover;
  while (1)
    {
      sem_wait(&sem_listaTerminados);
      pthread_mutex_lock(&mutex_listaTerminados);
      nodoARemover = queue_pop(listaTerminados);
      pthread_mutex_unlock(&mutex_listaTerminados);
      int pidARemover = nodoARemover->pcb.pid;
      debugTrackPLP("[Achievement] Removing process %d from the system.",pidARemover);
      debugTrackPLP("Requesting segment destroy...");
      t_datosEnviar * paquete = pedirPaquete((void*) &pidARemover,
          KERNEL_DESTRUIR_SEGMENTOS_PROGRAMA, sizeof(int));
      common_send(socketUMV, paquete, NULL );
      destruirPaquete(paquete);
      paquete = pedirPaquete("T", PROGRAMA_CERRAR, 2);
      common_send(nodoARemover->soquet_prog, paquete, NULL );
      destruirPaquete(paquete);
      free(nodoARemover);
    }
  return NULL ;

}
void terminarCPUSinPCB() {
	t_datosEnviar* info1 = pedirPaquete("a", CPU_PCP_DISCONNECTION, 2);
	t_datosEnviar* info = pedirPaquete("a", UMV_CAE_CPU, 2);
	common_send(umvSocket, info, NULL );
	common_send(pcpSocket, info1, NULL );
	destruirPaquete(info1);
	destruirPaquete(info);
	close(umvSocket);
	close(pcpSocket);
	config_destroy(configuracion_CPU);
	exit(0);
}
Beispiel #4
0
int
pedirSegmento(int * error, int tamanio, int pid)
{
  int tam = tamanio;

  t_datosEnviar * paquete;
  void * pedidoSegmentoUMV = malloc(3 * sizeof(int));
  memcpy(pedidoSegmentoUMV, &pid, sizeof(int));
  memcpy(pedidoSegmentoUMV + sizeof(int), &tam, sizeof(int));

  paquete = pedirPaquete(pedidoSegmentoUMV, KERNEL_PEDIR_SEGMENTO_UMV, //Pedir Segmento
      2 * sizeof(int));
  common_send(socketUMV, paquete, NULL );

  free(paquete->data);

  paquete = common_receive(socketUMV, NULL );

  if (paquete->codigo_Operacion == ERROR_NO_CREO_SEGMENTO)
    {
      *error = 1;
    }
  memcpy(&tam, paquete->data, sizeof(int));
  destruirPaquete(paquete);
  free(pedidoSegmentoUMV);
  return tam; //Guardado la base
}
void sendWaitAnswer(int cpuSocket, int codOp)
{
	char* mensajeComun = strdup("o");
	t_datosEnviar* mensaje = pedirPaquete(mensajeComun, codOp,
			string_length(mensajeComun) + 1);
	common_send(cpuSocket, mensaje, NULL );
	destruirPaquete(mensaje);
}
void printText(t_nodo_proceso_ejecutando* procesoEjecutando,
		struct pollfd** socketsCPU, int* cantSocketsCpu, t_datosEnviar* paquete)
{
	debugTrackPCP(" Request to print text \" %s \"", paquete->data);
	log_info(log_kernel,
			"Request to print text  \" %s \", requested by the CPU with pid %d\n",
			paquete->data, procesoEjecutando->cpu.pid);
	t_datosEnviar* mensaje = pedirPaquete(paquete->data,
			PROGRAMA_PAQUETE_PARA_IMPRIMIR, paquete->data_size);
	common_send(procesoEjecutando->proceso.soquet_prog, mensaje, NULL );
	destruirPaquete(mensaje);
}
void sharedVariableRequest(t_nodo_proceso_ejecutando* procesoEjecutando,
		struct pollfd** socketsCPU, int* cantSocketsCpu, t_datosEnviar* paquete)
{

	t_varCompartida* varBuscada = dictionary_get(dictionarySharedVariables,
			(char*) paquete->data);
	t_datosEnviar* mensaje = pedirPaquete(&varBuscada->valor, PCP_CPU_OK,
			sizeof(uint32_t));
	debugTrackPCP(" Request of the shared variable: %s .", mensaje->data);
	common_send(procesoEjecutando->cpu.socket, mensaje, NULL );
	destruirPaquete(mensaje);
}
void terminarCPU(void* dataExtra, t_size size) {
	if (proceso_activo) {
		t_datosEnviar* pcbAEnviar = empaquetarPCB(CPU_PCP_DISCONNECTION);
		common_send(pcpSocket, pcbAEnviar, NULL );
		destruirPaquete(pcbAEnviar);
	} else {
           t_datosEnviar* cerrar = pedirPaquete("a",CPU_PCP_DISCONNECTION,2);
           common_send(pcpSocket,cerrar,NULL);
           destruirPaquete(cerrar);
           }
           t_datosEnviar* info = pedirPaquete("a", UMV_CAE_CPU, 2);
           common_send(umvSocket, info, NULL );
           destruirPaquete(info);
           close(umvSocket);
	       close(pcpSocket);
	       clean_variable_dictionary();
	       free(currentPCB);
	       config_destroy(configuracion_CPU);
	       exit(0);
         
}
Beispiel #9
0
void doHandshakeUMV() {
	t_datosEnviar* paquete = pedirPaquete("a", HANDSHAKE_UMV, 2);
	common_send(umvSocket, paquete, destroyOnConnectionFailure);
	destruirPaquete(paquete);
	paquete = common_receive(umvSocket, NULL );
	if (paquete->codigo_Operacion != UMV_CONTESTA_CPU_EXITO) {
		log_debug(logger, "Failure handshaking umv");
		destruirPaquete(paquete);
		destroyOnConnectionFailure();
	}
	log_debug(logger, "Successful handshake with UMV");
	destruirPaquete(paquete);
}
Beispiel #10
0
void
startCommunicationWithUMV()
{
  t_datosEnviar *mensaje = pedirPaquete("OK", UMV_ACEPTA_KERNEL, 3);
  socketUMV = connect_to(configuration.ipUMV, configuration.puertoUMV);
  common_send(socketUMV, mensaje, NULL );
  mensaje = common_receive(socketUMV, NULL );
  if (mensaje->codigo_Operacion != UMV_ACEPTA_KERNEL)
    {
      shutdownKernel();
      log_error(log_kernel,
          "No se pudo conectar con la UMV, cerrando el Kernel");
    }
  log_info(log_kernel, "Se conecto exitosamente con la UMV");
}
Beispiel #11
0
void printVariable(t_nodo_proceso_ejecutando* procesoEjecutando,
		struct pollfd** socketsCPU, int* cantSocketsCpu, t_datosEnviar* paquete)
{
	debugTrackPCP(" Request to print variable: %d", *(int*)paquete->data);
	log_info(log_kernel,
			"Request to print variable \" %d \", requested by the CPU with pid %d\n",
			paquete->data, procesoEjecutando->cpu.pid);

	char* traduccion = string_itoa(*((int*) paquete->data));
	t_datosEnviar* mensaje = pedirPaquete(traduccion,
			PROGRAMA_PAQUETE_PARA_IMPRIMIR, string_length(traduccion) + 1);
	common_send(procesoEjecutando->proceso.soquet_prog, mensaje, NULL );
	destruirPaquete(mensaje);
	free(traduccion);
}
Beispiel #12
0
t_datos_kernel doHandshakeKernel() {
	int pidCPU = getpid();
	t_datosEnviar* paquete = pedirPaquete(&pidCPU, HANDSHAKE, sizeof(int));
	common_send(pcpSocket, paquete, destroyOnConnectionFailure);
	destruirPaquete(paquete);
	paquete = common_receive(pcpSocket, NULL );
	if (paquete->codigo_Operacion != PCP_CPU_OK) {
		log_debug(logger, "Failure handshaking kernel");
		destruirPaquete(paquete);
		destroyOnConnectionFailure();
	}
	log_debug(logger, "Successful handshake with Kernel");
	t_datos_kernel kernelData = getKernelData(paquete->data);
	destruirPaquete(paquete);
	return kernelData;

}
Beispiel #13
0
void exceptionCPU(t_nodo_proceso_ejecutando* procesoEjecutando,
		struct pollfd** socketsCPU, int* cantSocketsCpu, t_datosEnviar* paquete)
{

	t_pcb* pcb = unserializePCB(paquete->data);
	char* textoError = malloc(paquete->data_size - sizeof(t_pcb));
	memcpy(textoError, paquete->data + sizeof(t_pcb),
			paquete->data_size - sizeof(t_pcb));

	debugTrackPCP(
			" Exception on the process with pid: %d, on the CPU  %d. Exception details: %s",
			pcb->pid, procesoEjecutando->cpu.pid, textoError);
	log_warning(log_kernel, "Exception on process. Exception details: %s",textoError);

	procesoEjecutando->proceso.pcb = *pcb;
	debugTrackPCP("Updating PCB.");
	t_datosEnviar* mensaje = pedirPaquete(textoError,
			PROGRAMA_PAQUETE_PARA_IMPRIMIR, string_length(textoError) + 1);
	common_send(procesoEjecutando->proceso.soquet_prog, mensaje, NULL );
	destruirPaquete(mensaje);
	pcp_exitProcess(procesoEjecutando);
	free(procesoEjecutando);
	free(textoError);
}
Beispiel #14
0
void
atenderProgramaEntrante(t_datosEnviar* paquete, int socketCliente,
    int * PIDActual)
{
  if (paquete->codigo_Operacion == HANDSHAKE)
    {
      destruirPaquete(paquete);
      t_datosEnviar *paqueteEscribir;
      void *dataGenerica;
      paquete = pedirPaquete("K", PLP_PROGRAMA_CONFIRMAR_CONECCION, 2);
      common_send(socketCliente, paquete, NULL );
      destruirPaquete(paquete);
      paquete = common_receive(socketCliente, NULL );
      if (paquete->codigo_Operacion == ENVIAR_SCRIPT)
        {

          debugTrackPLP(
              "[Achievement] Programa autenticado, recibiendo script");
          char* programa = malloc(paquete->data_size);
          memcpy(programa, paquete->data, paquete->data_size);

          t_metadata_program * metadataDelPrograma;
          metadataDelPrograma = metadata_desde_literal((const char*) programa);
          int peso;
          peso = metadataDelPrograma->cantidad_de_funciones * 3
              + metadataDelPrograma->cantidad_de_etiquetas * 5
              + metadataDelPrograma->instrucciones_size;

          int error = 0;
          t_pcb * pcbNuevo = malloc(sizeof(t_pcb));

          pcbNuevo->pid = generarPid(PIDActual);

          int direccionStack, direccionCodigo, direccionIndiceEtiquetas,
              direccionIndiceCodigo;

          destruirPaquete(paquete);
          error = 0;

          debugTrackPLP("[Accion] Pidiendo segmentos en a la UMV");

          direccionCodigo = pedirSegmento(&error, strlen(programa) + 1,
              pcbNuevo->pid);
          direccionStack = pedirSegmento(&error, configuration.tamanio_pila,
              pcbNuevo->pid);
          direccionIndiceEtiquetas = pedirSegmento(&error,
              metadataDelPrograma->etiquetas_size, pcbNuevo->pid);
          direccionIndiceCodigo = pedirSegmento(&error,
              metadataDelPrograma->instrucciones_size * sizeof(t_intructions),
              pcbNuevo->pid);

          if (error == 1)
            {
              debugTrackPLP(
                  "[Error] No se pudo crear la totalidad de segmentos");
              paquete = pedirPaquete("A", PLP_PROGRAMA_NO_SE_PUDO_CREAR, 2);
              common_send(socketCliente, paquete, NULL );
              destruirPaquete(paquete);
              void * pidMomentaneo = malloc(sizeof(int));
              memcpy(pidMomentaneo, &(pcbNuevo->pid), sizeof(int));
              paquete = pedirPaquete(pidMomentaneo,
                  KERNEL_DESTRUIR_SEGMENTOS_PROGRAMA, sizeof(int));
              common_send(socketUMV, paquete, NULL );
              debugTrackPLP(
                  "[Accion] Eliminando el resto de los segmentos pedidos");
              PIDActual--;
              destruirPaquete(paquete);
              free(pidMomentaneo);
            }
          else
            {
              debugTrackPLP(
                  "[PID:%d]-[Achievement] Se pudo alocar todos los segmentos para el proceso ",
                  pcbNuevo->pid);
              pcbNuevo->codeIndex = direccionIndiceCodigo;
              pcbNuevo->codeSegment = direccionCodigo;
              pcbNuevo->indiceEtiquetas = direccionIndiceEtiquetas;
              pcbNuevo->programCounter =
                  metadataDelPrograma->instruccion_inicio;
              pcbNuevo->tamanioIndiceEtiquetas =
                  metadataDelPrograma->etiquetas_size;
              pcbNuevo->currentStack.base = direccionStack;
              pcbNuevo->currentStack.contextSize = 0;
              pcbNuevo->currentStack.size = configuration.tamanio_pila;
              pcbNuevo->currentStack.stack_pointer = direccionStack;

              if (pcbNuevo->codeSegment != -1)
                {
                  debugTrackPLP(
                      "[PID:%d]-[Achievement] Alocado segmento de codigo en la direccion virtual %d",
                      pcbNuevo->pid, pcbNuevo->codeSegment);

                  dataGenerica = malloc(
                      3 * sizeof(uint32_t) + strlen(programa) + 1);
                  int tamanioData = strlen(programa) + 1;

                  memcpy(dataGenerica, &(pcbNuevo->pid), sizeof(uint32_t));
                  memcpy(dataGenerica + sizeof(uint32_t), &tamanioData,
                      sizeof(uint32_t));
                  memcpy(dataGenerica + 2 * sizeof(uint32_t),
                      &(pcbNuevo->codeSegment), sizeof(uint32_t));
                  memcpy(dataGenerica + 3 * sizeof(uint32_t), programa,
                      tamanioData);

                  paqueteEscribir = pedirPaquete(dataGenerica,
                      KERNEL_ESCRIBIR_SEGMENTO_UMV,
                      3 * sizeof(uint32_t) + strlen(programa) + 1);
                  common_send(socketUMV, paqueteEscribir, NULL );
                  destruirPaquete(paqueteEscribir);
                  paqueteEscribir = common_receive(socketUMV, NULL );
                  free(dataGenerica);
                  destruirPaquete(paqueteEscribir);

                }
              if (pcbNuevo->codeIndex != -1)
                {
                  debugTrackPLP(
                      "[PID:%d]-[Achievement] Alocado segmento de indice de codigo en la direccion virtual %d",
                      pcbNuevo->pid, pcbNuevo->codeIndex);
                  int tamanioData = metadataDelPrograma->instrucciones_size
                      * sizeof(t_intructions);
                  paqueteEscribir = pedirPaquete(&(pcbNuevo->pid),
                      KERNEL_ESCRIBIR_SEGMENTO_UMV, sizeof(int));
                  aniadirAlPaquete(paqueteEscribir, &(tamanioData),
                      sizeof(int));
                  aniadirAlPaquete(paqueteEscribir, &(pcbNuevo->codeIndex),
                      sizeof(int));
                  aniadirAlPaquete(paqueteEscribir,
                      metadataDelPrograma->instrucciones_serializado,
                      tamanioData);
                  common_send(socketUMV, paqueteEscribir, NULL );
                  destruirPaquete(paqueteEscribir);
                  paqueteEscribir = common_receive(socketUMV, NULL );
                  destruirPaquete(paqueteEscribir);

                }
              if (pcbNuevo->indiceEtiquetas != -1)
                {

                  debugTrackPLP(
                      "[PID:%d]-[Achievement] Alocado segmento de indice de etiquetas en la direccion virtual %d",
                      pcbNuevo->pid, pcbNuevo->indiceEtiquetas);

                  int tamanioData = metadataDelPrograma->etiquetas_size;
                  dataGenerica = malloc(
                      (tamanioData + 1) * sizeof(char) + 3 * sizeof(int));
                  memcpy(dataGenerica, &(pcbNuevo->pid), sizeof(int));
                  memcpy(dataGenerica + sizeof(int), &tamanioData, sizeof(int));
                  memcpy(dataGenerica + 2 * sizeof(int),
                      &(pcbNuevo->indiceEtiquetas), sizeof(int));
                  memcpy(dataGenerica + 3 * sizeof(int),
                      metadataDelPrograma->etiquetas, tamanioData);
                  paqueteEscribir = pedirPaquete(dataGenerica,
                      KERNEL_ESCRIBIR_SEGMENTO_UMV, (tamanioData + 1) * sizeof(char) + 3 * sizeof(int));
                  common_send(socketUMV, paqueteEscribir, NULL );
                  destruirPaquete(paqueteEscribir);
                  paqueteEscribir = common_receive(socketUMV, NULL );
                  destruirPaquete(paqueteEscribir);
                  free(dataGenerica);

                }

              debugTrackPLP("[PID:%d]-[Achievement] PCB listo para encolarse",
                  pcbNuevo->pid);

              paquete = pedirPaquete("A", PLP_PROGRAMA_ENCOLADO, 2);
              common_send(socketCliente, paquete, NULL );
              free(programa);
              encolarPCB(pcbNuevo, peso, socketCliente);

            }
        }

      else
        {
          destruirPaquete(paquete);
          pedirPaquete("NK", PROGRAMA_CERRAR, 3);
          puts("No Validado");
          common_send(socketCliente, paquete, NULL );
          destruirPaquete(paquete);
        }
    }

}
Beispiel #15
0
int main(int argc, char **argv) {

	remove("/tp-2015-2c-signiorcodigo/swap/log_swap");

	swapConfig = config_create("/tp-2015-2c-signiorcodigo/swap/swapConfig");

	setupSwap();

	setPages();

	int socketEscucha, socketMemoria;

	char * puerto = getPort();

	socketEscucha = setup_listen("localhost", puerto);

	socketMemoria = esperarConexionEntrante(socketEscucha, 1024, log_swap);

	t_data * paqueteInicio = leer_paquete(socketMemoria);

	if (paqueteInicio->header->codigo_operacion == 1) {

		int datonulo = 0;

		paqueteInicio = pedirPaquete(2, sizeof(int), &datonulo);

		common_send(socketMemoria, paqueteInicio);

		log_info(log_swap, "Conectado con la memoria en el socket: %d",
				socketMemoria);

	} else {

		log_info(log_swap, "Falla en la conexion con la memoria");

		exit(EXIT_FAILURE);
	}

	t_data * paquete;

	while (1) {

		paquete = leer_paquete(socketMemoria);

		switch (paquete->header->codigo_operacion) {

		case LEER: {

			int pid, page;

			memcpy(&pid, paquete->data, sizeof(int));
			memcpy(&page, paquete->data + sizeof(int), sizeof(int));

			char * content = readProcessPage(pid, page);

			paquete = pedirPaquete(LEER, getSwapPagesSize(), content);

			common_send(socketMemoria, paquete);

			break;
		}

		case ESCRIBIR: {

			int pid, page;
			char * content = malloc(getSwapPagesSize());

			memcpy(&pid, paquete->data, sizeof(int));
			memcpy(&page, paquete->data + sizeof(int), sizeof(int));
			memcpy(content, paquete->data + 2 * sizeof(int),
					getSwapPagesSize());

			writeProcessPage(pid, page, content);

			break;
		}

		case INICIAR: {

			int pid, pagesAmount;

			memcpy(&pid, paquete->data, sizeof(int));
			memcpy(&pagesAmount, paquete->data + sizeof(int), sizeof(int));

			int blank_pages = getBlankPages();
			int success;

			if (blank_pages >= pagesAmount) {
				success = reserve(pid, pagesAmount);

				if (success == -1) {
					compact();
					success = reserve(pid, pagesAmount);
				}
			}else{
				success = -1;
			}


			if (success == -1) {

				paquete = pedirPaquete(0, sizeof(int), &pid);
				log_info(log_swap,
						"No se pudo reservar las paginas solicitadas para el proceso con pid: %d",
						pid);

			} else {

				paquete = pedirPaquete(1, sizeof(int), &pid);
				int absolutePage = getProcessFirstPage(pid);

				int byteInicial = absolutePage * getSwapPagesSize();

				int size = pagesAmount * getSwapPagesSize();

				log_info(log_swap,
						"Se inicia el proceso %d en el byte %d con tamanio %d",
						pid, byteInicial, size);

			}

			common_send(socketMemoria, paquete);

			break;
		}

		case FINALIZAR: {

			int pid;

			memcpy(&pid, paquete->data, sizeof(int));

			int processSpace = getProcessReservedSpace(pid)
					* getSwapPagesSize();

			int byteInicial = getProcessFirstPage(pid) * getSwapPagesSize();

			freeSpace(pid);

			log_info(log_swap,
					"Proceso %d liberado. N° de byte inicial: %d, y tamaño en bytes: %d",
					pid, byteInicial, processSpace);

			break;
		}

		default: {

			break;
		}

		}

	}

	return 0;
}
t_datosEnviar* empaquetarPCB(int codOp) {
	return pedirPaquete(currentPCB, codOp, sizeof(t_pcb));
}