Jump to content
Fórum Script Brasil
  • 0

Usando pipes


demonofnight

Question

Dae, estou tentando usar pipes com um sort como exemplo, quero fazer uma enrada de dados e pegar a saida, porem o codigo não funciona, estou meio perdido na teoria, alguém poderia me ajudar?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#define WRITE 1
#define READ 0

using namespace std;

int main()
{
        char    buf[5];
        int     fd_in[2], fd_out[2],
                c;
        pipe(fd_in);pipe(fd_out);
        if(fork() == 0)
        {
                close(fd_in[WRITE]);dup2(fd_in[READ], READ);
        close(fd_out[READ]); dup2(fd_out[WRITE], WRITE);

        dup2(fd_out[WRITE], WRITE); //stdout -> pipe's write (everything what would go to stdout will go to the pipe's write...
                execlp("sort", "sort", NULL);
        }

        write(fd_out[1], "c\n", 2);
        write(fd_out[1], "a\n", 2);
        write(fd_out[1], "b\n", 2);

        while((c = read(fd_in[0], buf, 5)) > 0)
                write(1, buf, c);

        return  0;
}

Vlw pelo help galera

Edited by kuroi
Adicionar tag CODE
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...