为C Shell项目实现管道

时间:2019-05-15 10:46:26

标签: c shell command pipe

所以我有一个项目,涉及用C创建一个简单的shell,其功能之一就是支持管道,例如,像这样的程序:

./ a.out | 。/回合 会将标准输出从./a.out重定向到./b.out

的标准输入

处于我的分配状态的指令使用2个fork()调用,每个命令一个。

这是我到目前为止所拥有的...但是我不知道如何使用2个fork调用,甚至不知道该怎么做...我将不胜感激任何提示!谢谢!

void doPipe()
{
    // here I have 2 int arrays that will be passed in to pipe(),
    // since pipe takes in an int array of file descriptors to know 
    // where to get input and store output
    int p1[2] = {STDIN_FILENO, STDOUT_FILENO};
    int p2[2] = {STDIN_FILENO, STDOOUT_FILENO};

    // here i fork to run child process
    pid_t pID = fork();

    if (pID == 0} { do stuff in child; }
    else { do stuff in parent;}
}

0 个答案:

没有答案