Process Creation and Communication using Pipes [on hold]











up vote
-4
down vote

favorite












I am attempting to resolve the following problem:




Write a program in C that executes the following commands the same way a command interpreter would: paste file1 file2 | sort | nl > file3. You must consider that file1, file2 and file 3 will be parameters given to your program in the command line.




#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>

#define BUF_SIZE 1024

int main(int argc, char *argv) {
int t[2];
int fd;

if (argc != 4) {
printf("Format: %s <archivo1> <archivo2> <archivo3>.n", argv[0]);
exit(1);
}

pipe(t);

if (fork() != 0) {
wait(NULL);
printf("Padren");

if (!(fd = open(argv[3], O_WRONLY))) {
printf("Error al abrir el archivo %s.n", argv[3]);
exit(1);
}

dup2(t[0], STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
close(t[0]);
close(t[1]);
execlp("nl", "nl", NULL);

} else {
if (fork() != 0) {
wait(NULL);
printf("Hijo 1n");
dup2(t[0], STDIN_FILENO);
dup2(t[1], STDOUT_FILENO);
close(t[0]);
close(t[1]);
execlp("sort", "sort", NULL);

} else {
printf("Hijo 2n");
dup2(t[1], STDOUT_FILENO);
close(t[0]);
close(t[1]);
execlp("paste", "paste", argv[1], argv[2], NULL);
}
}

return 0;
}


But once it reaches to the section where the sort command is executed, it continues using the standard input and not the pipe. Am I missing something? Where did I go wrong?










share|improve this question







New contributor




Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley 14 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley

If this question can be reworded to fit the rules in the help center, please edit the question.

















    up vote
    -4
    down vote

    favorite












    I am attempting to resolve the following problem:




    Write a program in C that executes the following commands the same way a command interpreter would: paste file1 file2 | sort | nl > file3. You must consider that file1, file2 and file 3 will be parameters given to your program in the command line.




    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/wait.h>

    #define BUF_SIZE 1024

    int main(int argc, char *argv) {
    int t[2];
    int fd;

    if (argc != 4) {
    printf("Format: %s <archivo1> <archivo2> <archivo3>.n", argv[0]);
    exit(1);
    }

    pipe(t);

    if (fork() != 0) {
    wait(NULL);
    printf("Padren");

    if (!(fd = open(argv[3], O_WRONLY))) {
    printf("Error al abrir el archivo %s.n", argv[3]);
    exit(1);
    }

    dup2(t[0], STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    close(t[0]);
    close(t[1]);
    execlp("nl", "nl", NULL);

    } else {
    if (fork() != 0) {
    wait(NULL);
    printf("Hijo 1n");
    dup2(t[0], STDIN_FILENO);
    dup2(t[1], STDOUT_FILENO);
    close(t[0]);
    close(t[1]);
    execlp("sort", "sort", NULL);

    } else {
    printf("Hijo 2n");
    dup2(t[1], STDOUT_FILENO);
    close(t[0]);
    close(t[1]);
    execlp("paste", "paste", argv[1], argv[2], NULL);
    }
    }

    return 0;
    }


    But once it reaches to the section where the sort command is executed, it continues using the standard input and not the pipe. Am I missing something? Where did I go wrong?










    share|improve this question







    New contributor




    Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.











    put on hold as off-topic by 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley 14 hours ago


    This question appears to be off-topic. The users who voted to close gave this specific reason:


    • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley

    If this question can be reworded to fit the rules in the help center, please edit the question.















      up vote
      -4
      down vote

      favorite









      up vote
      -4
      down vote

      favorite











      I am attempting to resolve the following problem:




      Write a program in C that executes the following commands the same way a command interpreter would: paste file1 file2 | sort | nl > file3. You must consider that file1, file2 and file 3 will be parameters given to your program in the command line.




      #include <stdlib.h>
      #include <stdio.h>
      #include <unistd.h>
      #include <errno.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <sys/wait.h>

      #define BUF_SIZE 1024

      int main(int argc, char *argv) {
      int t[2];
      int fd;

      if (argc != 4) {
      printf("Format: %s <archivo1> <archivo2> <archivo3>.n", argv[0]);
      exit(1);
      }

      pipe(t);

      if (fork() != 0) {
      wait(NULL);
      printf("Padren");

      if (!(fd = open(argv[3], O_WRONLY))) {
      printf("Error al abrir el archivo %s.n", argv[3]);
      exit(1);
      }

      dup2(t[0], STDIN_FILENO);
      dup2(fd, STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("nl", "nl", NULL);

      } else {
      if (fork() != 0) {
      wait(NULL);
      printf("Hijo 1n");
      dup2(t[0], STDIN_FILENO);
      dup2(t[1], STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("sort", "sort", NULL);

      } else {
      printf("Hijo 2n");
      dup2(t[1], STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("paste", "paste", argv[1], argv[2], NULL);
      }
      }

      return 0;
      }


      But once it reaches to the section where the sort command is executed, it continues using the standard input and not the pipe. Am I missing something? Where did I go wrong?










      share|improve this question







      New contributor




      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I am attempting to resolve the following problem:




      Write a program in C that executes the following commands the same way a command interpreter would: paste file1 file2 | sort | nl > file3. You must consider that file1, file2 and file 3 will be parameters given to your program in the command line.




      #include <stdlib.h>
      #include <stdio.h>
      #include <unistd.h>
      #include <errno.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <sys/wait.h>

      #define BUF_SIZE 1024

      int main(int argc, char *argv) {
      int t[2];
      int fd;

      if (argc != 4) {
      printf("Format: %s <archivo1> <archivo2> <archivo3>.n", argv[0]);
      exit(1);
      }

      pipe(t);

      if (fork() != 0) {
      wait(NULL);
      printf("Padren");

      if (!(fd = open(argv[3], O_WRONLY))) {
      printf("Error al abrir el archivo %s.n", argv[3]);
      exit(1);
      }

      dup2(t[0], STDIN_FILENO);
      dup2(fd, STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("nl", "nl", NULL);

      } else {
      if (fork() != 0) {
      wait(NULL);
      printf("Hijo 1n");
      dup2(t[0], STDIN_FILENO);
      dup2(t[1], STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("sort", "sort", NULL);

      } else {
      printf("Hijo 2n");
      dup2(t[1], STDOUT_FILENO);
      close(t[0]);
      close(t[1]);
      execlp("paste", "paste", argv[1], argv[2], NULL);
      }
      }

      return 0;
      }


      But once it reaches to the section where the sort command is executed, it continues using the standard input and not the pipe. Am I missing something? Where did I go wrong?







      c unix child-process






      share|improve this question







      New contributor




      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 17 hours ago









      Omari Celestine

      931




      931




      New contributor




      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Omari Celestine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      put on hold as off-topic by 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley 14 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley

      If this question can be reworded to fit the rules in the help center, please edit the question.




      put on hold as off-topic by 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley 14 hours ago


      This question appears to be off-topic. The users who voted to close gave this specific reason:


      • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, πάντα ῥεῖ, Sᴀᴍ Onᴇᴌᴀ, Heslacher, Nic Hartley

      If this question can be reworded to fit the rules in the help center, please edit the question.



























          active

          oldest

          votes






















          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes

          Popular posts from this blog

          Ellipse (mathématiques)

          Quarter-circle Tiles

          Mont Emei