LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 944|回复: 4

以下的实现管道功能的程序为什么不能运行?

[复制链接]
发表于 2003-3-2 21:28:57 | 显示全部楼层 |阅读模式
//以下程序实现的功能是  ls -l | grep mos.c | wc -l
//但是不知哪里出了问题,结果明显错误

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <dirent.h>
#include "mos.h"
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif



int main()
{
  int num=2;
  int i=0;
  pid_t pid[3];
  pid[0]=pid[1]=pid[2]=1;
  int fd[2][2];
  while(i<=num&&(pid=fork())==0)
  {
      if(i==num)
      {
        close(fd[i-1][0]);
        dup2(fd[i-1][1],STDOUT_FILENO);
        close(fd[i-1][1]);
        i++;
      }
      else if(i==0)
      {
        pipe(fd);
        close(fd[1]);
        dup2(fd[0],STDIN_FILENO);
        close(fd[0]);
        i++;
      }
      else
      {
        pipe(fd);
        close(fd[i-1][1]);
        dup2(fd[i-1][0],STDIN_FILENO);
        close(fd[i-1][0]);
        close(fd[0]);
        dup2(fd[1],STDOUT_FILENO);
        close(fd[1]);
        i++;
      }
  }
  

  if(pid[2]==0)
  {
     execlp("ls","ls","-l",0);
  }
   if(pid[1]==0)
  {
    execlp("grep","grep","mos.h",0);
  }
  if(pid[0]==0)
  {
    execlp("wc","wc-l",0);
  }
   wait();
}
 楼主| 发表于 2003-3-2 21:33:36 | 显示全部楼层
num//代表的是管道数目
发表于 2003-3-2 22:07:32 | 显示全部楼层
fork()之后,你的子进程干什么去了?
 楼主| 发表于 2003-3-2 22:46:23 | 显示全部楼层
fork()之后子进程继续创建子进程.
 楼主| 发表于 2003-3-2 23:18:44 | 显示全部楼层
本程序功能的另一个实现的版本,却可以得出正确结果
/* MyShell! v1.0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <dirent.h>
#include "mos.h"
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#ifndef TIOCGWINSZ
#include <sys/ioctl.h>
#endif


void PrintTag();
void CallOutsideCommand( );


/* main() */
int main( void )
{
        printf( "\t\t\tMyShell! v1.0\n" );
        CallOutsideCommand(  );//&#205;&#226;&#178;&#191;&#195;ü&#193;&#238;
        return 0;
       
}





/* Show the tag if a new line '\n' was met. */
void PrintTag()
{
        //char Buf[MAXPATH]={0X00};
        //char* path =getcwd(Buf,MAXPATH);
        printf("\n%s","]@" );
       
}


  

void CallOutsideCommand(  )
{
  
            pid_t pid[3];
            int fd[2];
        int fds[2];
        char *param[4]={0x00};
        char *p_cmd;
        if((pid[0]=fork())<0)
        {
                fprintf(stderr,"fork error\n");
                return;
        }
        if(pid[0]==0)//in child1
        {
                if(pipe(fd)<0)
                {
                        fprintf(stderr,"pipe error\n");
                        return;
                }
                if(pid[1]=fork()==0)
                {
                        if(pipe(fds)<0)
                        {
                                fprintf(stderr,"pipe error\n");
                                return;
                        }
                        if(pid[2]=fork()==0)
                        {
                                close(fds[0]);//&#185;&#216;±&#213;&#182;&#193;&#182;&#203;
                                if(fds[1]!=STDOUT_FILENO)
                                {
                                        if(dup2(fds[1],STDOUT_FILENO)!=STDOUT_FILENO)
                                        {
                                                fprintf(stderr,"dup2 error to stdin\n");
                                                return;
                                        }
                                        close(fds[1]);
                                }                               
                                p_cmd="ls";
                                param[0]="ls";
                                param[1]="-l";
                                param[2]=NULL;
                                if(execvp(p_cmd,param)<0)
                                {
                                        fprintf(stderr,"execvp error for ls\n");
                                        PrintTag();
                                        return;
                                }
                        }
                        close(fds[1]);
                        if(fds[0]!=STDIN_FILENO)
                        {
                                if(dup2(fds[0],STDIN_FILENO)!=STDIN_FILENO)
                                {
                                        fprintf(stderr,"dup2 error to stdin\n");
                                        return;
                                }
                                close(fds[0]);
                        }
                        close(fd[0]);
                        if(fd[1]!=STDOUT_FILENO)
                        {
                                if(dup2(fd[1],STDOUT_FILENO)!=STDOUT_FILENO)
                                {
                                        fprintf(stderr,"dup2 error to stdout2\n");
                                        return;
                                }
                                close(fd[1]);
                        }
                        p_cmd="grep";
                        param[0]="grep";
                        param[1]="mos.c";
                        param[2]=NULL;
                        if(execvp(p_cmd,param)<0)
                        {
                                fprintf(stderr,"execvp error for grep\n");
                                return;
                        }
                }
                close(fd[1]);//&#185;&#216;±&#213;&#182;&#212;&#201;&#207;&#210;&#187;&#185;&#220;&#181;&#192;&#181;&#196;&#208;&#180;&#182;&#203;
                if(fd[0]!=STDIN_FILENO)
                {
                        if(dup2(fd[0],STDIN_FILENO)!=STDIN_FILENO)
                        {
                                fprintf(stderr,"dup2 error to stdout3\n");
                                return;
                        }
                        close(fd[0]);
                }
                p_cmd="wc";
                param[0]="wc";
                param[1]="-l";
                param[2]=NULL;
                if(execvp(p_cmd,param)<0)
                {
                        fprintf(stderr,"execl error for wc\n");
                        PrintTag();
                        return;
                }
        }       
        wait();
        return ;   
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表