fork download
  1. #include<dirent.h>
  2. #include<string.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<unistd.h>
  6.  
  7. int main()
  8. {
  9. int canal_son[2];
  10. int canal_father[2];
  11. pipe(canal_father);
  12. pipe(canal_son);
  13.  
  14. if (fork()==0)
  15. {
  16. close(canal_father[1]);
  17. close(canal_son[0]);
  18.  
  19. printf("Son %d\n",getpid());
  20. int j;
  21. for(j=0;j<5;j++)
  22. {
  23. char mesaj_father[20] = {0};
  24. if (read(canal_father[0],mesaj_father,sizeof(mesaj_father)) > 0)
  25. {
  26. printf("The message from father is: %s \n",mesaj_father);
  27. write(canal_son[1],"son",3);
  28. }
  29. }
  30. }
  31. else
  32. {
  33. close(canal_father[0]);
  34. close(canal_son[1]);
  35.  
  36. int i=5;
  37. for (i=0;i<5;i++)
  38. {
  39. char mesaj_son[20] = {0};
  40. write(canal_father[1],"mesas",5);
  41. if (read(canal_son[0],mesaj_son,sizeof(mesaj_son)) > 0)
  42. printf("we are in father:%s\n",mesaj_son);
  43. }
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
Son 14208
The message from father is: mesas 
The message from father is: mesas 
The message from father is: mesas 
The message from father is: mesas 
The message from father is: mesas 
we are in father:son
we are in father:son
we are in father:son
we are in father:son
we are in father:son