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.
You want to spawn two processes, with the first process's stdout connected to the second process's stdin. Right?
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.
Simply looking at forkAndExecute'
s prototype shows that it can't do that:
void forkAndExecute( char* arrayOfWords[] , vector *vectorOfPIDs ,
bool hasNextCmd , bool hasPrevCmd);
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.
Think about how to manage the file descriptors that you need, and redesign forkAndExecute
to be able to use these.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…