fork() 的问题
我正在编写一个分叉的 shell,父进程读取输入,子进程使用 execvp 解析并执行它。
main 方法的伪代码:
do{
pid = fork();
print pid;
if (p<0) { error; exit; }
if (p>0) { wait for child to finish; read input; }
else { call function to parse input; exit; }
}while condition
return;
发生的情况是我似乎从未进入子进程(打印的 pid 始终为正,我从不输入 else)。但是,如果我不调用解析函数而只是退出,我确实会正确地交替输入父级和子级。
full code:
int main(int argc, char *argv[]){
char input[500];
pid_t p;
int firstrun = 1;
do{
p = fork();
printf("PID: %d", p);
if (p < 0) {printf("Error forking"); exit(-1);}
if (p > 0){
wait(NULL);
firstrun = 0;
printf("\n> ");
bzero(input, 500);
fflush(stdout);
read(0, input, 499);
input[strlen(input)-1] = '\0';
}
else exit(0);
else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }
}while(strcmp(input, "exit") != 0);
return 0;
}
编辑:
-否则 exit(0) 只是我忘记的东西 -
在打印中添加换行符表明它实际上正确分叉并进入子进程;谢谢,问题似乎出在解析上
I'm writing a shell which forks, with the parent reading the input and the child process parsing and executing it with execvp.
pseudocode of main method:
do{
pid = fork();
print pid;
if (p<0) { error; exit; }
if (p>0) { wait for child to finish; read input; }
else { call function to parse input; exit; }
}while condition
return;
what happens is that i never seem to enter the child process (pid printed is always positive, i never enter the else). however, if i don't call the parse function and just have else exit, i do correctly enter parent and child alternatingly.
full code:
int main(int argc, char *argv[]){
char input[500];
pid_t p;
int firstrun = 1;
do{
p = fork();
printf("PID: %d", p);
if (p < 0) {printf("Error forking"); exit(-1);}
if (p > 0){
wait(NULL);
firstrun = 0;
printf("\n> ");
bzero(input, 500);
fflush(stdout);
read(0, input, 499);
input[strlen(input)-1] = '\0';
}
else exit(0);
else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }
}while(strcmp(input, "exit") != 0);
return 0;
}
EDIT:
-that else exit(0) just something i forgot there from playing around
-adding a newlines to the prints shows that it does in fact correctly fork and enter the child process; thank you, the problem seems to be in the parse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的核心有点混乱,包含所有嵌套的
if
和else
。还有一些悬空的else
语句 (else exit(0);
)。我首先要清理它们。我看不出您的代码有任何其他逻辑问题。这很简单。Your core is a tad messy with all the nested
if
's andelse
's. There are some danglingelse
statements as well (else exit(0);
). I'd start by cleaning those up. I can't see any other logical problems with your code. It's simple enough.交换线路
并
Swap the lines
and
除了其他人所说的其他问题一团糟之外,还有一些其他问题在您解决后会遇到。
在子进程中,
input
数组在第一次运行时将是垃圾,因为在分叉之前您没有在其中放入任何内容。分叉似乎完全没有意义,因为您没有在子进程中执行任何操作,而是在等待子进程在父进程中完成。为什么不直接从父级调用 parse 呢?
Apart from everything everybody else has said about the fact that the else's are a complete mess, there are some other issues you will hit when you have fixed them.
In the child, the
input
array will be garbage on the first run because you don't put anything in it before forking.It seems completely pointless to fork at all since you are not exec'ing anything in the child but you are waiting for the child to finish in the parent. Why not just call parse from the parent?
罪魁祸首是
else exit(0);
它将在子 shell 中执行,这意味着它永远不会进入解析阶段。该代码在语法上也是无效的,因为之后您还有另一个。
One culprit is
else exit(0);
That would execute in the child shell, which means it never gets to the parsing stage. The code is also syntactically invalid because after that you have another else.
`if (p >= 0) {
} else {
}`
我会像上面的伪代码那样做。
else exit(0);
是子进程在代码中执行的操作。`if (p >= 0) {
} else {
}`
I'd do it like the above pseudo code.
else exit(0);
is what the child process is doing in your code.