Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <dirent.h>
  11. #include <pwd.h>
  12. #include <ctype.h>
  13. #include <sys/wait.h>
  14. #include <unistd.h>
  15. #include <time.h>
  16. #include <math.h>
  17.  
  18. int putere(int a, int b) //a la put b
  19. {
  20. for(int i=1; i<=b; i++)
  21. a=a*a;
  22. return a;
  23. }
  24. int main(int argc, char* argv[])
  25. {
  26. int p1[2],p2[2];
  27. if(pipe(p1)==-1)
  28. {
  29. printf("eroare la pipe 1");
  30. exit(0);
  31. }
  32. if(pipe(p2)==-1)
  33. {
  34. printf("eroare la pipe 2");
  35. exit(0);
  36. }
  37. pid_t fiu1=fork()
  38. {
  39. if(fiu1==-1)
  40. {
  41. printf("eroare la primul fiu");
  42. exit(0);
  43. }
  44. else if(fiu1==0)
  45. {
  46. close(p1[1]);
  47. close(p2[0]);
  48. close(p2[1]);
  49. int a1, b1;
  50. read(p1[0],&a1,sizeof(int));
  51. read(p1[0],&b1,sizeof(int));
  52. int rezultat=putere(a1,b1);
  53. close(p1[0]);
  54. }
  55. else
  56. {
  57. pid_t fiu2=fork()
  58. if(fiu2==-1)
  59. {
  60. printf("eroare la fiul 2");
  61. exit(0);
  62. }
  63. }
  64. else if(fiu2==0)
  65. {
  66. close(p1[0]);
  67. close(p2[1]);
  68. close(p1[1]);
  69. int a2,b2;
  70. read(p2[0],&a2,sizeof(int));
  71. read(p2[0],&b2,sizeof(int));
  72. int rezultat2=putere(a2,b2);
  73. close(p2[0]);
  74. }
  75. else
  76. {
  77. close(p1[0]);
  78. close(p2[0]);
  79. srand(time(NULL));
  80. int nr1=rand()%10;
  81. srand(time(NULL));
  82. int nr2=rand()%10;
  83. write(p1[1],&nr1,sizeof(int));
  84. write(p2[1],&nr2,sizeof(int));
  85. write(p1[1],&nr2,sizeof(int));
  86. write(p2[1],&nr1,sizeof(int));
  87. close(p1[1]);
  88. close(p2[1]);
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement