故障管道三个命令“dmesg|sort|more” c++
我已成功将一个命令的输出通过管道传输到另一个命令的输入中,然后将第二个命令的输出显示到屏幕上。
我想用三个连续的命令来做到这一点。 (实际上最终我想用在运行时传递到程序中的 N 个命令来完成此操作。
这是我尝试将三个命令一起管道化。
更新:我更新了我的问题以反映我最新的尝试。
#include <string.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
int main(int argc, char * argv[])
{
pid_t pid;
int pfd[2];
char* prgname = NULL;
if(pipe(pfd) == -1)
{
perror("error on pipe call");
return(1);
}
for(int j = 0;j<numberOfCommands;j++)
{
cout<<commands[j]<<"_"<<endl;
}
pid = fork();
if(pid == 0){//child process
close(pfd[0]); //close read end of pipe
dup2(pfd[1],1);//connect the pipes
close(pfd[1]);//close extra file descriptors
prgname = (char*)"dmesg"; //commands[0];//first command
execlp(prgname, prgname, 0);//Load the program
}
else
{
int pfd2[2];
if(pipe(pfd2) == -1)
{
perror("error on pipe call 2");
return(1);
}
pid = fork();
if(pid == 0)//child
{
close(pfd[1]);
dup2(pfd[0],0);
close(pfd[0]);
close(pfd2[0]);
dup2(pfd2[1],1);
close(pfd2[1]);
prgname = (char*)"sort";
execlp(prgname,prgname,0);
}
else
{
close(pfd2[1]); //close the write end of the pipe
dup2(pfd2[0],0);//connect the pipes
close(pfd2[0]); //close extra file descriptor
prgname = (char*)"more"; //commands[1];//now run the second command
execlp(prgname, prgname, 0);//Load the program
}
}
return 0;
}
为了简单起见,我对所有值进行了硬编码。 程序显示“dmesg|more”的输出,但不执行排序部分,然后冻结。我在左下角看到 dmesg 等请求,但我无法再查看。
有什么想法吗?
I have successfully piped the output of one command into the input of another and then show the output of the second command to the screen.
I want to do this with three successive commands. (actually eventually I want to do it with N commands passed into the program at run time.
This is my attempt at pipelining three commands together.
UPDATED: i updated my question to reflect my latest try.
#include <string.h>
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
int main(int argc, char * argv[])
{
pid_t pid;
int pfd[2];
char* prgname = NULL;
if(pipe(pfd) == -1)
{
perror("error on pipe call");
return(1);
}
for(int j = 0;j<numberOfCommands;j++)
{
cout<<commands[j]<<"_"<<endl;
}
pid = fork();
if(pid == 0){//child process
close(pfd[0]); //close read end of pipe
dup2(pfd[1],1);//connect the pipes
close(pfd[1]);//close extra file descriptors
prgname = (char*)"dmesg"; //commands[0];//first command
execlp(prgname, prgname, 0);//Load the program
}
else
{
int pfd2[2];
if(pipe(pfd2) == -1)
{
perror("error on pipe call 2");
return(1);
}
pid = fork();
if(pid == 0)//child
{
close(pfd[1]);
dup2(pfd[0],0);
close(pfd[0]);
close(pfd2[0]);
dup2(pfd2[1],1);
close(pfd2[1]);
prgname = (char*)"sort";
execlp(prgname,prgname,0);
}
else
{
close(pfd2[1]); //close the write end of the pipe
dup2(pfd2[0],0);//connect the pipes
close(pfd2[0]); //close extra file descriptor
prgname = (char*)"more"; //commands[1];//now run the second command
execlp(prgname, prgname, 0);//Load the program
}
}
return 0;
}
i have hard coded all values for simplicity.
The program displays what should be the output of "dmesg|more" but does not do the sort part and then freezes. I see the begging of dmesg and more in the bottom left but i can not view any more.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pipe(2)
仅为 1 个管道提供 2 个文件描述符。第三个文件描述符 (pfd[2]
) 是垃圾并且永远不会被初始化。如果要创建一个包含 3 个命令的管道,则需要调用两次pipe()
来获得两个管道:一个用于连接第一个和第二个进程,一个用于连接第二个和第三个进程流程。pipe(2)
only provides 2 file descriptors for 1 pipe. The 3rd file descriptor (pfd[2]
) is junk and is never getting initialized. If you want to create a pipeline with 3 commands in it, you need to callpipe()
twice to get two pipes: one for connecting the first and second processes, and one for connecting the second and third processes.