我刚刚写了以下代码:-
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Create a pipe
int fd[2];
pipe(fd);
int i;
close(0); //close the input to this process
dup(fd[0]); // duplicate the input of this pipe to 0, so that stdin now refers to the input of the pipe
char *test[3] = {"A", "B", "C"};
for ( i=0 ; i < 3; ++i ) {
write(fd[0], test[i], strlen(test[i]));
write(fd[0], '\n', 1);
}
execlp("sort", "sort", NULL);
return 0;
}
我期望sort
从管道的输出中获取输入fd[1]
并将排序的输出打印到标准输出。