|
|
|
//鍐欎竴娈电畝鍗曠殑浠诲姟闂ㄦ祴璇曠▼搴
- // 鍐欎竴娈电畝鍗曠殑浠诲姟闂ㄦ祴璇曠▼搴#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/types.h>#include <sys/wait.h>#include <errno.h>int main(int argc, char *argv[]){ int i; int status; int n = 5; pid_t pid; for (i = 0; i < n; i++) { pid = fork(); if (pid == 0) { break; } else if (pid < 0) { perror("fork"); exit(1); } else { continue; } } if (i == n) { while (wait(&status) > 0) { if (WIFEXITED(status)) { printf("child %d exit with code %d\n", WEXITSTATUS(status)); continue; } else if (WIFSIGNALED(status)) { printf("child %d exit with signal %d\n", WTERMSIG(status)); continue; } else if (WIFSTOPPED(status)) { printf("child %d exit with signal %d\n", WSTOPSIG(status)); continue; } else { printf("unknow status\n"); continue; } } if (errno != ECHILD) { perror("wait"); exit(1); } else { printf("all child exit\n"); return 0; } } else { sleep(i); printf("child %d exit\n", i); return 0; } return 0;}
复制代码 |
|