Skip to content

Instantly share code, notes, and snippets.

@LucasKuhn
Created February 21, 2022 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucasKuhn/18e1fa2ea65a0c2c797dfce172990750 to your computer and use it in GitHub Desktop.
Save LucasKuhn/18e1fa2ea65a0c2c797dfce172990750 to your computer and use it in GitHub Desktop.
Exemplo de pipes
# include <fcntl.h>
# include "string.h"
# include <unistd.h>
int main()
{
int pipe_fds[2];
pipe(pipe_fds);
printf("Antes de redirecionar...\n");
int file_fd = open("testfile", O_RDWR);
char buffer[1024];
int read_return;
dup2(pipe_fds[1], STDOUT_FILENO); // Taquei stdout na saída do cano
printf("oi!\n"); // Escrevi no cano
close(pipe_fds[1]); // Fechei a saída do cano
read_return = read(pipe_fds[0], buffer, 1024); // Li do cano
close(pipe_fds[0]); // Fechei a entrada do cano (ja li né)
write(file_fd, buffer, strlen(buffer)); // Escrevi do buffer (lido do cano 🎉)
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment