我正在尝试创建一个调用sort的子节点。父级通过管道将数据发送给子级。我的代码编译并运行,但没有输出。我究竟做错了什么?我没有正确关闭管道,写管道还是正确输出数据?
[eddit]在我的系统上,我需要调用/ bin / sort NOT / usr / bin / sort!
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
int main(void){
int pipes[2];
pid_t pid;
FILE *stream;
if(pipe(pipes) == -1)
printf("could not create pipe\n");
switch(fork()){
case -1:
fprintf(stderr, "error forking\n");
break;
case 0:
dup2(pipes[0], STDIN_FILENO);
pid = getpid();
printf("in child, pid=%d\n");
if(close(pipes[1]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
execl("/usr/bin/sort", "sort", (char*) NULL);
break;
default:
stream = fdopen(pipes[1], "w");
pid = getpid();
printf("in parent, pid=%d\n", pid);
if (stream == NULL)
fprintf(stderr, "could not create file streami\n");
if(close(pipes[0]) == -1)
printf("err closing read end pid=%d\n");
fputs("bob\n",stream);
fputs("cat\n",stream);
fputs("ace\n",stream);
fputs("dog\n",stream);
if(fclose(stream) == EOF)
fprintf(stderr, "error while closing stream\n");
break;
}
return 0;
}
[edit]这是我的工作代码。谢谢大家
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void){
int pipes[2];
pid_t pid;
FILE *stream;
int stat;
if(pipe(pipes) == -1)
printf("could not create pipe\n");
switch(fork()){
case -1:
fprintf(stderr, "error forking\n");
break;
case 0:
dup2(pipes[0], STDIN_FILENO);
pid = getpid();
printf("in child, pid=%d\n", pid);
if(close(pipes[1]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
if(close(pipes[0]) == -1)
fprintf(stderr,"err closing write end pid=%d\n", pid);
execl("/bin/sort", "sort", (char*) NULL);
exit(EXIT_FAILURE);
break;
default:
stream = fdopen(pipes[1], "w");
pid = getpid();
printf("in parent, pid=%d\n", pid);
if (stream == NULL)
fprintf(stderr, "could not create file streami\n");
if(close(pipes[0]) == -1)
printf("err closing read end pid=%d\n");
fputs("bob\n",stream);
fputs("cat\n",stream);
fputs("ace\n",stream);
fputs("dog\n",stream);
if(fclose(stream) == EOF)
fprintf(stderr, "error while closing stream\n");
break;
}
wait(&stat);
return 0;
}
答案 0 :(得分:7)
您肯定在代码中没有足够的close()
次调用,这会锁定进程。
伪代码:
Create pipe
Fork
In parent:
Close read end of pipe
Write data to be sorted down write end of pipe
Close write end of pipe
Wait for child to die
In child
Close write end of pipe
Duplicate read end of pipe to stdin
Close read end of pipe
Exec the sort program
Exit with an error if the exec returns
请注意,伪代码最终会关闭管道的所有四个端点 - 父级中的两个端口和子级中的两个端口。如果你不这样做,你将陷入僵局。
答案 1 :(得分:2)
您唯一缺少的是在父代码的末尾调用wait()
或waitpid()
,以便在子代完成之前不会退出。
答案 2 :(得分:0)
sort命令没有参数。简单地运行execl是行不通的。 一个简单的测试程序是:
int main(void){
execl("/bin/sort","/bin/sort","filename", (char*) NULL);
}
我将尝试为您创建一个简单的程序来分析情况。
在这里,试试这段代码:
int main(void){
int pipefd[2];
pid_t pid = 0;
int status;
char data[100]={0};
int fildes[2] ;
int nbytes;
char buf[100]={0};
status = pipe(fildes);
if (status == -1 ) {
// handle eerrror.
}
switch (fork()) {
case -1: /* Handle error */
break;
case 0: /* Child - reads from pipe */
close(fildes[1]); /* Write end is unused */
nbytes = read(fildes[0], buf, 100); /* Get data from pipe */
fprintf(stderr,"Inside child val recieved is %s\n", buf);
/* At this point, a further read would see end of file ... */
execl("/bin/sort", "/bin/sort",buf, (char*) NULL);
close(fildes[0]); /* Finished with pipe */
exit(0);
default: /* Parent - writes to pipe */
close(fildes[0]); /* Read end is unused */
write(fildes[1], "file", strlen("file")); /* Write data on pipe */
close(fildes[1]); /* Child will see EOF */
exit(0);
}
}
此处“文件”是需要排序的文件。
希望您可以根据需要自定义它。
享受.. !!!