elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Los 10 CVE más críticos (peligrosos) de 2020


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / Procesos con fork en: 23 Mayo 2020, 02:05 am
Hola a todos. Estoy viendo el tema de procesos con fork y tuberías con pipe y haciendo un mini shell (o intentando :D). Mi problema se presenta cuando, para decirlo más gráficamente, encuentro un comando o mandato como este:

Código
  1. ls | grep 'patron' | wc -l

Entonces, en este caso son 3 comandos. Tengo un código de prueba pero no esta funacionando. Si alguien pasó por esto o tiene alguna sugerencia bienvenido sea.

Código
  1. int main (int argc, char *argv[]) {
  2. int i,
  3. fd[2], fd0[2],
  4. pid[3];
  5.  
  6. /* Creación recurso tubería, por el padre */
  7. pipe(fd);
  8.  
  9. /* 1er hijo, ejecuta ls */
  10. if ( (pid[0]= fork()) == 0) {
  11. dup2(fd[1], STDOUT_FILENO);
  12. close(fd[0]);
  13. execlp("/bin/ls", "ls", NULL);
  14. perror("Hijo1: Fallo al hacer exec");
  15. exit (1);
  16. }
  17.  
  18. pipe(fd0);
  19.  
  20. /* 2do hijo, ejecuta filtro */
  21. if ( (pid[1]=fork()) == 0) {
  22. dup2(fd[0], STDIN_FILENO);
  23. close(fd[1]);
  24. dup2(fd0[1], STDOUT_FILENO);
  25. close(fd0[0]);
  26. char* arg[] = { "sort", "sort", NULL };
  27. execvp(arg[0], &arg[1]);
  28. //execvp(argv[1], &argv[1]);
  29. perror("Hijo2: Fallo al hacer exec");
  30. exit(1);
  31. }
  32.  
  33. if((pid[2]=fork()) == 0) {
  34. dup2(fd0[0], STDIN_FILENO);
  35. close(fd0[1]);
  36. char* arg[] = { "wc", "wc", "-l", NULL };
  37. execvp(arg[0], &arg[1]);
  38. perror("Hijo2: Fallo al hacer exec");
  39. exit(1);
  40. }
  41.  
  42. /* El padre no interviene */
  43. close(fd[0]);
  44. close(fd[1]);
  45. close(fd0[0]);
  46. close(fd0[1]);
  47. /*for (i=0; i<2; i++)
  48. wait(pid[i]);*/
  49. exit (0);
  50. }
  51.  
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines