【发布时间】:2016-02-05 23:04:13
【问题描述】:
教育任务:想模仿管道符号(命令、方法)“|”工作。程序从 STDIN 获取类似 unix shell 的命令:
command1 | command2 | command3 | ....
并且应该执行它将 STDIN|STDOUT 重定向到每个命令的管道。最终输出重定向到 result.out 文件。应该只使用 execlp 和 fork。
第一个变体:适用于 1-2 个命令,但冻结 3 个或更多。我做错了什么:似乎我关闭了所有管道描述符?
现在在第二个变体中,execute_line 被简化了,现在另一个问题是:输出混乱。如何在命令之间正确传递管道?
第三个变体:最接近正确,添加了更多调试信息。问题:如何正确连接中间孩子?
第 4 个变体,固定逻辑,几乎正确:在 1、3 或更多命令下工作正常,在 2 开始时失败(之前工作正常) - 很奇怪 :)
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
void split(const string& str, vector<string> &tokens,
const string &delimiters = " ")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
inline string trim(string &str)
{
const string whitespaces(" \t\f\v\n\r");
string::size_type pos = str.find_first_not_of(whitespaces);
if(pos != string::npos)
str.erase(0, pos); // prefixing spaces
pos = str.find_last_not_of(whitespaces);
if(pos != string::npos)
str.erase(pos + 1); // surfixing spaces
return str;
}
void parse_command(string &command, string &name, string &argc)
{
command = trim(command);
string::size_type pos = command.find_first_of(' ');
if(pos != string::npos) {
name = command.substr(0, pos);
argc = command.substr(pos + 1, command.length() - pos - 1);
} else {
name = command;
argc = "";
}
}
void exec_command(uint n, vector<string> &commands)
{
string name, args;
parse_command(commands[n], name, args);
if(args.length() > 0)
execlp(name.c_str(), name.c_str(), args.c_str(), NULL);
else
execlp(name.c_str(), name.c_str(), NULL);
}
// who ----(stdout)---> pfd[1] --- pfd[0] ----(stdin)---> wc -l
void execute_line(vector<string> &commands, uint i, int *parent_pfd = 0)
{
int pfd[2];
pipe(pfd);
if(i > 0 && !fork()) {
// Child
printf("Child, i: %d\n", i);
if(i > 1) {
execute_line(commands, i-1, pfd);
close(pfd[1]);
close(pfd[0]);
} else {
printf("Deeper child %d: %s, parent_pfd[0]=%d, parent_pfd[1]=%d, "
"pfd[0]=%d, pfd[1]=%d\n",
getpid(), trim(commands[i-1]).c_str(),
parent_pfd[0], parent_pfd[1], pfd[0], pfd[1]);
close(STDOUT_FILENO);
// if(parent_pfd)
// dup2(parent_pfd[1], STDOUT_FILENO); // Copy STDOUT to parent pipe out
// else
dup2(pfd[1], STDOUT_FILENO); // Copy STDOUT to pipe out
close(pfd[1]);
close(pfd[0]);
exec_command(i - 1, commands);
}
} else {
if(parent_pfd) {
printf("Middle Child, i: %d\n", i);
printf("Middle child %d: %s, parent_pfd[0]=%d, parent_pfd[1]=%d, "
"pfd[0]=%d, pfd[1]=%d\n",
getpid(), trim(commands[i]).c_str(), parent_pfd[0], parent_pfd[1],
pfd[0], pfd[1]);
close(STDIN_FILENO);
dup2(pfd[0], STDIN_FILENO); // Copy STDIN to pipe in
close(STDOUT_FILENO);
dup2(parent_pfd[1], STDOUT_FILENO); // Copy STDOUT to parent pipe out
close(pfd[1]);
close(pfd[0]);
exec_command(i, commands);
} else {
printf("Final, i: %d\n", i);
printf("Final %d: %s, pfd=%p, parent_pfd=%p, pfd[0]=%d, pfd[1]=file\n",
getpid(), trim(commands[i]).c_str(), pfd, parent_pfd, pfd[0]);
int fd = open("result.out", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
dup2(fd, STDOUT_FILENO); // Copy stdout to file
dup2(pfd[0], STDIN_FILENO); // Copy STDIN to pipe in
close(pfd[0]); // Close as was redirected
close(pfd[1]); // Close WRITE as not necessary here
close(fd);
exec_command(i, commands);
}
}
}
int main()
{
char buffer[1024];
ssize_t size = read(STDIN_FILENO, buffer, 1024);
if(size > 0) {
buffer[size] = '\0';
string command = buffer;
vector<string> commands;
split(command, commands, "|");
execute_line(commands, commands.size() - 1);
}
return 0;
}
【问题讨论】:
标签: c++ unix redirect pipe stdout