我的班级给了我这个任务,这个程序差不多完成了,但是输出结果与它应该是什么相反。
我在linux系统上运行它并使用geany ide。
这是该计划的主要要点:
实现一个C ++程序,提示用户输入2个命令。每个输入字符串应该是UNIX命令,允许使用参数。例如,输入1可以是“ls -l”,输入2可以是“wc -l”。然后程序将创建一个管道和两个子进程。第一个子进程将运行第一个输入中指定的命令。它将输出到管道而不是标准输出。第二个子进程将运行第二个输入中指定的命令。它将从管道输入而不是标准输入。父进程将等待其两个子进程完成,然后整个过程将重复。当“退出”作为第一个命令输入时,执行将停止。
到目前为止我的代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstring>
using namespace std;
int main() {
//Declare Variables
char firstAns[80], secondAns[80];
int rs, ansLen, pipefd[2];
int pid1, pid2;
char * command1[5], *command2[5];
char quit[] = "quit\0";
//Allocate memory for commands
for(int a=0; a<5; a++){
command1[a]=new char[80];
}
for(int b=0; b<5; b++){
command2[b]=new char[80];
}
//Asking for the commands
cout << "Please enter your first command(incl. args) or quit: ";
cin >> firstAns;
//Do the program while the first answer isn't quit
while(firstAns != quit) {
//Copy first answer into first command
ansLen = strlen(firstAns);
for(int i=0; i < ansLen; i++){
strcpy(command1[i], firstAns);
}
//Just skip to end of program if first command is quit
cout << "Please enter your second command(incl. args): ";
cin >> secondAns;
//Copy second answer into second command
ansLen = strlen(secondAns);
for(int i=0; i < ansLen; i++){
strcpy(command1[i], secondAns);
}
//pipe
rs = pipe(pipefd);
//if pipe fails to be made
if(rs == -1){
perror("Fail to create a pipe");
exit(EXIT_FAILURE);
}
//Fork for the two processes
pid1 = fork();
if (pid1 != 0){
pid2 = fork();
}
if(pid1 == -1){
perror("Fail to create a pipe");
exit(EXIT_FAILURE);
}
if(pid2 == -1){
perror("Fail to create a pipe");
exit(EXIT_FAILURE);
}
if (pid1 == 0) { // 1st child process
// close write end of pipe
close(pipefd[0]);
// duplicate
dup2(pipefd[1], 1);
//execute the input argument
rs = execvp(command1[0], command1);
//error check
if (rs == -1) {
perror("Fail to execute command");
exit(EXIT_FAILURE);
}
}
if (pid2 == 0) { // 2st child process
// close write end of pipe
close(pipefd[1]);
// duplicate
dup2(pipefd[0], 0);
//execute the input argument
rs = execvp(command2[0], command2);
//error check
if (rs == -1) {
perror("Fail to execute command");
exit(EXIT_FAILURE);
}
}
else {// parent process
// close read and write end of pipe
close(pipefd[0]);
close(pipefd[1]);
// wait for child processes
wait(&pid1);
wait(&pid2);
}
//Asking for the commands
cout << "Please enter your first command(incl. args) or quit: ";
cin >> firstAns;
}; //end of while()
return 0;
}
我没有收到任何错误,但我没有得到我想要的输出。以下是它应该如何工作的示例输出:
Enter command 1 (incl. args) or quit: ls
Enter command 2 (incl. args): wc
5 5 63
Enter command 1 (incl. args) or quit: ls -l
Enter command 2 (incl. args): wc -w
47
Enter command 1 (incl. args) or quit: hello
Enter command 2 (incl. args): more
hello: No such file or directory
Enter command 1 (incl. args) or quit: quit
相反,我的输出只产生perror,然后说第一个命令找不到文件或目录,第二个命令的错误地址如下:
Enter command 1 (incl. args) or quit: ls
Enter command 2 (incl. args): wc
Failed to execute command: No such file or directory
Failed to execute command: Bad address
我猜它是因为变量在execpv中不匹配,但是我可以用一点点推动来最终击败这个程序。
如果有人对此有任何建议,那么quit命令似乎也不起作用。任何形式的建议都很棒,可以帮助这位年轻的程序员:)