static void init ( ) { MaskSignal ( SIGUSR1 ); // Respond to SIGUSR2 (= controlled destruction) SetSignalHandler ( SIGUSR2, die ); // Respond to SIGCHLD (= valet has finished parking car) SetSignalHandler ( SIGCHLD, ack ); } // Fin de init
////////////////////////////////////////////////////////////////// PUBLIC //---------------------------------------------------- Fonctions publiques void Entrance ( TypeBarriere entrance ) // Algorithme : // 1. Dequeue a car from the mailbox (or wait for a car to arrive in // the mailbox) and decrement the number of available spots // 2. Read the number of available spots (from the shared memory) // 4. If there's enough space to go in directly, go to 6 // 5. Otherwise, place a request in the shared memory and wait for SIGUSR1 // 6. A spot is available for us, call GarerVoiture() // 7. Sleep for <ENTRANCE_SLEEP_DELAY> seconds // 8. Goto step 1. { //----------------------------------------------------- INITIALIZATION init ( ); //---------------------------------------------------------------- RUN for ( ; ; ) { Car next = waitForCar ( entrance ); int freeSpotsLeft = decrementFreeSpots ( ); DessinerVoitureBarriere ( entrance, next.priority ); // If there's no free spot right now, place a request // and wait patiently to be signaled by the exit gate // TODO: let GarerVoiture make the canGoIn check? if ( freeSpotsLeft < 0 ) { authorizationReceived = false; placeRequest ( entrance, next ); SetSignalHandler ( SIGUSR1, authorize ); while ( !authorizationReceived ) { pause ( ); } MaskSignal ( SIGUSR1 ); } // Allow the car to go in pid_t valetPid = GarerVoiture ( entrance ); currentValets[valetPid] = next; // Sleep for at least <ENTRANCE_SLEEP_DELAY> // before allowing another car in unsigned int remaining = ENTRANCE_SLEEP_DELAY; while ( remaining > 0 ) { remaining = sleep ( remaining ); } } } // Fin de Entrance
static void die ( int signalNumber ) // Mode d'emploi : // Use for controlled destruction. Kills any child task. { MaskSignal ( SIGCHLD ); // Kill every running GarerVoiture for ( std::map<pid_t, Car>::iterator it = currentValets.begin(); it != currentValets.end(); ++it ) { kill ( it->first, SIGUSR2 ); waitpid( it->first, NULL, 0 ); } exit ( 0 ); } // Fin de die
//////////////////////////////////////////////////////////////////////////////// /// 名 称:TCPSocket::Init /// 功 能:初始化对象 /// 参 数:void /// 返回 值:bool public //////////////////////////////////////////////////////////////////////////////// bool TCPSocket::Init() { // 读取配置文件 if (!LoadConfig()) { return false; } // 设置读写ID口文件最大数 SetLimitMax(); // 显示当前Socket配置情况 if (!ShowSocketStatus()) { return false; } // 初始化线程池 if (!ThreadPollInit()) { return false; } // 屏蔽多重发送的报错信号 if (!MaskSignal()) { return false; } // 创建Socket if (!CreateSocket()) { return false; } // 创建Epoll if (!CreateEpoll()) { return false; } // 绑定Socket if (!BindSocket()) { return false; } // 开启监听该Socket if (!ListenSocket()) { return false; } return true; }