bash解释器linux的读/写stdin/out,fork-execl
我一直在尝试编写一个程序,向bashshell(/bin/sh)发送和接收命令。就像围绕bash shell的包装程序。因此,我可以写入stdin“cd~/Desktop”,然后再次写入“ls”,我将收到桌面上的文件列表。但是我不能让它工作。在这段代码的第二个write命令中,它将回显我写给stdin的任何内容。我也尝试过使用popen(),但它只提供输出,不允许我写入stdin。有人能帮我解决这个问题吗?谢谢bash解释器linux的读/写stdin/out,fork-execl,c,linux,bash,execl,C,Linux,Bash,Execl,我一直在尝试编写一个程序,向bashshell(/bin/sh)发送和接收命令。就像围绕bash shell的包装程序。因此,我可以写入stdin“cd~/Desktop”,然后再次写入“ls”,我将收到桌面上的文件列表。但是我不能让它工作。在这段代码的第二个write命令中,它将回显我写给stdin的任何内容。我也尝试过使用popen(),但它只提供输出,不允许我写入stdin。有人能帮我解决这个问题吗?谢谢 void main() { // Create a pipe and for
void main()
{
// Create a pipe and fork
//
int fd[2];
int p = pipe(fd);
pid_t pid = fork();
if (pid > 0)
{
// Read from the pipe and output the result
//
//close(fd[1]);
char buf[1024] = { 0 };
read(fd[0], buf, sizeof(buf));
printf("1 - %s\n", buf);
write (fd[1], "ifconfig", strlen ("ifconfig") );
// problem is here, read is returning echo'd bytes from write()
read(fd[0], buf, sizeof(buf));
printf("2 - %s\n", buf);
// Wait for child to terminate
int status;
wait(&status);
}
else if (pid == 0)
{
// Redirect stdout and stderr to the pipe and execute the shell
// command
//
dup2(fd[0], STDIN_FILENO);
dup2(fd[1], STDOUT_FILENO);
dup2(fd[1], STDERR_FILENO);
//close(fd[0]);
execl("/bin/sh", "exec sh", "-c", "ls", (char*) NULL );
}
}
编辑-根据第一个答案更新代码,现在第二个read()调用没有输出
}请记住,管道是单向通信流。不能将其用于两个进程之间的双向通信。为此,您需要两个管道,每个方向一个 也许像这个简单的例子:
// Pipe for the child process to write to the parent process
int child_to_parent[2];
// Pipe for the parent process to write to the child process
int parent_to_child[2];
// Create the TWO pipes
pipe(child_to_parent);
pipe(parent_to_child);
pid_t pid = fork();
if (pid > 0)
{
// In parent process
// Read the output of the child from child_to_parent[0]
// We don't need child_to_parent[1] so close it
close(child_to_parent[1]);
// Write output to the child using parent_to_child[1]
// We don't need parent_to_child[0] so close it
close(parent_to_child[0]);
// Read from and write to the child process...
}
else if (pid == 0)
{
// In child process
// child_to_parent[1] is were we write output, it's the
// new standard output, child_to_parent[0] can be closed
dup2(child_to_parent[1], STDOUT_FILENO);
close(child_to_parent[0]);
// parent_to_child[0] is where we read input from, it's the
// new standard input, parent_to_child[1] can be closed
dup2(parent_to_child[0], STDIN_FILENO);
close(parent_to_child[1]);
// Do whatever the child is supposed to do
}
请记住,管道是单向通信流。不能将其用于两个进程之间的双向通信。为此,您需要两个管道,每个方向一个 也许像这个简单的例子:
// Pipe for the child process to write to the parent process
int child_to_parent[2];
// Pipe for the parent process to write to the child process
int parent_to_child[2];
// Create the TWO pipes
pipe(child_to_parent);
pipe(parent_to_child);
pid_t pid = fork();
if (pid > 0)
{
// In parent process
// Read the output of the child from child_to_parent[0]
// We don't need child_to_parent[1] so close it
close(child_to_parent[1]);
// Write output to the child using parent_to_child[1]
// We don't need parent_to_child[0] so close it
close(parent_to_child[0]);
// Read from and write to the child process...
}
else if (pid == 0)
{
// In child process
// child_to_parent[1] is were we write output, it's the
// new standard output, child_to_parent[0] can be closed
dup2(child_to_parent[1], STDOUT_FILENO);
close(child_to_parent[0]);
// parent_to_child[0] is where we read input from, it's the
// new standard input, parent_to_child[1] can be closed
dup2(parent_to_child[0], STDIN_FILENO);
close(parent_to_child[1]);
// Do whatever the child is supposed to do
}
您需要两个管道:一个用于写入父进程,另一个用于读取子进程,另一个用于相反方向。这不是“dup2(fd[0],STDIN_FILENO);dup2(fd[1],STDOUT_FILENO);”的含义吗?你能解释一下你在代码中说的是什么吗?你需要两个管道:一个用于父进程写入,一个用于子进程读取,另一个用于相反方向。这不是“dup2(fd[0],STDIN_FILENO);dup2(fd[1],STDOUT_FILENO);”的意思吗?你能解释一下你在代码中说的是什么吗?嗯,我想试试这个,但你所做的只是从我的原始代码中添加对close()的调用。很抱歉,这仍然有点不清楚,特别是因为它无法编译,但我会尝试一下……您能否指定我应该从何处读取()和写入()?@shelly3783与您的代码相比,计算我的示例中的管道数。。。你看过评论了吗?注意到管道的两个不同变量了吗?是的,通过编辑,我现在可以看到差异,谢谢。我正在尝试。仍然不工作,我将更新原始代码。嗯,我将尝试此操作,但您所做的只是从我的原始代码中添加对close()的调用。很抱歉,这仍然有点不清楚,特别是因为它无法编译,但我会尝试一下……您能否指定我应该从何处读取()和写入()?@shelly3783与您的代码相比,计算我的示例中的管道数。。。你看过评论了吗?注意到管道的两个不同变量了吗?是的,通过编辑,我现在可以看到差异,谢谢,我正在尝试。仍然不工作,我将更新原始代码