child 进程未在 c 中正确终止
child processes are not terminating correctly in c
在学习分叉和进程方面仍然是新手,我的任务是创建 3 个 child 进程执行一些操作,然后 parent 应该在它们终止时打印退出状态。
我遇到的问题是 child 1 提前终止,我认为我没有正确使用 wait()。
这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
pid_t child1,child2, child3,wpid;
int child1Status,child2Status,child3Status;
child1 = fork();
if (child1 == 0){
float marks[8];
float average = 0.0;
float sum = 0.0;
printf("I am child one my pid is %d \n",getpid());
printf("Please enter 8 marks and I will calcuate the average and highest mark\n");
for (int i = 0; i < 8; ++i) {
printf("%i) ",i+1);
scanf("%f", &marks[i]);
sum += marks[i];
}
average = sum / 8;
printf("Average = %.2f and highest = %.2f\n", average,highest(&marks));
exit(1);
}
else{
child2 = fork();
if (child2 == 0){
char *cmd = "wc";
char *args[4];
args[0] = "wc";
args[1] = "-c";
args[2] = "test.txt";
args[3] = NULL;
execvp(cmd, args);
}
else
{
child3 = fork();
if (child3 == 0){
char *cmd = "wc";
char *args[4];
args[0] = "wc";
args[1] = -c;
args[2] = "anotherfile.txt";
args[3] = NULL;
execvp(cmd, args);
}
else
{
wait(&child1Status);
printf(" child one has exited with exit status %d \n", (child1Status >> 8));
wait(&child2Status);
printf(" child two has exited with exit status %d \n", (child2Status >> 8));
wait(&child3Status);
printf(" child three has exited with exit status %d \n", (child3Status >> 8));
}
}
在我当前的输出中 child 1 表示它在我输入任何标记之前已经退出,而它应该说它在打印出最高和平均标记后已经退出。
我也知道,因为我在 child2 和 3 中使用 execvp,所以 exit() 代码不会 运行,在那种情况下我如何获得退出状态?
我还需要在所有 child 进程终止后打印“parent 已完成”,我如何确定所有 child 进程在我之前已终止打印“parent 完成了”
编辑:根据评论将最后一个 else 块替换为以下内容
else
{
waitpid( child1, &child1Status, 0);
printf(" child one has exited with exit status %d \n", (child1Status >> 8));
waitpid( child2, &child2Status, 0);
printf(" child two has exited with exit status %d \n", (child2Status & 0x7F));
waitpid( child3, &child3Status, 0);
printf(" child three has exited with exit status %d \n", (child3Status & 0x80));
}
以上所有 3 个 child 进程在最后一个接一个地退出,这不应该是这种情况
预期输出:
I am child 1 please enter 8 marks and i will find the average
I am child 2 here is the word count
50
child 2 has exited
i am child 3 here is the word count
96
child 3 has exited
**enter 8 marks by user**
average is
child 1 has exited
parent has finished
当前输出:
I am child 1 please enter 8 marks and i will find the average
I am child 2 here is the word count
50
i am child 3 here is the word count
96
**enter 8 marks by user**
average is
child 1 has exited
child 2 has exited
child 3 has exited
由于各种错误,您的代码甚至无法编译...
无论如何,看起来您想在 child 结束时立即显示。为此,你应该循环 wait
直到没有更多的 child 等待,并使用 return 值来知道哪一个已经结束:
...
else
{
for(;;) {
pid_t pid = wait(&child1Status);
int child_num;
if (pid == child1) child_num = 1;
else if (pid == child2) child_num = 2;
else if (pid == child3) child_num = 3;
else break; // no more child to wait...
printf(" child %d has exited with exit status %d \n",
child_num, (child1Status >> 8));
}
}
...
在学习分叉和进程方面仍然是新手,我的任务是创建 3 个 child 进程执行一些操作,然后 parent 应该在它们终止时打印退出状态。
我遇到的问题是 child 1 提前终止,我认为我没有正确使用 wait()。
这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
pid_t child1,child2, child3,wpid;
int child1Status,child2Status,child3Status;
child1 = fork();
if (child1 == 0){
float marks[8];
float average = 0.0;
float sum = 0.0;
printf("I am child one my pid is %d \n",getpid());
printf("Please enter 8 marks and I will calcuate the average and highest mark\n");
for (int i = 0; i < 8; ++i) {
printf("%i) ",i+1);
scanf("%f", &marks[i]);
sum += marks[i];
}
average = sum / 8;
printf("Average = %.2f and highest = %.2f\n", average,highest(&marks));
exit(1);
}
else{
child2 = fork();
if (child2 == 0){
char *cmd = "wc";
char *args[4];
args[0] = "wc";
args[1] = "-c";
args[2] = "test.txt";
args[3] = NULL;
execvp(cmd, args);
}
else
{
child3 = fork();
if (child3 == 0){
char *cmd = "wc";
char *args[4];
args[0] = "wc";
args[1] = -c;
args[2] = "anotherfile.txt";
args[3] = NULL;
execvp(cmd, args);
}
else
{
wait(&child1Status);
printf(" child one has exited with exit status %d \n", (child1Status >> 8));
wait(&child2Status);
printf(" child two has exited with exit status %d \n", (child2Status >> 8));
wait(&child3Status);
printf(" child three has exited with exit status %d \n", (child3Status >> 8));
}
}
在我当前的输出中 child 1 表示它在我输入任何标记之前已经退出,而它应该说它在打印出最高和平均标记后已经退出。
我也知道,因为我在 child2 和 3 中使用 execvp,所以 exit() 代码不会 运行,在那种情况下我如何获得退出状态?
我还需要在所有 child 进程终止后打印“parent 已完成”,我如何确定所有 child 进程在我之前已终止打印“parent 完成了”
编辑:根据评论将最后一个 else 块替换为以下内容
else
{
waitpid( child1, &child1Status, 0);
printf(" child one has exited with exit status %d \n", (child1Status >> 8));
waitpid( child2, &child2Status, 0);
printf(" child two has exited with exit status %d \n", (child2Status & 0x7F));
waitpid( child3, &child3Status, 0);
printf(" child three has exited with exit status %d \n", (child3Status & 0x80));
}
以上所有 3 个 child 进程在最后一个接一个地退出,这不应该是这种情况
预期输出:
I am child 1 please enter 8 marks and i will find the average
I am child 2 here is the word count
50
child 2 has exited
i am child 3 here is the word count
96
child 3 has exited
**enter 8 marks by user**
average is
child 1 has exited
parent has finished
当前输出:
I am child 1 please enter 8 marks and i will find the average
I am child 2 here is the word count
50
i am child 3 here is the word count
96
**enter 8 marks by user**
average is
child 1 has exited
child 2 has exited
child 3 has exited
由于各种错误,您的代码甚至无法编译...
无论如何,看起来您想在 child 结束时立即显示。为此,你应该循环 wait
直到没有更多的 child 等待,并使用 return 值来知道哪一个已经结束:
...
else
{
for(;;) {
pid_t pid = wait(&child1Status);
int child_num;
if (pid == child1) child_num = 1;
else if (pid == child2) child_num = 2;
else if (pid == child3) child_num = 3;
else break; // no more child to wait...
printf(" child %d has exited with exit status %d \n",
child_num, (child1Status >> 8));
}
}
...