fork download
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6.  
  7. int main()
  8. {
  9. int pipe_fd[2];
  10. pid_t pid1 ;
  11.  
  12. if (pipe(pipe_fd)==-1)
  13. {
  14. printf("error");
  15. return 1;
  16.  
  17. }
  18.  
  19. pid1 = fork();
  20. if (pid1==-1)
  21. {
  22. printf("error creating child process");
  23. }
  24.  
  25. else if(pid1==0)
  26. {
  27. close(pipe_fd[0]);
  28. int x;
  29. printf("enter a input number : ");
  30. scanf("%d",&x);
  31. write(pipe_fd[1] , &x , sizeof(int));
  32. close(pipe_fd[1]);
  33. }
  34.  
  35. else
  36. {
  37. close(pipe_fd[1]);
  38. int y;
  39.  
  40. read(pipe_fd[0] , &y , sizeof(int));
  41. close(pipe_fd[0]);
  42. printf("copied value from child process : %d " , y);
  43.  
  44. //wait(NULL);
  45.  
  46. }
  47.  
  48.  
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
enter a input number : copied value from child process : 0