fork() 的问题

发布于 2024-09-05 20:43:23 字数 1062 浏览 5 评论 0原文

我正在编写一个分叉的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别把无礼当个性 2024-09-12 20:43:24

您的核心有点混乱,包含所有嵌套的 ifelse。还有一些悬空的 else 语句 (else exit(0);)。我首先要清理它们。我看不出您的代码有任何其他逻辑问题。这很简单。

Your core is a tad messy with all the nested if's and else's. There are some dangling else 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.

独享拥抱 2024-09-12 20:43:24

交换线路

else exit(0);

else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }

Swap the lines

else exit(0);

and

else { if (parse(input) != 0 && firstrun != 1) { printf("Error parsing"); exit(-1); } exit(0); }
鸠魁 2024-09-12 20:43:24

除了其他人所说的其他问题一团糟之外,还有一些其他问题在您解决后会遇到。

在子进程中,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?

绿光 2024-09-12 20:43:23

罪魁祸首是 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.

柏拉图鍀咏恒 2024-09-12 20:43:23

`if (p >= 0) {

if (p == 0) {/* chile process */}

else if (p > 0) {/* parent process */}

} else {

/* handle the error returned by fork() */

}`

我会像上面的伪代码那样做。

else exit(0); 是子进程在代码中执行的操作。

`if (p >= 0) {

if (p == 0) {/* chile process */}

else if (p > 0) {/* parent process */}

} else {

/* handle the error returned by fork() */

}`

I'd do it like the above pseudo code.

else exit(0); is what the child process is doing in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文