加工和管道
Processing and Pipes
本文关键字:管道 更新时间:2023-10-16
前提:实现一个c++程序,提示用户输入2个命令。每个输入字符串应该是一个UNIX命令,允许带参数。例如,输入1可以是"ls -l",输入2可以是"wc -l"。然后,程序将创建一个管道和两个子进程。第一个子进程将运行第一个输入中指定的命令。它将输出到管道而不是标准输出。第二个子进程将运行第二个输入中指定的命令。它将从管道中获取输入,而不是标准输入。父进程将等待它的两个子进程完成,然后整个过程将重复。当"quit"作为第一个命令时,执行将停止。
我相信我几乎完成了这个程序,但是我在弄清楚如何让用户输入在管道中执行时遇到了麻烦,并且我最终出现了语法错误。
下面是我的代码:#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 first, second[80];
int rs, pipefd[2];
int pid1, pid2;
char *command1[80], *command2[80];
//Asking for the commands
cout << "Please enter your first command(incl. args) or quit: ";
cin >> first;
//Do the program while the first answer isn't quit
while(first[0] != 'quit') {
//Copy first answer into first command
strcpy(command1, ((char*)first);
//Just skip to end of program if first command is quit
cout << "Please enter your second command(incl. args): ";
cin >> second;
//Copy second answer into second command
strcpy(command2, ((char*)first);
//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
execvp(command1[0], command1);
}
if (pid1 == 0) { // 2st child process
// close write end of pipe
close(pipefd[0]);
// duplicate
dup2(pipefd[1], 1);
//execute the input argument
execvp(command2[0], command2);
}
else { // parent process
// close read end of pipe
close(pipefd[0]);
// wait for child processes
wait(&pid1);
wait(&pid2);
}
//Asking for the commands
cout << "Please enter your first command(incl. args) or quit: ";
cin >> first;
}; //end of while()
return 0;
}
任何帮助/提示将不胜感激,因为这是由于在不久的将来,我希望最终有这个野兽解决。
Edit: 添加得到的错误
In function int main()':
z1674058.cxx:26:33: error: expected ')' before ';' token
z1674058.cxx:33:33: error: expected ')' before ';' token
// close write end of pipe
close(pipefd[0]);
// duplicate
dup2(pipefd[1], 1);
在子#1和子#2中都有相同的两行。这两个子函数应该做相反的运算。其中一个应该是关闭pipefd[1]
并将pipefd[0]
转储到stdin。
你也有一些字符串处理错误。
char first, second[80];
没有声明两个80个字符的数组。它声明了一个char
和一个char[80]
。我怀疑这就是为什么您在这里放入(char *)
强制转换以沉默编译器错误:
strcpy(command1, ((char*)first);
不要那样做。不要使用强制类型转换来关闭编译器。cast正在掩盖一个严重的错误。
char *command1[80], *command2[80];
这些声明也是不正确的。它声明了两个数组,每个数组有80个char *
。也就是说,两个数组各包含80个字符串。我把这个问题留给你们自己解决。
相关文章:
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- IPC使用多个管道和分支进程来运行Python程序
- 如何创建函数管道,以便函数一个接一个地运行?
- Gstreamer 管道从命令 lne 到 c 代码
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 将旧管道转换为现代 openGL 时出现问题
- 如何使用管道在父级和子级之间来回传递文件
- 在没有管理员权限的情况下连接到同一网络中的命名管道
- 如何测量管道延迟?
- 我如何使用此程序管道多个命令?C++
- 先进先出:一个进程永远不会从管道读取
- Node.js fs.open() 在尝试打开 4 个以上的命名管道 (FIFO) 后挂起
- 使用模板而不是虚拟方法的管道模式
- 我可以写入关闭的套接字并强制纠正损坏的管道错误吗?
- 在 Azure DevOps 构建管道中使用英特尔C++编译器为 Linux 环境构建C++代码
- Opencv GStreamer管道在Raspberry Pi 4上不起作用
- 阻塞管道连接命名管道不触发
- GLib-ERROR:为GWakeup创建管道:打开的文件太多
- 向 vulkan 管道添加额外的 UBO 会停止所有几何体渲染
- 在命名管道中发送标准::字符串流