Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. Код первой программы:
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <iomanip>
  6.  
  7. #include <iostream>
  8.  
  9. #include <pthread.h>
  10.  
  11. #include <unistd.h>
  12.  
  13. #include <fcntl.h>
  14.  
  15. using namespace std;
  16.  
  17. int f[2];
  18.  
  19. size_t c=4;
  20.  
  21. void* func1(void* a){
  22.  
  23. bool*flag1=(bool*)a;
  24.  
  25. int b=123;
  26.  
  27. while(*flag1 ){
  28.  
  29. ssize_t rc=write(f[1] , &b , c);
  30.  
  31. if(rc==-1)
  32.  
  33. {perror(" ");}
  34.  
  35. sleep(1);
  36.  
  37. }
  38.  
  39. pthread_exit((void*)3);
  40.  
  41. }
  42.  
  43. void* func2(void* a){
  44.  
  45. bool*flag2=(bool*)a;
  46.  
  47. int b;
  48.  
  49. while(*flag2){
  50.  
  51. b=0;
  52.  
  53. ssize_t rc=read(f[0] , &b , c);
  54.  
  55. if(rc==-1)
  56.  
  57. {perror(" ");}
  58.  
  59. else{
  60.  
  61. cout<<b<<flush;
  62.  
  63. }
  64.  
  65. sleep(1);
  66.  
  67. }
  68.  
  69. pthread_exit((void*)4);
  70.  
  71. }
  72.  
  73. int main(){
  74.  
  75. bool flag1=true;
  76.  
  77. bool flag2=true;
  78.  
  79. pthread_t thread1,thread2;
  80.  
  81. pipe2(f, O_NONBLOCK);
  82.  
  83. pthread_create(&thread1, NULL, func1, &flag1);
  84.  
  85. pthread_create(&thread2, NULL, func2, &flag2);
  86.  
  87. getchar();
  88.  
  89. flag1=false;
  90.  
  91. flag2=false;
  92.  
  93. void* st1;
  94.  
  95. void* st2;
  96.  
  97. pthread_join(thread1, &st1);
  98.  
  99. pthread_join(thread2, &st2);
  100.  
  101. close(f[0]);
  102.  
  103. close(f[1]);
  104.  
  105. cout<<"status 1,2:"<<((int*)st1)<<" "<<((int*)st2);
  106.  
  107. return(0);
  108.  
  109. }
  110.  
  111. Код второй программы:
  112.  
  113. #include <stdio.h>
  114.  
  115. #include <iomanip>
  116.  
  117. #include <iostream>
  118.  
  119. #include <pthread.h>
  120.  
  121. #include <unistd.h>
  122.  
  123. #include <fcntl.h>
  124.  
  125. using namespace std;
  126.  
  127. int f[2];
  128.  
  129. size_t c=4;
  130.  
  131. void* func1(void* a){
  132.  
  133. bool*flag1=(bool*)a;
  134.  
  135. int b=123;
  136.  
  137. while(*flag1 ){
  138.  
  139. ssize_t rc=write(f[1] , &b , c);
  140.  
  141. if(rc==-1)
  142.  
  143. {perror(" ");};
  144.  
  145. sleep(1);
  146.  
  147. }
  148.  
  149. pthread_exit((void*)3);
  150.  
  151. }
  152.  
  153. void* func2(void* a){
  154.  
  155. bool*flag2=(bool*)a;
  156.  
  157. int b;
  158.  
  159. while(*flag2){
  160.  
  161. b=0;
  162.  
  163. ssize_t rc=read(f[0] , &b , c);
  164.  
  165. if(rc==-1)
  166.  
  167. {perror(" ");}
  168.  
  169. else{
  170.  
  171. cout<<b<<flush;
  172.  
  173. };
  174.  
  175. sleep(1);
  176.  
  177. }
  178.  
  179. pthread_exit((void*)4);
  180.  
  181. }
  182.  
  183. int main(){
  184.  
  185. bool flag1=true;
  186.  
  187. bool flag2=true;
  188.  
  189. pthread_t thread1,thread2;
  190.  
  191. pipe(f);
  192.  
  193. int op= fcntl(f[0], F_GETFL);
  194.  
  195. fcntl(f[0], F_SETFL,op|O_NONBLOCK);
  196.  
  197. pthread_create(&thread1, NULL, func1, &flag1);
  198.  
  199. pthread_create(&thread2, NULL, func2, &flag2);
  200.  
  201. getchar();
  202.  
  203. flag1=false;
  204.  
  205. flag2=false;
  206.  
  207. void* st1;
  208.  
  209. void* st2;
  210.  
  211. pthread_join(thread1, &st1);
  212.  
  213. pthread_join(thread2, &st2);
  214.  
  215. close(f[0]);
  216.  
  217. close(f[1]);
  218.  
  219. cout<<"status 1,2:"<<((int*)st1)<<" "<<((int*)st2);
  220.  
  221. return(0);
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement