2012-06-03 43 views
2

试图编写一个处理内部和外部命令的shell。我可以一次获得内部命令和一个外部命令。使用execv和输入重定向的C++流水线命令

我的问题是如何获得这样的命令来运行:“ls -l命令| grep的LIB |厕所-l”

我用叉子(),并通过execv通过外部命令()在一个char * []中。

有关如何工作的任何想法?我假设使用pipe()或其他东西,但我不确定。

问题的第二部分:如何处理I/O重定向?任何人都可以指给我一个有用的地方吗?

编辑 到目前为止,@Alex W是我的英雄。然而,由于我是pipe()和dup2()命令的新手,我对每个调用和变量都有些犹豫。

下面的代码我有一个处理一个单一的外部命令(例如=“ls -l命令-A”):

pid_t pid; 
    pid = fork(); 
    if (pid < 0) 
    { 
     cout << "Fork failed." << endl; 
    } 
    else if (pid == 0) 
    { 
     execvp(exec_args[0], exec_args); //exec_args is a char*[] where 
     _exit (EXIT_FAILURE);    //exec_args[0] contains "/bin/ls" 
    }           //[1]="ls" and [2]="-l" [3]="-a" 
    else 
    { 
     int status; 
     waitpid(pid, &status, 0); 
    } 
    break; 
+0

你建议的方式正是我如何做到的。 (顺便说一下,您可能会将您的问题标记为“posix”,因为它不适用于Windows程序员 - 即使您必须删除其中一个其他标记才能执行此操作。) – thb

+0

我在标记中添加了posix。基本上我正在寻找一些有关“pipe()”如何工作的见解 – twsmale

+0

我明白了。试试'man 7 pipe'。 (您可能知道手册的第7部分是系统概念记录的地方。) – thb

回答

2

你使用pipe和POSIX系统的管道工程以类似的方式来一份文件。您可以写入管道并从管道中读取,但如果管道中没有任何内容会阻塞。查找有关Unix系统调用信息的最佳位置是系统调用的手册页。在Unix系统上,您可以在终端中输入man pipe。如果你没有访问Unix终端,那么只需google“man pipe”。关于手册页的好处是他们告诉你为给定的系统调用包含哪些库。确保你记得在使用任何exec类型系统调用时,你正在将一个全新的进程加载到该内存中,并且正在执行的进程将在其轨道中停止。

要使用它做这样的事情:

int main() 
{ 
    int id[2]; 
    int fd[2]; 
    int fd2[2]; 
    int fd3[2]; 
    FILE file; 
    int status; 
    int sz = 0; 
    char buff[1000]; 
    char buff2[1000]; 
    string launch[2]; 
    FILE *fp; 

    launch[0] = "./anotherProgramToExecute"; 
    launch[1] = "./yetAnotherProgram"; 

    pipe(fd); 
    pipe(fd2); 
    pipe(fd3); 

    for(int i = 0; i < 2; i++) 
    { 
     id[i] = fork(); 

     if (id[i] == -1) /* an error occurred */ 
     { 
      perror("Fork failed.\n"); 
     } 
     else if (id[i] == 0) /* this is the child process currently acting */ 
     { 
      if(i == 0) 
      { 
       dup2(fd[1],1); 
      } 
      else if(i == 1) 
      { 
       dup2(fd2[0],0); 
       dup2(fd3[1],1); 
      } 
      execlp(launch[i],launch[i], NULL); 
     } 
     else /* this is the parent process currently acting */ 
     { 
      sz = read(fd[0], buff, 1000); 
      buff[sz] = '\0'; 
      printf("buff = %s\n",buff); 

      close(fd[0]); 

      write(fd2[1],buff, 1000); 

      read(fd3[0],buff2,1000); 

      fp = fopen("bin.txt","w"); 
      if(fp == NULL) 
       printf("Cannot open file.\n"); 
      else 
      { 
       fprintf(fp,buff2); 
       fclose(fp); 
      } 

      //printf("Creation of Child Process #%d succeeded!\n",id[i]); 
      while(waitpid(id[i], &status, WNOHANG) == 0) 
       sleep(0.3); 
      if (WIFEXITED(status)) 
      { 
       // successfully terminated children 
      } 
      else 
      { 
       perror("Child has not terminated correctly.\n"); 
      } 
     } 
    } 
} 
+0

非常有帮助,但是你能否偶然评论一些变量,以便我知道每个人都在做什么?就像我说的,我是一个管道()noob – twsmale

+0

当你调用pipe(fd)时,它用两个文件描述符填充int fd [2]数组。在索引0处,它将放置读取文件描述符,并且在索引1处(即fd [1]),它将放置写入文件描述符。您可以使用它们来读取和写入管道,就像它是一个常规文件一样。 –

0

最好的在线教程关于管道和这种是一个过时的歌曲,但糖果我所看到的:http://beej.us/guide/bgipc/。你可以浏览它。有人怀疑你会喜欢它。