分叉三个子进程会产生奇怪的随机输出
我尝试让三个进程通过管道相互连接。然而,我对分叉第三个进程感到非常困惑。分叉和管道只有两个进程可以正常工作。当我添加 +1 循环来测试第三个进程是否生成时,我在终端中得到奇怪的结果。
这是我的代码(带有奇怪的结果):
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(){
int status, i;
int pip[2];
/* Spawn 3 subprocesses and pipe the first 2*/
for (i=0; i<3; i++){
if (i==0) pipe(pip);
if (fork()==0){
/* First subprocess */
if (i==0){
dup2(pip[1], 1); //pip[0] will replace stdout
close(pip[0]);
if (execlp("ls", "ls", NULL)) perror("process1");
}
/* Second subprocess */
if (i==1){
dup2(pip[0], 0); //pip[1] -> will replace stdin
close(pip[1]);
if (execlp("more", "more", NULL)) perror("process2");
}
/* Third subprocess */
if (i==2){
close(pip[0]); //reseting fd
close(pip[1]); //reseting fd
open(0); //reseting fd
open(1); //reseting fd
if (execlp("ls", "ls", NULL)) perror("process3");
}
}
}
wait(&status);
return 0;
}
将 for 循环更改为 2 个循环而不是 3 个循环可以停止奇怪的行为。 奇怪的行为是,我会随机在终端中得到这些输出之一:
manos@megistanas:~/Desktop/test$ ./test
test test2.c test3.c test4.c test.5c test.c
test
test2.c
test3.c
test4.c
test.5c
test.c
manos@megistanas:~/Desktop/test$
在这种情况下,它会正常工作。现在在某些时候它是这样的:
manos@megistanas:~/Desktop/test$ test
test2.c
test3.c
test4.c
test.5c
test.c
test test2.c test3.c test4.c test.5c test.c
manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$
按下 Enter 键只会写入提示并等待更多输入。第三个奇怪的行为是:
manos@megistanas:~/Desktop/test$ test
test2.c
test3.c
test4.c
test.5c
test.c
(blinking prompt symbol)
一旦我按下回车键,程序就会正常结束。有人可以解释发生了什么事吗?
I try to make three processes to pipe to each other. However I am really puzzled with forking the third process. Forking and piping only two processes works without problem. When I add +1 loop to test if the third process spawns, I get weird results in terminal.
This is my code(with the weird results):
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(){
int status, i;
int pip[2];
/* Spawn 3 subprocesses and pipe the first 2*/
for (i=0; i<3; i++){
if (i==0) pipe(pip);
if (fork()==0){
/* First subprocess */
if (i==0){
dup2(pip[1], 1); //pip[0] will replace stdout
close(pip[0]);
if (execlp("ls", "ls", NULL)) perror("process1");
}
/* Second subprocess */
if (i==1){
dup2(pip[0], 0); //pip[1] -> will replace stdin
close(pip[1]);
if (execlp("more", "more", NULL)) perror("process2");
}
/* Third subprocess */
if (i==2){
close(pip[0]); //reseting fd
close(pip[1]); //reseting fd
open(0); //reseting fd
open(1); //reseting fd
if (execlp("ls", "ls", NULL)) perror("process3");
}
}
}
wait(&status);
return 0;
}
Changing the the for-loop to 2 loops instead of 3 stops the weird behaviour.
The weird behaviour is that randomly I will get one of these outputs in terminal:
manos@megistanas:~/Desktop/test$ ./test
test test2.c test3.c test4.c test.5c test.c
test
test2.c
test3.c
test4.c
test.5c
test.c
manos@megistanas:~/Desktop/test$
In that case it worked as normally. Now at some points it goes like this:
manos@megistanas:~/Desktop/test$ test
test2.c
test3.c
test4.c
test.5c
test.c
test test2.c test3.c test4.c test.5c test.c
manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$ manos@megistanas:~/Desktop/test$
Where hitting enter just writes the prompt and waits for more input. The third weird behaviour is this:
manos@megistanas:~/Desktop/test$ test
test2.c
test3.c
test4.c
test.5c
test.c
(blinking prompt symbol)
Once I hit enter the program ends normally. Can someone explain what is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请阅读 open(2) 的手册页。
提示:它不需要一个参数。您可能应该使用
-Wall
进行构建,并注意编译器警告。这可能并不能完全解释您所看到的内容,但考虑到这个明显的错误,我懒得进一步查看。
Please read the man page for open(2).
Hint: it doesn't take one parameter. You should probably build with
-Wall
, and pay attention to compiler warnings.This probably doesn't completely explain what you are seeing, but given this obvious bug, I am too lazy to look any further.