Advertisement
fr1sk

Untitled

Jun 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/wait.h>
  7. #include <sys/types.h>
  8. #include <sys/uio.h>
  9. #include <unistd.h>
  10.  
  11. void checker(bool stmt, const char *usrmsg, const char *file, const char *func, const int line){
  12. if(!stmt){
  13. perror(usrmsg);
  14. fprintf(stderr, "%s %s %d\n", file, func, line);
  15. exit(EXIT_FAILURE);
  16. }
  17. }
  18.  
  19. #define osAssert(stmt, usrmsg) checker(stmt,usrmsg,__FILE__, __func__, __LINE__);
  20.  
  21. #define PIPE_RD (0)
  22. #define PIPE_WR (1)
  23. #define MAX_LINE (128)
  24.  
  25. int main(){
  26. int pipeFD[2];
  27. osAssert(-1 != pipe(pipeFD), "pipe creation failed");
  28.  
  29. pid_t child = fork();
  30. osAssert(-1 != child, "fork failed");
  31.  
  32. if(child>0){
  33. //parrent
  34. close(pipeFD[PIPE_RD]);
  35. printf("Parrent: ");
  36.  
  37. char *lineRead = NULL;
  38. size_t lineReadLen = 0;
  39. osAssert(-1 != getline(&lineRead, &lineReadLen, stdin), "user read failed");
  40.  
  41. osAssert(-1 != write(pipeFD[PIPE_WR], lineRead, lineReadLen), "write failed");
  42. osAssert(-1 != wait(NULL), "wait failed");
  43. close(pipeFD[PIPE_WR]);
  44.  
  45.  
  46. } else {
  47. //kid
  48. close(pipeFD[PIPE_WR]);
  49.  
  50. char buf[MAX_LINE];
  51. osAssert(-1 != read(pipeFD[PIPE_RD], buf, sizeof buf), "read failed");
  52. close(pipeFD[PIPE_RD]);
  53. printf("Child: %s\n", buf);
  54. exit(0);
  55. }
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement