int main(int argc, const char *argv[]) { pid_t pid; CLEAR(); HIDE_CURSOR(); //开启终端读取一个字符的功能 disable_terminal_return(); //创建一个线程 pid = fork(); PERROR("fork", pid); //进入父进程//控制游戏的基本运行 if (pid > 0) { //延时1秒,等待子进程就绪 sleep(1); father_process(); } //进入子进程//控制玩家的操作 if (pid == 0) { child_process(); } CLEAR(); RESET_CURSOR(); SHOW_CURSOR(); return 0; }
int main (int argc, char* argv[]) { int pongPipe[2]; int pingPipe[2]; pid_t child; // initialise le pipe. pipe(pongPipe); pipe(pingPipe); // divise le processus. child = fork(); if(child == -1){ // décris l'erreur. perror("Fork error"); // ferme les descripteur ouvert. close(pingPipe[1]); close(pongPipe[0]); close(pongPipe[1]); close(pingPipe[0]); // arrêt suite à une erreur. exit(EXIT_FAILURE); } if(child == 0){ // lance le code du fils. _exit(child_process(pongPipe, pingPipe)); } else{ // lance le code du père. exit(father_process(child, pongPipe, pingPipe)); } }