fork download
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<sys/types.h>
  5. #include<sys/wait.h>
  6. #include<string.h>
  7.  
  8. #define SIZE 25
  9.  
  10. int main(){
  11. char read_msg[SIZE];
  12. char write_msg[SIZE];
  13. int fd_p[2];
  14. int fd_c[2];
  15. pid_t pid;
  16. int status;
  17.  
  18.  
  19. if(pipe(fd_p) == -1 || pipe(fd_c) == -1)
  20. return 0;
  21.  
  22. pid=fork();
  23.  
  24.  
  25. if(pid == -1){
  26. printf("The child is not created\n");
  27. return 0;}
  28.  
  29. else if (pid > 0){
  30.  
  31.  
  32. close(fd_p[0]);
  33. close(fd_c[1]);
  34. printf("Write the message from parents end");
  35. scanf("%s",write_msg);
  36. write(fd_p[1],write_msg,strlen(write_msg)+1);
  37. close(fd_p[1]);
  38.  
  39.  
  40. read(fd_c[0],read_msg,SIZE);
  41. printf("The message is %s read by %d\n",read_msg,pid);
  42. close(fd_c[0]);
  43.  
  44. }
  45.  
  46. else{
  47.  
  48. close(fd_p[1]);
  49. close(fd_c[0]);
  50. read(fd_p[0],read_msg,SIZE);
  51. printf("The message is %s read by %d\n",read_msg,pid);
  52. close(fd_p[0]);
  53.  
  54.  
  55. printf("Write the message from child end");
  56. scanf("%s",write_msg);
  57. close(fd_c[0]);
  58. write(fd_c[1],write_msg,strlen(write_msg)+1);
  59. close(fd_c[1]);
  60. }
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
The message is  read by 0
Write the message from child endWrite the message from parents endThe message is  read by 530250