void * threadPLP(void* vacio) { int PIDActual = 0; setvbuf(stdout, NULL, _IONBF, 0); log_info(log_kernel,"Se inicio el PLP"); pthread_t hilo_PLP_destruccion; pthread_create(&hilo_PLP_destruccion, NULL, hiloDestruccion, NULL ); int socketEscucha; socketEscucha = setup_listen(configuration.ipKernel,configuration.puertoProg); log_info(log_kernel,"El PLP escucha en el socket %d",socketEscucha); while (1) { t_datosEnviar * paquete; listen(socketEscucha, 1000); struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int socketCliente; socketCliente = accept(socketEscucha, (struct sockaddr *) &addr, &addrlen); log_info(log_kernel,"Se recibio una script nueva en el socket %d",socketCliente); paquete = common_receive(socketCliente, NULL ); atenderProgramaEntrante(paquete, socketCliente, &PIDActual); } pthread_join(hilo_PLP_destruccion, NULL ); return NULL ; }
int main(int argc, char **argv) { int pth1; pthread_t thread_serv_listen; pthread_attr_t attr; // set of attributes for the thread pthread_attr_init(&attr); // get the default attributes s = setup_listen(SERVER_PORT); if (s < 0) { printf("error opening server\n"); exit(1); }else{ printf("server started\n"); } if( (pth1 = pthread_create( &thread_serv_listen, &attr, server_listen, NULL)) ){ printf("Server listen thread creation failed: %d\n", pth1); }else{ printf("Server thread created\n"); } while(1){ // printf("while1 read temp\n"); usleep(1000000); // cakamo eno sekundo buf_put((element_t)get_temp()); } close(s); return 0; }
void * threadPLP(void* vacio) { int PIDActual = 0; setvbuf(stdout, NULL, _IONBF, 0); puts("[KERNEL] - [PLP] - ESCUCHANDO\n"); pthread_t hilo_PLP_destruccion; pthread_t hilo_PLP_multiprogramacion; pthread_create(&hilo_PLP_destruccion, NULL, hiloDestruccion, NULL ); pthread_create(&hilo_PLP_multiprogramacion, NULL, hiloMultiprogramacion, NULL ); int socketEscucha; socketEscucha = setup_listen(configuration.puertoProg); while (1) { setvbuf(stdout, NULL, _IONBF, 0); t_datosEnviar * paquete; listen(socketEscucha, 1000); struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int socketCliente; socketCliente = accept(socketEscucha, (struct sockaddr *) &addr, &addrlen); paquete = common_receive(socketCliente, NULL ); atenderProgramaEntrante(paquete, socketCliente, &PIDActual); } pthread_join(hilo_PLP_destruccion, NULL ); pthread_join(hilo_PLP_multiprogramacion, NULL ); return NULL ; }
int main(int argc, char **argv) { char buf[1000]; int socket_listen; int socket_talk; int dummy, len; threadpool pool; struct timeval then, now, diff; if (argc != 2) { fprintf(stderr, "(SERVER): Invoke as './server socknum'\n"); fprintf(stderr, "(SERVER): for example, './server 4434'\n"); exit(-1); } /* * Set up the 'listening socket'. This establishes a network * IP_address:port_number that other programs can connect with. */ pool = create_threadpool(POOL_SIZE,NULL); socket_listen = setup_listen(argv[1]); /* * Here's the main loop of our program. Inside the loop, the * one thread in the server performs the following steps: * * 1) Wait on the socket for a new connection to arrive. This * is done using the "accept" library call. The return value * of "accept" is a file descriptor for a new data socket associated * with the new connection. The 'listening socket' still exists, * so more connections can be made to it later. * * 2) Read a request off of the listening socket. Requests * are, by definition, REQUEST_SIZE bytes long. * * 3) Process the request. * * 4) Write a response back to the client. * * 5) Close the data socket associated with the connection */ gettimeofday(&then, NULL); while (1) { socket_talk = saccept(socket_listen); // step 1 if (socket_talk < 0) { fprintf(stderr, "An error occured in the server; a connection\n"); fprintf(stderr, "failed because of "); perror(""); exit(1); } dispatch(pool, request_client, (void *) socket_talk); count_dispatches++; if (count_dispatches == 10) { double seconds; //elapsed time in seconds between then and now gettimeofday(&now, NULL); timersub(&now, &then, &diff); seconds = 1000000*diff.tv_sec + diff.tv_usec; fprintf(stdout, "Loops: %6d Threads: %2d Dispatches/sec: %10.3f\n", NUM_LOOPS, POOL_SIZE, seconds/1000); } } }
THREAD_RV THREAD_CC channel_thread_loop(void* in_val) { tbus objs[32]; int num_objs; int timeout; int error; THREAD_RV rv; log_message(&log_conf, LOG_LEVEL_DEBUG, "chansrv[channel_thread_loop]: " "channel_thread_loop: thread start"); rv = 0; error = setup_listen(); if (error == 0) { timeout = 0; num_objs = 0; objs[num_objs] = g_term_event; num_objs++; trans_get_wait_objs(g_lis_trans, objs, &num_objs, &timeout); while (g_obj_wait(objs, num_objs, 0, 0, timeout) == 0) { if (g_is_wait_obj_set(g_term_event)) { log_message(&log_conf, LOG_LEVEL_DEBUG, "chansrv[channel_thread_loop]: " "channel_thread_loop: g_term_event set"); //clipboard_deinit(); //sound_deinit(); //dev_redir_deinit(); //seamrdp_deinit(); user_channel_deinit(); break; } if (g_lis_trans != 0) { if (trans_check_wait_objs(g_lis_trans) != 0) { log_message(&log_conf, LOG_LEVEL_WARNING, "chansrv[channel_thread_loop]: " "trans_check_wait_objs error"); } } if (g_con_trans != 0) { if (trans_check_wait_objs(g_con_trans) != 0) { log_message(&log_conf, LOG_LEVEL_WARNING, "chansrv[channel_thread_loop]: " "trans_check_wait_objs error resetting"); //clipboard_deinit(); //sound_deinit(); //dev_redir_deinit(); //seamrdp_deinit(); user_channel_deinit(); trans_delete(g_con_trans); g_con_trans = 0; error = setup_listen(); if (error != 0) { break; } } } //clipboard_check_wait_objs(); //sound_check_wait_objs(); //dev_redir_check_wait_objs(); //seamrdp_check_wait_objs(); user_channel_check_wait_objs(); timeout = 0; num_objs = 0; objs[num_objs] = g_term_event; num_objs++; trans_get_wait_objs(g_lis_trans, objs, &num_objs, &timeout); trans_get_wait_objs(g_con_trans, objs, &num_objs, &timeout); //clipboard_get_wait_objs(objs, &num_objs, &timeout); //sound_get_wait_objs(objs, &num_objs, &timeout); //dev_redir_get_wait_objs(objs, &num_objs, &timeout); //seamrdp_get_wait_objs(objs, &num_objs, &timeout); user_channel_get_wait_objs(objs, &num_objs, &timeout); } } trans_delete(g_lis_trans); g_lis_trans = 0; trans_delete(g_con_trans); g_con_trans = 0; log_message(&log_conf, LOG_LEVEL_DEBUG, "chansrv[channel_thread_loop]: " "channel_thread_loop: thread stop"); g_set_wait_obj(g_thread_done_event); return rv; }
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; }
THREAD_RV THREAD_CC channel_thread_loop(void *in_val) { tbus objs[32]; int num_objs; int timeout; int error; THREAD_RV rv; LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread start")); rv = 0; setup_api_listen(); error = setup_listen(); if (error == 0) { timeout = -1; num_objs = 0; objs[num_objs] = g_term_event; num_objs++; trans_get_wait_objs(g_lis_trans, objs, &num_objs); trans_get_wait_objs(g_api_lis_trans, objs, &num_objs); while (g_obj_wait(objs, num_objs, 0, 0, timeout) == 0) { if (g_is_wait_obj_set(g_term_event)) { LOGM((LOG_LEVEL_INFO, "channel_thread_loop: g_term_event set")); clipboard_deinit(); sound_deinit(); dev_redir_deinit(); rail_deinit(); break; } if (g_lis_trans != 0) { if (trans_check_wait_objs(g_lis_trans) != 0) { LOGM((LOG_LEVEL_INFO, "channel_thread_loop: " "trans_check_wait_objs error")); } } if (g_con_trans != 0) { if (trans_check_wait_objs(g_con_trans) != 0) { LOGM((LOG_LEVEL_INFO, "channel_thread_loop: " "trans_check_wait_objs error resetting")); clipboard_deinit(); sound_deinit(); dev_redir_deinit(); rail_deinit(); /* delete g_con_trans */ trans_delete(g_con_trans); g_con_trans = 0; /* create new listener */ error = setup_listen(); if (error != 0) { break; } } } if (g_api_lis_trans != 0) { if (trans_check_wait_objs(g_api_lis_trans) != 0) { LOG(0, ("channel_thread_loop: trans_check_wait_objs failed")); } } LOG(10, ("0 %p", g_api_con_trans)); if (g_api_con_trans != 0) { LOG(10, ("1 %p %d", g_api_con_trans, g_tcp_can_recv(g_api_con_trans->sck, 0))); if (trans_check_wait_objs(g_api_con_trans) != 0) { LOG(10, ("channel_thread_loop: trans_check_wait_objs failed, " "or disconnected")); g_free(g_api_con_trans->callback_data); trans_delete(g_api_con_trans); g_api_con_trans = 0; } } xcommon_check_wait_objs(); sound_check_wait_objs(); dev_redir_check_wait_objs(); fuse_check_wait_objs(); timeout = -1; num_objs = 0; objs[num_objs] = g_term_event; num_objs++; trans_get_wait_objs(g_lis_trans, objs, &num_objs); trans_get_wait_objs(g_con_trans, objs, &num_objs); trans_get_wait_objs(g_api_lis_trans, objs, &num_objs); trans_get_wait_objs(g_api_con_trans, objs, &num_objs); xcommon_get_wait_objs(objs, &num_objs, &timeout); sound_get_wait_objs(objs, &num_objs, &timeout); dev_redir_get_wait_objs(objs, &num_objs, &timeout); fuse_get_wait_objs(objs, &num_objs, &timeout); } } trans_delete(g_lis_trans); g_lis_trans = 0; trans_delete(g_con_trans); g_con_trans = 0; trans_delete(g_api_lis_trans); g_api_lis_trans = 0; trans_delete(g_api_con_trans); g_api_con_trans = 0; LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread stop")); g_set_wait_obj(g_thread_done_event); return rv; }