Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/wait.h>
  3. #include <sys/types.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <stdint.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <string.h>
  14.  
  15.  
  16. int main(int argc, char *argv[]) {
  17. double x;
  18. sscanf(argv[1], "%lf", &x);
  19. int n;
  20. sscanf(argv[2], "%d", &n);
  21. n++;
  22. double dx;
  23. sscanf(argv[3], "%lf", &dx);
  24. int fd1[2];
  25. int fd2[2];
  26. pipe(fd1);
  27. pipe(fd2);
  28.  
  29. if (!fork()) {
  30. if (!fork()) {
  31. close(fd1[0]);
  32. close(fd2[1]);
  33. dup2(fd2[0], 0);
  34. close(fd2[0]);
  35. double res = 0;
  36. double cur = 0;
  37. for (int i = 0; i < n; ++i) {
  38. dprintf(fd1[1], "%.10g\n", sin(x + i * dx));
  39. fflush(NULL);
  40. scanf("%lf", &cur);
  41. res += cur * cur;
  42. }
  43. close(fd1[1]);
  44. printf("1 %.10g\n", res);
  45. fflush(NULL);
  46. exit(0);
  47. }
  48. close(fd1[0]);
  49. close(fd1[1]);
  50. close(fd2[0]);
  51. close(fd2[1]);
  52. wait(NULL);
  53. exit(0);
  54. }
  55.  
  56. if (!fork()) {
  57. if (!fork()) {
  58. close(fd1[1]);
  59. close(fd2[0]);
  60. dup2(fd1[0], 0);
  61. close(fd1[0]);
  62. double res = 0;
  63. double cur = 0;
  64. for (int i = 0; i < n; ++i) {
  65. dprintf(fd2[1], "%.10g\n", cos(x + i * dx));
  66. fflush(NULL);
  67. scanf("%lf", &cur);
  68. if (cur < 0) {
  69. cur = - cur;
  70. }
  71. res += cur;
  72. }
  73. close(fd2[1]);
  74. printf("2 %.10g\n", res);
  75. fflush(NULL);
  76. exit(0);
  77. }
  78. close(fd1[0]);
  79. close(fd1[1]);
  80. close(fd2[0]);
  81. close(fd2[1]);
  82. wait(NULL);
  83. exit(0);
  84. }
  85.  
  86. close(fd1[0]);
  87. close(fd1[1]);
  88. close(fd2[0]);
  89. close(fd2[1]);
  90. wait(NULL);
  91. wait(NULL);
  92. printf("0 0\n");
  93. fflush(NULL);
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement