Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 创建子进程时重定向输出问题。在POSIX中编写自己的Shell_C_Linux_Shell_Process_Io Redirection - Fatal编程技术网

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

C 创建子进程时重定向输出问题。在POSIX中编写自己的Shell,c,linux,shell,process,io-redirection,C,Linux,Shell,Process,Io Redirection,我试图重定向输出,但我有两个问题。问题1:ls-l>文件工作正常,但是如果我使用cat file1>file2,我的shell似乎在后台无限期地工作。在等待2到3分钟后,我必须中断Ctrl-D。 问题2:使用sortfile2工作正常。但我仍然有问题2,输入来自文件。@fredvanl,我不断收到错误消息:如果我将文件描述符从1更改为STDIN_FILENO或0,则文件描述符不正确。在这种情况下,可能是时候开始实现代码的真正管道部分,并开始填充管道,以便进程可以实际读取它了。威廉已经记下了。

我试图重定向输出,但我有两个问题。问题1:ls-l>文件工作正常,但是如果我使用cat file1>file2,我的shell似乎在后台无限期地工作。在等待2到3分钟后,我必须中断Ctrl-D。 问题2:使用sort
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;
}

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

您使用的是dup2fd,1;在所有情况下。我认为,对于输入STDIN,您需要替换文件描述符0或预定义的宏STNDYFILNOR.

。您可能会考虑再次读取Maun-Excel,尤其是在如何传递参数的方式上。或者重新考虑使用execvp。@William Pursell,所有文件描述符都已关闭,但是,在execlp之前,我并没有关闭它们。现在,cat file1>file2工作正常。但我仍然有问题2,输入来自文件。@fredvanl,我不断收到错误消息:如果我将文件描述符从1更改为STDIN_FILENO或0,则文件描述符不正确。在这种情况下,可能是时候开始实现代码的真正管道部分,并开始填充管道,以便进程可以实际读取它了。威廉已经记下了。