简体   繁体   English

在外壳中实施管道

[英]Implementing pipes in shell

I am trying to implement pipes in a simple shell but unable to get it working. 我试图在一个简单的外壳中实现管道,但无法使其正常工作。 Can some one please suggest what I might be doing wrong. 有人可以建议我可能做错了什么吗? Read other similar posts but the logic applied is different in other posts. 阅读其他类似的帖子,但是在其他帖子中应用的逻辑不同。 When i run the program giving a command like : cat shell.c | 当我运行程序时,给出如下命令:cat shell.c | grep int it gives broken pipe error. grep int它给出了管道破裂的错误。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#define BUFFER_SIZE 1<<16
#define ARR_SIZE 1<<16

void parse_args(char *buffer, char** args, 
            size_t args_size, size_t *nargs)
{
char *buf_args[args_size]; /* You need C99 */
char **cp;
char *wbuf;
size_t i, j;

wbuf=buffer;
buf_args[0]=buffer; 
args[0] =buffer;

for(cp=buf_args; (*cp=strsep(&wbuf, " \n\t")) != NULL ;){
    if ((*cp != '\0') && (++cp >= &buf_args[args_size]))
        break;
}

for (j=i=0; buf_args[i]!=NULL; i++){
    if(strlen(buf_args[i])>0)
        args[j++]=buf_args[i];
}

*nargs=j;
args[j]=NULL;
}


int main(int argc, char *argv[], char *envp[]){
char buffer[BUFFER_SIZE];
char *args[ARR_SIZE];

int *ret_status;
size_t nargs;
pid_t pid;
size_t j;
int pipe_fd[2];
      pipe(pipe_fd); // should pipe be placed here?    
while(1){
    printf("$ ");
    fgets(buffer, BUFFER_SIZE, stdin);
    parse_args(buffer, args, ARR_SIZE, &nargs); 

    if (nargs==0) continue;
    if (!strcmp(args[0], "exit" ) ) exit(0);       
    pid = fork();
    if(pid==0)
     {
     dup2(pipe_fd[0],0);
       close(pipe_fd[1]);

        j=execvp(args[0], args);
        if(j)
        {
            puts(strerror(errno));
            exit(127);
        }

    }else{
            dup2(pipe_fd[1],1);
           close(pipe_fd[0]);
          pid = wait(ret_status);
         }
  }    
 return 0;
}

When your while loop finishes, the parent process loops back and forks again, the trouble is you have already closed an end of the pipe in the parent process, so it will be closed in the child process. 当您的while循环结束时,父进程将循环返回并再次进行分叉,问题是您已经在父进程中关闭了管道的一端,因此它将在子进程中关闭。 To fix it, make sure both pipes are closed at the end of loop, and re-call the pipe function within the loop. 要对其进行修复,请确保在循环结束时关闭两个管道,然后在循环中重新调用管道功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM