C语言 使用管道将输入写入cat命令



我对使用管道和分叉是新手。我想做的是创建一个程序来执行"cat"函数,这样我就可以通过我的程序向cat发送输入,并以文本文件的形式接收输出。

我有两个问题:

使用execvp函数,是否有办法运行"cat"而不是被迫与提示交互,而是通过C?

另一个问题是捕获cat的输入并将其写入文本文件。

例如,如果我写的是

send_cat("hi");
send_cat("hello");

那么在文本文件中它会读为

hi
hello

您所描述的问题的解决方案涉及使用pipe()系统调用和dup2系统调用。

基本上,您将在父进程和子进程之间设置pipe(),然后您的解决方案应该使用dup2将运行cat的子进程的标准重定向到调用execvp的进程的标准。您的解决方案应该对标准输出执行类似的操作:使用dup2execvp子进程的标准输出重定向到程序的标准输出。

编辑:在上面的解释中有一些修饰,你发现我非常慷慨,所以这样的程序结构可能看起来像这样:

编辑2:我第一次尝试用cat而不是echo来编写这个示例程序,但后来我意识到你需要从cat进程中以某种方式向cat进程发送EOF信号,而发送''是无效的。

int pipefd[2];
int result = pipe(pipefd);
if (result < 0) {
// pipe error
perror("pipe failure");
exit(1);
}
// Redirect the program's stdout and stdin to go to and from the pipe, respectively.
// This means that "echo"'s output will go to the pipe, and when "echo" finishes and we return execution to the parent process, we'll be able to read the information that "echo" just output from that pipe
// This is necessary in order to restore stdin and stdout to what they were prior to running this program
int savedStdin = dup(0);
int savedStdout = dup(1);
// Redirect stdin to come from the pipe
if ( dup2(pipefd[0], 0) < 0 ) {
perror("dup2 error");
exit(1);
}
// Close the read end of the pipe because the original descriptor was dupliechoed
close(pipefd[0]);
// Redirect stdout to go to the pipe
if ( dup2(pipefd[1], 1) < 0 ) {
perror("dup2 error");
exit(1);
}
// Close the write end of the pipe because the original descriptor was dupliechoed
close(pipefd[1]);
if ( fork() == 0 ) {

// Child process, will call "echo" and die
execlp("echo", "echo", "Hello_world!", NULL);
// The program should never ever get to this point, ever
// but if it does, we need to handle it
exit(1);
} else {
// Parent process, we need to wait for "echo" to terminate
wait(NULL);
// At this point stdout and stdin are still coming to/from the pipe, so if we do something like cin >> s, that will read from the pipe
// First, let's restore stdout to what it was before we redirected it, so that we can print the output of "echo" to the terminal
if (dup2(savedStdout, 1) < 0 ) {
perror("dup2 error");
exit(1);
}
close(savedStdout);
string s;
// Now we're going to read from stdin (the pipe) and print to stdout (the terminal, if you're running this from the command-line)
while (cin >> s) printf("%sn", s.c_str() );
// We've read everything from "echo", let's fix stdin now
if (dup2(savedStdin, 0) < 0 ) {
perror("dup2 error");
exit(1);
}
close(savedStdin);
}

最新更新