void *operatore(){ while(1){ //generatore di numeri random usato da wait_client() e ask_confirmation() srand(time(NULL)); //attende un cliente wait_client(); //proteggo l'accesso dell'operatore al db pthread_mutex_lock(&op_lock); active_op++; if (active_op==1){ pthread_mutex_lock(&db_lock); } pthread_mutex_unlock(&op_lock); //controllo disponibilita' if (check_availability()){ pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); //se il posto e' disponibile chiedo conferma della prenotazione if (ask_confirmation()){ pthread_mutex_lock(&db_lock); //avuta la conferma dal cliente, ricontrollo se il posto sia ancora libero if (check_availability()){ //ricevuta la conferma, prenoto il posto e rilascio il lock sul db book_seat(); pthread_mutex_unlock(&db_lock); //confermo al cliente l'avvenuta prenotazione confirm_booking(); } else { pthread_mutex_unlock(&db_lock); printf("Ci dispiace, qualcuno ha prenotato il posto prima di lei\n"); } } else { //prenotazione non confermata, rilascio il lock sul db pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); printf("Ci dispiace che abbia cambiato idea, speriamo di rivederla al piu' presto!\n"); } } else { pthread_mutex_lock(&op_lock); active_op--; if (active_op==0){ pthread_mutex_unlock(&db_lock); } pthread_mutex_unlock(&op_lock); printf("Ci dispiace, il viaggio che le interessa e' gia' al completo! Speriamo di rivederla presto.\n"); break; } } }
static pid_t child_make(int i, int listenfd) { pid_t pid; if ((pid = fork_wrapper()) > 0) return pid; while(true) { wait_client(listenfd); start_transaction(); } exit(0); }
int main() { int listen_socket = creat_socket(); while(1) { if (count_client < 100) { int client_socket = wait_client(listen_socket); pthread_t id; pthread_create(&id, NULL, hanld_client, (void *)&client_socket); pthread_detach(id); //把线程分离出去。 } else { sleep(5); //单位是秒 } } close(listen_socket); return 0; }
void main(int argc, char** argv) { int loops = 0; void * cfw_channel; if (argc > 1) { baud_rate = baudrate_to_termios(strtol(argv[1], NULL, 0)); } else { baud_rate = baudrate_to_termios(0); /* forcing 115200 */ } /* Set the remote CPU handlers */ set_cpu_message_sender(CPU_ID_BLE, linux_ipc_send_message); set_cpu_message_sender(CPU_ID_ARC, linux_ipc_send_message); set_cpu_message_sender(CPU_ID_HOST, linux_ipc_send_message); set_cpu_free_handler(CPU_ID_BLE, linux_ipc_free); set_cpu_free_handler(CPU_ID_ARC, linux_ipc_free); set_cpu_free_handler(CPU_ID_HOST, linux_ipc_free); set_cpu_id(CPU_ID_QUARK); int uart_fd = wait_client(SOCKET_NAME, UART_PORT); printf("Client connected on %d\n", uart_fd); uart_ipc_init(uart_fd); fd_set fds; FD_ZERO(&fds); struct timeval tv; _server_cb.queue = queue_create(10, NULL); cfw_service_mgr_init(_server_cb.queue); /* We use the main task queue to support most services */ set_service_queue(_server_cb.queue); _server_cb.client = (cfw_client_t *)cfw_client_init(_server_cb.queue, srv_client_handle_msg, &_server_cb); /* get informed on new services */ cfw_register_svc_available(_server_cb.client, BLE_CORE_SERVICE_ID, &_server_cb); /* some test services */ test_service_init(_server_cb.queue, TEST_SERVICE_ID); /* ble service */ ble_cfw_service_init(_server_cb.queue); cfw_channel = ipc_uart_channel_open(0, ipc_uart_message_cback); linux_ipc_set_channel(cfw_channel); while(1) { FD_SET(uart_fd, &fds); FD_SET(0, &fds); tv.tv_sec = 1; tv.tv_usec = 0; int ret = select(uart_fd + 1, &fds, NULL, NULL, &tv); if (ret == -1) { perror("select()"); } else if (ret) { if (FD_ISSET(0, &fds)) { } else if(FD_ISSET(uart_fd, &fds)) { uart_ipc_handle_data(uart_fd); } } else { printf("."); #ifdef CONFIG_BLE_CORE_TEST test_ble_service_update_battery_level(battery_level--); if (battery_level == 10) battery_level = 100; #endif } cfw_process(_server_cb.queue); } }
/* * ************************************************************* * main of connection server * ************************************************************* */ int main(int argc, char *argv[]) { srv_opts_t run_options; int RequestCmd; void *res; initialize( &run_options ); get_arguments ( argc, argv, &run_options ); if( run_options.run_as_daemon ) { if( (run_options.error_code = start_daemon( &run_options )) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } } if( (run_options.error_code = make_socket( &run_options )) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } if( prepareThreads( &run_options ) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } else { if( (run_options.error_code = ctl_create_thread( &run_options, MAINCTL_THREAD_NUM)) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } } // while no exit request RequestCmd = 0; do { if( (run_options.error_code = wait_client( &run_options )) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } else { if( (run_options.error_code = get_request( &run_options )) != E_NOERR ) { do_log(&run_options, LOG_CRITERROR); cleanup_and_exit( &run_options ); } else { switch( RequestCmd = parse_req( run_options.p_rcv_buf ) ) { case CTLREQ_EXIT: sprintf(run_options.p_rsp_buf, "Assume exit: %s\n", run_options.p_rcv_buf ); send_response(&run_options); close(run_options.cli_socket); break; case CTLREQ_API: sprintf(run_options.p_rsp_buf, "Assume API: %s\n", run_options.p_rcv_buf ); send_response(&run_options); close(run_options.cli_socket); break; default: // assume telnet request sprintf(run_options.p_rsp_buf, "Assume telnet: %s\n", run_options.p_rcv_buf ); send_response(&run_options); close(run_options.cli_socket); break; } } } } while( RequestCmd != CTLREQ_EXIT ); if( pthread_attr_destroy(&run_options.tattr) != 0 ) { run_options.error_code = E_FAIL; run_options.sys_errno = errno; sprintf( run_options.logbuffer, "destroy attr failed, errno was %d\n", errno ); do_log(&run_options, LOG_WARN); } run_options.mainctl_run = 0; run_options.api_run = 0; run_options.telnet_run = 0; if( run_options.tinfo[MAINCTL_THREAD_NUM].thread_id >= 0 ) { pthread_join(run_options.tinfo[MAINCTL_THREAD_NUM].thread_id, &res); } if( run_options.tinfo[API_THREAD_NUM].thread_id >= 0 ) { pthread_join(run_options.tinfo[API_THREAD_NUM].thread_id, &res); } if( run_options.tinfo[TELNET_THREAD_NUM].thread_id >= 0 ) { pthread_join(run_options.tinfo[TELNET_THREAD_NUM].thread_id, &res); } pthread_cancel(run_options.tinfo[MAINCTL_THREAD_NUM].thread_id); pthread_cancel(run_options.tinfo[API_THREAD_NUM].thread_id); pthread_cancel(run_options.tinfo[TELNET_THREAD_NUM].thread_id); // and die cleanup_and_exit( &run_options ); }