// 写一段简单的任务门测试程序
#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;
}
写一段简单的任务门测试程序
最新推荐文章于 2024-07-08 22:12:26 发布
本文介绍了一个使用C语言编写的简单任务门测试程序,通过`fork()`创建多个子进程,并利用`wait()`函数监控子进程的退出状态。程序演示了如何处理不同类型的子进程退出情况。
摘要由CSDN通过智能技术生成