写我自己的外壳...卡在管道上? [英] Writing my own shell... stuck on pipes?

查看:124
本文介绍了写我自己的外壳...卡在管道上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在试图编写自己的shell实现,但是我似乎已经被困在管道工作正常。我能够单独解析一个行和分离管道之间的命令(例如:ls | sort),但似乎不能让他们从一个管道输入到另一个。

For the past few days I have been attempting to write my own shell implementation but I seem to have gotten stuck on getting pipes to work properly. I am able to parse a line and fork off the commands between the pipes (ex: ls | sort) individually but can't seem to get them to pipe input from one into the other.

我想我只是不明白如何使用dup2()和管道正确。

I think I just don't understand how to use dup2() and pipes properly.

我现在已包含我的代码,但仍然失败... :(因此卡住...

I've now included my code which is still failing... :( So stuck...

void forkAndExecute( char* arrayOfWords[] , vector<pid_t> *vectorOfPIDs , bool hasNextCmd , bool hasPrevCmd) {

int fd[ 2 ];
pid_t pid;

if( hasNextCmd ){
	pipe(fd);
}

pid = fork();

//error if PID < 0
if( pid < 0 ) {
	cerr << ">>> fork failed >>>" << endl;
	exit(-1);
}
//child process if PID == 0
else if( pid == 0 ) {
	if ( hasPrevCmd ){
		dup2(fd[0] , 0);
		close(fd[0]);
		close(fd[1]);

	}
	if ( hasNextCmd ){
		dup2(fd[1],1);
		close(fd[0]);
		close(fd[1]);
	}
	execvp( arrayOfWords[0] , arrayOfWords );
	cout << ">>> command not found >>>" << endl;
	//if logic reaches here, exec failed
	exit(0);
} 
//parent process
else{
	close(fd[0]);
	close(fd[1]);
	//if( ! isLastCmd ){

	//}
	vectorOfPIDs->push_back(pid);
}

}

推荐答案

第一个建议:符号常量比魔术数字好。

First suggestion: Symbolic constants are better than magic numbers.

const int PIPE_READ = 0;
const int PIPE_WRITE = 1;
int fd[2];
pipe(fd);
// Now you can refer to fd[PIPE_READ] and fd[PIPE_WRITE].

第二个建议:退一步,想想你想要完成什么。

Second suggestion: Take a step back and think about what you're trying to accomplish.

您想生成两个进程,第一个进程的stdout连接到第二个进程的stdin。对吗?

You want to spawn two processes, with the first process's stdout connected to the second process's stdin. Right?

所以,在C中,这意味着你需要调用 pipe ,传递 fd [PIPE_WRITE] 到第一个子进程,将 dup2 更改为1,并传递 fd [PIPE_READ ] 到第二个子进程,将 dup2 更改为0.

So, in C, this means that you need to take call pipe, pass fd[PIPE_WRITE] to the first child process, which will dup2 it to 1, and pass fd[PIPE_READ] to the second child process, which will dup2 it to 0.

看看 forkAndExecute'的原型显示它不能这样做:

Simply looking at forkAndExecute's prototype shows that it can't do that:

void forkAndExecute( char* arrayOfWords[] , vector *vectorOfPIDs , 
    bool hasNextCmd , bool hasPrevCmd);

它只处理单个命令,并且从查看该参数列表,除非它使用邪恶的全局变量,没有办法从它的PrevCmd接收文件描述符,或从它的NextCmd接收文件描述符。

It only handles a single command, and from looking at that argument list, unless it resorts to evil global variables, there's no way for it to receive a file descriptor from its PrevCmd or receive a file descriptor from its NextCmd.

想想如何管理你需要的文件描述符,并重新设计 forkAndExecute 以便能够使用这些。

Think about how to manage the file descriptors that you need, and redesign forkAndExecute to be able to use these.

这篇关于写我自己的外壳...卡在管道上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆