труба и сортировка перенаправления - PullRequest
0 голосов
/ 23 ноября 2018

Я делаю программу сортировки.

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

моя идея такая.я не работал хорошо .... я не знаю, где проблема.Я думаю, что я хорошо трубу и перенаправление.Я не могу найти свои ошибки.


Во-первых, я получаю две трубы.sencend, fork (input_output и sort) подключают stdin и out к каналам, затем execl sort получают строку и отправляют сортировку и сортировку, а затем возвращают и prnit.затем закройте трубу и сортируйте плашки.

 #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);
    }
...