Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. int main(int argc, char* argv[]){
  2.  
  3.  
  4. char* argument1[]={argv[1], NULL};
  5. char* argument2[]={argv[2], NULL};
  6. int fd[2];
  7. int d;
  8. pid_t pid;
  9. char buffer[30];
  10.  
  11. if(argc < 3){
  12. printf("No parameter");
  13. return 1;
  14. }
  15.  
  16. if(pipe(fd)==-1){
  17. perror("pipe failed");
  18. exit(1);
  19. }
  20.  
  21. else{
  22.  
  23. pid=fork();
  24.  
  25. if(pid==0){
  26. /*child process*/
  27. close(1);
  28. dup(fd[1]);
  29. close(fd[0]);
  30.  
  31. //close(fd[1]);
  32.  
  33. execvp(argument1[0], argument1);
  34.  
  35. }
  36.  
  37. else if(pid>0){
  38. /*Parent process*/
  39. close(0);
  40. dup(fd[0]);
  41. close(fd[1]);
  42.  
  43. //close(fd[0]);
  44.  
  45. execvp(argument2[0], argument2);
  46.  
  47.  
  48. }
  49.  
  50. }
  51.  
  52.  
  53. return 0;
  54.  
  55. }
  56.  
  57. int m_pipe(char *cmd1, char *cmd2)
  58. {
  59. int fd[2];
  60.  
  61. if (pipe(fd) == -1)
  62. {
  63. perror("Pipe failed ");
  64. return (-1);
  65. }
  66. if (fork() == 0)
  67. {
  68. /*Child process*/
  69. dup2(fd[0], 0);
  70. close(fd[1]);
  71. m_exec(cmd2);
  72. }
  73. else
  74. {
  75. /*Parent process*/
  76. dup2(fd[1], 1);
  77. close(fd[0]);
  78. m_exec(cmd1);
  79. }
  80. return (0);
  81. }
  82.  
  83. int main(int argc, char* argv[])
  84. {
  85. if(argc != 3)
  86. {
  87. write(2, "Usage ./a.out cmd1 cmd2n", strlen("Usage ./a.out cmd1 cmd2n"));
  88. return EXIT_FAILURE;
  89. }
  90. if (m_pipe(argv[1], argv[2]) == -1)
  91. return EXIT_FAILURE;
  92. return EXIT_SUCCESS;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement