连接N个命令在一个shell管道? [英] Connecting n commands with pipes in a shell?
问题描述
我想实现我C.可以执行简单的指令只是一个简单的execvp(罚款),但要求之一就是管理这样的命令的shell:ls -l命令|头|尾-4与一个for循环,只有一个人管()'语句重定向stdin和stdout。现在天后我有点失落。
I am trying to implement a shell in C. I can execute simple commands just fine with a simple execvp() but one of the requirements is to manage commands like this: "ls -l | head | tail -4" with a 'for' loop and only one 'pipe()' statement redirecting stdin and stdout. Now after days I'm a bit lost.
N =简单命令的数量(本例中3:LS,头,尾)
命令=与结构的命令的列表,像这样的:
N = Number of simple commands (3 in the example: ls, head, tail) commands = a list of structs with the commands, like this:
commands[0].argv[0]: ls
commands[0].argv[1]: -l
commands[1].argv[0]: head
commands[2].argv[0]: tail
commands[2].argv[1]: -4
所以,我做了循环,并开始重定向stdin和stdout,以连接所有管道命令,但......我只是无能,为什么它不能正常工作。
So, I made the for loop, and started to redirect stdin and stdout in order to connect all the commands with pipes, but...I'm just clueless why it doesn't work.
for (i=0; i < n; i++){
pipe(pipe);
if(fork()==0){ // CHILD
close(pipe[0]);
close(1);
dup(pipe[1]);
close(pipe[1]);
execvp(commands[i].argv[0], &commands[i].argv[0]);
perror("ERROR: ");
exit(-1);
}else{ // FATHER
close(pipe[1]);
close(0);
dup(pipe[0]);
close(pipe[0]);
}
}
我想创建一个childed过程中行:
What I want to create is a 'line' of childed processes:
[ls -l命令] ----管----> [主治] ----管----> [尾-4]
[ls -l] ----pipe----> [head] ----pipe----> [tail -4]
所有这些过程有一个根(过程乳宁我的壳),因此,第一个父亲也是shell进程的孩子,我有点早已疲惫不堪,任何人都可以帮助我在这里吗?
All this processes have a root (the process runing my shell) so, the first father is also a child of the shell process, I'm a bit exhausted already, can anyone help me here please?
我甚至不知道,如果孩子的应该是执行命令的人。
I'm not even sure if the childs should be the ones executing the commands.
谢谢你们!!
推荐答案
没有什么复杂在这里,只需记住,最后的命令应该输出到原来进程的文件描述符1,首先应该从原来的进程文件描述符0阅读你只是在产卵顺序过程中,沿previois的输入端承载着管道
电话。
Nothing complex here, just have in mind that the last command should output to the original process' file descriptor 1 and the first should read from original process file descriptor 0. You just spawn the processes in order, carrying the along the input side of the previois pipe
call.
所以,在这里的都是类型:
So, here's are the types:
#include <unistd.h>
struct command
{
const char **argv;
};
请用一个简单的定义良好的语义一个辅助函数:
Make a helper function with a simple well defined semantics:
int
spawn_proc (int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
和这里的主叉常规:
int
fork_pipes (int n, struct command *cmd)
{
int i;
pid_t pid;
int in, fd [2];
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
for (i = 0; i < n - 1; ++i)
{
pipe (fd);
/* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */
spawn_proc (in, fd [1], cmd + i);
/* No need for the write end of the pipe, the child will write here. */
close (fd [1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = fd [0];
}
/* Last stage of the pipeline - set stdin be the read end of the previous pipe
and output to the original file descriptor 1. */
if (in != 0)
dup2 (in, 0);
/* Execute the last stage with the current process. */
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
和一个小测试:
int
main ()
{
const char *ls[] = { "ls", "-l", 0 };
const char *awk[] = { "awk", "{print $1}", 0 };
const char *sort[] = { "sort", 0 };
const char *uniq[] = { "uniq", 0 };
struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} };
return fork_pipes (4, cmd);
}
似乎工作。 :)
Appears to work. :)
这篇关于连接N个命令在一个shell管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!