【问题标题】:Using pipes to implement simple shell in c? [duplicate]使用管道在c中实现简单的shell? [复制]
【发布时间】:2014-03-02 18:53:58
【问题描述】:

我正在使用fork 和execlp 在c 中构建一个简单的shell。我将得到一组用管道分隔的命令。例如:ls -l | wc -l .

我想使用管道进行进程内通信。所以ls -l 的输出是wc -l 的输入。可以有任意数量的由管道分隔的命令。我不明白是否在子进程和父进程之间创建一个管道,然后当我从子进程获得输出时,以某种方式将该输出传输到另一个子进程......我已经解析了输入。我该怎么办?

void excueteCommands() {

    int i, j;
    int fd[2];
    int cid1;

    commandNode* ptr = head;

    while (ptr != NULL) {

        for (i = 0; i <= pipeCount; i++) {

            cid1 = fork();

            if (!cid1) {

                if (i != 0) {

                    dup2(fd[i - 1][0], 0);
                }

                if (i != pipeCount) {

                    dup2(fd[i][1], 1);
                }

                for (j = 0; j < pipeCount; j++) {

                    close(fd[j][0]);
                    close(fd[j][1]);
                }

                execlp(ptr->command, ptr->args, NULL);
                exit(0);
            }

            ptr = ptr->next;
        }
    }

    for (i = 0; i < pipeCount; i++) {
        close(fd[i][0]);
        close(fd[i][1]);
    }

} 

【问题讨论】:

  • 检查此页面右侧的所有“相关”问题。也许其中一些已经涵盖了您的要求。

标签: c


【解决方案1】:

我去年也有同样的任务。您不需要单独处理从一个进程到另一个进程的输入和输出。只需初始化一系列流程之间的管道,输入和输出流程就会自行发生。您只需要为第一个命令提供输入并从最后一个命令获取输出。

以下是我使用的代码 sn-p。

#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <sys/wait.h>

int exit_flag = 0;

#define ARG_SIZE 100



int main ()  
{

    int size = 1000,i,j;
    int pipe_count = 0;
    int fd[100][2],cid1,cid2,length,status;


    char string[][100] = {"ls","wc"};

    pipe_count = 1;

    if(pipe_count)
    {
        for(i = 0;i < pipe_count;i++)
        {
            pipe(fd[i]);
        }

        for(i = 0;i <= pipe_count;i++)
        {
            cid1 = fork();
            if(!cid1)
            {

                if(i!=0)
                {
                    dup2(fd[i-1][0],0);
                }


                if(i!=pipe_count)
                {
                    dup2(fd[i][1],1);
                }


                for(j = 0;j < pipe_count;j++)
                {   
                        close(fd[j][0]);
                        close(fd[j][1]);
                }

                execlp(string[i], string[i], NULL);
                exit(0);
            }


        }
        for(i = 0;i < pipe_count;i++)
        {
            close(fd[i][0]);
            close(fd[i][1]);
        }
        waitpid(cid1,&status,0);


    }
    else
    {
        execlp(string[0], string[0], NULL);
    }


    return 0;
}

在上面的代码中,string[] 数组包含了按顺序用'|' 分隔的所有命令。 fd[][2] 是文件描述符数组。 fd[i][0] 是 i+1 命令的输入,fd[i][1] 是 ith 命令的输出。所以i 的输出使用fd[i] 管道进入i+1 的输入。

我现在不记得如何处理 dup2 和 close 命令了。愿你能得到它们,因为你最近完成了它们。我有任何疑问,然后我会尽力澄清。

【讨论】:

  • 你能解释一下你的代码吗?我不明白这些进程中的 n 个将如何相互通信。 N 个不同的命令....
  • 你知道dup2,关闭命令吗?
  • 是的,我确实知道它们。我的问题是如何为 n 个不同的命令进行缩放
  • 我已经解析了信息并且我有命令的链接列表。我需要知道如何执行它们并使用 dup2 将最终输出到主程序。
  • 如果你愿意,我可以发布我的解析器代码...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2013-03-21
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多