Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Created March 31, 2017 18:16
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 morsoinferno/cbfe9a28cd59812af14babd5e572ceaf to your computer and use it in GitHub Desktop.
Save morsoinferno/cbfe9a28cd59812af14babd5e572ceaf to your computer and use it in GitHub Desktop.
06_dup2
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
void main()
{
int fd[2];
int pid;
//time_t start = time(NULL), end;
//if (pid = fork() == 0){
if (pipe(fd) == -1) {
printf("No se pudo crear el pipe\n");
exit(-1);
}
if ((pid = fork()) == -1) {
printf("no se pudo crear un hijo\n");
exit(-1);
}
if (pid == 0) { //soy hijo
dup2(fd[1], STDOUT_FILENO);
//close(STDOUT_FILENO); // cierra descriptor 1
//dup(mi_pipe[1]); // duplica descriptor mi_pipe[1] con 1
close(fd[0]); // cierra lado de lectura del pipe
//close(fd[1]);
execlp("ls", "ls", (char*)NULL); // si esto funciona no hay retorno
}
else {
dup2(fd[0], STDIN_FILENO);
//close(STDIN_FILENO); // cierra descriptor 0
//dup(mi_pipe[0]); // duplica descriptor mi_pipe[0] con 0
close(fd[1]); // cierra lado de escritura
//close(fd[0]);
execlp("wc", "wc", "-l", (char*)NULL); // si esto funciona no hay retorno
}
/*}else{
wait(NULL);
end = time(NULL);
printf("Time: %ld\n", start);
printf("Time: %ld\n", end);
printf("Time: %ld\n", end - start);
printf("Time: %.f\n", difftime(start, end));
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment