创建子进程时重定向输出问题。在POSIX中编写自己的Shell

时间:2013-10-21 08:05:15

标签: c linux shell process io-redirection

我正在尝试重定向输出,但我有2个问题。问题1:ls -l>文件工作正常,但如果我做cat file1> file2,我的shell似乎在后台无限期地工作。我必须在等待2到3分钟后打断它(Ctrl-D)。 问题2:使用sort<时出现同样的问题水果,我的shell似乎等待一个过程完成,但它永远不会。所以我必须打断它。我知道我做的不对,但我似乎无法弄清楚什么是错的/缺失的。我还没有实现管道。我们将非常感谢您的帮助。

int create_childprocess(char *argv[], int argc)
{

    //int pid;

    int fd, i, ret;

    pid_t pid;


    int redirect_sign =0;

    if((pid = fork()) == -1)
    {
        /*error exit -fork failed*/
        perror("Fork failed");
        exit(-1);
    }
    if(pid == 0)
    {
        /*this is the child*/
        printf("\nThis is the child ready to execute: %s\n", argv[0]);

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

            if((strcmp(">", argv[i])) ==0)
                redirect_sign=1;

            else if((strcmp(">>", argv[i])) ==0)
                redirect_sign=2; 

            else if((strcmp("<", argv[i])) ==0)
                redirect_sign=3;

             else if((strcmp("<<", argv[i])) ==0)
                redirect_sign=4;  

        }   

        if (redirect_sign==1)  //if ">" is found...
        {

            fd = open(argv[argc-1],O_TRUNC | O_WRONLY | O_CREAT, 0755);
            if(fd == -1)
            {   
                /*An error occured. Print an error message and bail.*/
                perror("open");
                exit(-1);   
            }   
            else
            {
                printf("Writing output of the command %s to file created\n", argv[0]);
                dup2(fd,1);

                execlp(argv[0], argv[0], NULL);
                close(fd);

            }


        }
        else if (redirect_sign==2)  //if ">>" was found...
        {
            fd = open(argv[argc-1], O_WRONLY | O_APPEND | O_CREAT, 0755);
            if(fd == -1)
            {   
                /*An error occured. Print an error message and bail.*/
                perror("open");
                exit(-1);   
            }   
            else
            {
                printf("Appending output of the command %s to file created\n", argv[0]);
                dup2(fd,1);

                execlp(argv[0], argv[0], NULL);
                close(fd);  
            }
        }
        else if (redirect_sign==3)  //if "<" was found...
        {
            fd = open(argv[argc-1], O_TRUNC | O_WRONLY | O_CREAT, 0755);
            if(fd == -1)
            {   
                /*An error occured. Print an error message and bail.*/
                perror("open");
                exit(-1);   
            }   
            else
            {
                printf("Writing content of file %s to disk\n", argv[argc-1]);
                dup2(fd,1);

                execlp(argv[0], argv[0], NULL);
                close(fd);  
            }
        }
        else if (redirect_sign==4)  //if "<" was found...
        {
            fd = open(argv[argc-1], O_TRUNC | O_WRONLY | O_CREAT, 0755);
            if(fd == -1)
            {   
                /*An error occured. Print an error message and bail.*/
                perror("open");
                exit(-1);   
            }   
            else
            {
                printf("Writing content of file %s to %s \n", argv[argc-1], argv[0] );
                dup2(fd,1);


                execlp(argv[0], argv[0], NULL);
                close(fd);  
            }
        }
        else  //if ">" or ">>" or "<" or "<<" was not found 
        {
            execvp(argv[0], &argv[0]);

            /*error exit - exec returned*/
            perror("Exec returned");
            exit(-1);
        }

    }
    else
    {
        /*this is the parent -- wait for child to terminate*/
        wait(pid,0,0);
        printf("\nThe parent is exiting now\n");
    }

    free (argv);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

如果不仔细查看代码,可能不会关闭文件描述符。特别是,在调用close(fd)之前,您肯定希望execlp,并且要非常小心,您已经关闭了所创建的任何管道中的所有其他相关文件描述符。更正此特定错误并关闭输出文件不太可能解决您的问题;更有可能是你没有显示的代码中有错误,你已经打开了一些管道,以便cat等待永远不会来的EOF,因为它有管道的写入侧打开,并且该管道的读取端是它自己的标准输入。

答案 1 :(得分:0)

您正在使用dup2(fd,1);在所有情况下。我认为对于input(stdin),你想要替换文件描述符0(或预定义的宏STDIN_FILENO。