Guest User

Untitled

a guest
Jan 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. void main()
  2. {
  3. int pipefd[2], n;
  4. char input = 0;
  5. char buffer[100] = {0};
  6. char outpipe[100] = {0};
  7.  
  8. if(pipe(pipefd) < 0) {
  9. printf("FAILED TO MAKE PIPESn");
  10. return;
  11. }
  12.  
  13. printf("Starting up, read fd = %d, write fd = %dn", pipefd[0],pipefd[1]);
  14.  
  15. do {
  16. //print menu options (send message, get message, get my fd,
  17. // set a fd to talk to, quit)
  18.  
  19. // if "send a message":
  20. {
  21. printf("What would you like to send?n");
  22. fgets(buffer, 100, stdin);
  23. write(pipefd[1], buffer, strlen(buffer));
  24. }
  25. //else if "read a message":
  26. {
  27. if(open(outpipe, 0) < 0)
  28. printf("Couldn't open the pipe!n");
  29. else {
  30. n = read(outpipe, buffer, 100);
  31. printf("I got a read of %d bytesnIt was %sn",n, buffer);
  32. close(outpipe);
  33. }
  34. }
  35. //else if "get my file descriptor":
  36. printf("My fd tag is: /proc/%d/fd/%dn", (int)getpid(), pipefd[0]);
  37. //else if "set a file descriptor to talk to":
  38. {
  39. printf("What is the pipe's file descriptor?n");
  40. fgets(outpipe, 100, stdin);
  41. n = strlen(outpipe) - 1;
  42. outpipe[n] = '';
  43. }
  44. } while (input != 'Q');
  45. return;
  46. }
  47.  
  48. lr-x------ 1 mike users 64 Sep 26 23:31 3 -> pipe:[33443]
  49. l-wx------ 1 mike users 64 Sep 26 23:31 4 -> pipe:[33443]
  50.  
  51. //terminal 1
  52. Pick an option:
  53. 3
  54. My fd tag is: /proc/8956/fd/3
  55.  
  56. //terminal 2
  57. Pick an option:
  58. 4
  59. What is the pipe's file descriptor?
  60. /proc/8956/fd/3
  61.  
  62. Pick an option:
  63. 1
  64. What would you like to send?
  65. hello
  66.  
  67. //terminal 1
  68. Pick an option:
  69. 2
  70. I got a read of -1 bytes
  71. It was
  72.  
  73. #include <stdio.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <unistd.h>
  77.  
  78. int main(void)
  79. {
  80. int pipefds[2];
  81. char input[128];
  82. char output[128];
  83. ssize_t nread;
  84.  
  85. if (pipe(pipefds) == -1)
  86. {
  87. perror("Could not create pipe");
  88. return EXIT_FAILURE;
  89. }
  90.  
  91. printf("Enter input: ");
  92. if (fgets(input, sizeof(input), stdin) == NULL)
  93. {
  94. perror("Could not read input");
  95. return EXIT_FAILURE;
  96. }
  97.  
  98. /* "Remove" newline from input */
  99. if (input[strlen(input) - 1] == 'n')
  100. input[strlen(input) - 1] = '';
  101.  
  102. /* Now write the received input to the pipe */
  103. if (write(pipefds[1], input, strlen(input) + 1) == -1)
  104. {
  105. perror("Could not write to pipe");
  106. return EXIT_FAILURE;
  107. }
  108.  
  109. /* Now read from the pipe */
  110. if ((nread = read(pipefds[0], output, sizeof(output))) == -1)
  111. {
  112. perror("Could not reaf from pipe");
  113. return EXIT_FAILURE;
  114. }
  115.  
  116. /* We don't need to terminate as we send with the '' */
  117.  
  118. printf("Received: "%s"n", output);
  119.  
  120. return EXIT_SUCCESS;
  121. }
  122.  
  123. ./ipctest.c: In function ‘main’:
  124.  
  125. ./ipctest.c:32:9: warning: passing argument 1 of ‘read’ makes integer from pointer without a cast [enabled by default]
  126. /usr/include/unistd.h:361:16: note: expected ‘int’ but argument is of type ‘char *’
  127.  
  128. ./ipctest.c:34:9: warning: passing argument 1 of ‘close’ makes integer from pointer without a cast [enabled by default]
  129. /usr/include/unistd.h:354:12: note: expected ‘int’ but argument is of type ‘char *’
Add Comment
Please, Sign In to add comment