//写一段简单的任务门测试程序
- // 写一段简单的任务门测试程序
- 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;
- }
没有回复内容