當我意識到我的程序沒有退出時,我在系統編程中練習管道。我在孩子和家長中都加了exit()
,但孩子仍然沒有退出。請幫助... 下面是代碼:即使在調用exit()函數後,c中的進程也不會退出
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
//#include "apue.h"
main() {
int n,max=20;
pid_t pid;
int fd[2];
char line[max];
int i;
for(i=0;i<20;i++) {
line[i]='\0';
}
if(pipe(fd)<0) {
perror("pipe error");
}
if((pid=fork())<0) {
perror("fork error");
}
else if(pid > 0) {
close(fd[0]);
write(fd[1], "hello world\n", 12);
exit(1);
} else {
close(fd[1]);
read(fd[0], line, max);
}
puts(line);
exit(1);
}
你怎麼知道它不會退出?你得到的行爲是什麼?你期望的是什麼? – 2012-11-02 20:05:10
出口(0)是更好的... – 0x90
好吧,出口(EXIT_FAILURE)是最好的,如果這是... – effeffe