Advertisement
Guest User

Untitled

a guest
Jul 8th, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <math.h>
  5. #include <iostream>
  6. #include <assert.h>
  7. #include <string>
  8. #include <stdlib.h>
  9. #include <map>
  10. #include <stdio.h>
  11. #include <curl/curl.h>
  12. #include <vector>
  13. #include <dirent.h>
  14. #include <sys/stat.h>
  15.  
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20.  
  21. using namespace std;
  22.  
  23.  
  24. int main()
  25. {
  26. int pid;
  27. int pc[2]; /* Parent to child pipe */
  28. int cp[2]; /* Child to parent pipe */
  29. char ch;
  30. int incount = 0, outcount = 0;
  31.  
  32. /* Make pipes */
  33. if( pipe(pc) < 0)
  34. {
  35. perror("Can't make pipe");
  36. exit(1);
  37. }
  38. if( pipe(cp) < 0)
  39. {
  40. perror("Can't make pipe");
  41. exit(1);
  42. }
  43.  
  44.  
  45. /* Create a child to run command. */
  46. switch( pid = fork() )
  47. {
  48. case -1:
  49. perror("Can't fork");
  50. exit(1);
  51. case 0:
  52. /* Child. */
  53. close(STDOUT_FILENO); /* Close current stdout. */
  54. dup2(cp[1], STDOUT_FILENO);
  55.  
  56. close(STDIN_FILENO);
  57. dup2(pc[0], STDIN_FILENO);
  58.  
  59. close( pc[1]);
  60. close( cp[0]);
  61. execlp("cat", "cat", NULL);
  62. exit(1);
  63. default:
  64. /* Parent. */
  65. /* Close what we don't need. */
  66. printf("Input to child:\n");
  67.  
  68. string theinput("Hey there baby");
  69. write(pc[1], theinput.c_str(), theinput.size());
  70. close(pc[1]);
  71.  
  72. cout << "The input : " << theinput << endl;
  73.  
  74.  
  75. printf("\nOutput from child:\n");
  76. close(cp[1]);
  77. while( read(cp[0], &ch, 1) == 1)
  78. {
  79. write(1, &ch, 1);
  80. outcount++;
  81. }
  82.  
  83. exit(0);
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement