管道和重定向排序

时间:2018-11-23 15:45:41

标签: redirect pipe fork

我制作排序程序。

     +--------------+                +--------------+
stdin   >0      >== pipe to sort ====>      |
     |   in_out     |        |             sort     |
stdout  <1      <== pipe from sort ==<      |
     +--------------+                +--------------+

我的想法就是这样。我做的不好。 我不知道哪里出了问题。我认为我做得很好,管道和重定向。 我找不到我的错误。


首先,我得到两个管道。 sencend,fork(input_output和sort) 连接标准输入并输出到管道,然后执行排序 获取字符串并发送排序和排序,然后返回并执行。 然后关闭管道并分类模具。

 #include   <stdio.h>
    #include    <unistd.h>
    #include    <stdlib.h>
    #include    <sys/types.h>
    #include    <sys/stat.h>
    #include    <sys/wait.h>
    #include    <string.h>
    #include    <fcntl.h>


    #define oops(m,x)   { perror(m); exit(x); }

    void be_sort(int in[2], int out[2]);
    void in_out(int todc[2], int fromdc[2]);
    void fatal(char mess[]);

    int main()
    {
        int pid, tosort[2], fromsort[2];    /*equipment */

        /* make two pipes */
        if( pipe(tosort) == -1 || pipe(fromsort) == -1 )
            oops("pipe failed", 1);

        /* get a process for user interface */
        if(( pid = fork()) == -1 )
            oops("cannot fork", 2);
        if( pid == 0 )              /* child is sort    */
            be_sort(tosort, fromsort);
        else {
            in_out(tosort, fromsort);       /* parent is ui */
            wait(NULL);         /* wait for child */
        }
        return 0;

    }

    void be_sort(int in[2], int out[2])
    /*
     *  set up stdin and stdout, then execl sort
     */
    {
        /* setuup stdin from pipein */
        if( dup2(in[0], 0) == -1 )  /* copy read end to 0   */
            oops("sort: cannot redirect stdin", 3);
        close(in[0]);           /* moved to fd 0    */
        close(in[1]);           /* won't write here */

        /* setup stdout to pipeout */
        if( dup2(out[1], 1) == -1)  /* dupe write end to 1  */
            oops("sort: cannot redirect stdout", 4);
        close(out[1]);          /* moved to fd 1    */
        close(out[0]);          /* won't read from here */

        /* now execl sort with the - option */
        execlp("sort", "sort", (char *)NULL);
        oops("Cannot run sort", 5);
    }

    void in_out( int tosort[2], int fromsort[2])
    {
        char    operation[BUFSIZ], message[BUFSIZ], *fgets();
        FILE    *fpout, *fpin, *fdopen();

        /* setup */
        close(tosort[0]);           /* won't read from pipe to sort */
        close(fromsort[1]);     /* won't write to pipe from sort*/

        fpout = fdopen( tosort[1], "w" );   /* convert file descriptors to stream */
        fpin  = fdopen( fromsort[0], "r" );

        if( fpout == NULL || fpin == NULL )
            fatal("Error converting pipes to streams");

        /* main loop */
        while( fgets(message, BUFSIZ, stdin) != NULL ) 
        {
            message[strlen(message)-1]=NULL;
            printf("%s ,%ld\n",message,strlen(message));

            if (fprintf(fpout, "%s", *message) == EOF)
                fatal("writing");

            fflush( fpout );

            if (fgets(operation, BUFSIZ, fpin) == NULL)
                break;
            printf("%s", operation); // stdout
        }
        fclose(fpout);  /* close pipe       */
        fclose(fpin);   /* sort will see EOF    */
    }
    void fatal( char mess[] )
    {
        fprintf(stderr, "Error: %s\n", mess);
        exit(1);
    }

0 个答案:

没有答案