Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. Код первой программы:
  2.  
  3. #include <iostream>
  4.  
  5. #include <pthread.h>
  6.  
  7. #include <unistd.h>
  8.  
  9. #include <stdio.h>
  10.  
  11. #include <iomanip>
  12.  
  13. #include <semaphore.h>
  14.  
  15. using namespace std;
  16.  
  17. sem_t sem;
  18.  
  19. void* func1(void* arg) {
  20.  
  21. bool* f1 = (bool*)arg;
  22.  
  23. while(*f1) {
  24.  
  25. sem_wait(&sem);
  26.  
  27. for(int i=0; i<3;i++){
  28.  
  29. cout << "1" << flush;
  30.  
  31. sleep(1);
  32.  
  33. }
  34.  
  35. sem_post(&sem);
  36.  
  37. sleep(1);
  38.  
  39. }
  40.  
  41. pthread_exit(0);
  42.  
  43. }
  44.  
  45. void* func2(void* arg) {
  46.  
  47. bool* f2 = (bool*)arg;
  48.  
  49. while(*f2) {
  50.  
  51. sem_wait(&sem);
  52.  
  53. for(int f=0; f<3;f++){
  54.  
  55. cout << "2" << flush;
  56.  
  57. sleep(1);
  58.  
  59. }
  60.  
  61. sem_post(&sem);
  62.  
  63. sleep(1);
  64.  
  65. }
  66.  
  67. pthread_exit(0);
  68.  
  69. }
  70.  
  71. int main() {
  72.  
  73. bool f1 = true, f2 = true;
  74.  
  75. void *status1, *status2;
  76.  
  77. pthread_t thread1;
  78.  
  79. pthread_t thread2;
  80.  
  81. sem_init(&sem, 0, 1);
  82.  
  83. pthread_create(&thread1, NULL, func1, &f1);
  84.  
  85. pthread_create(&thread2, NULL, func2, &f2);
  86.  
  87. getchar();
  88.  
  89. f1 = false;
  90.  
  91. f2 = false;
  92.  
  93. pthread_join(thread1, &status1);
  94.  
  95. pthread_join(thread2, &status2);
  96.  
  97. sem_destroy(&sem);
  98.  
  99. }
  100.  
  101. Код второй программы:
  102.  
  103. #include <iostream>
  104.  
  105. #include <pthread.h>
  106.  
  107. #include <unistd.h>
  108.  
  109. #include <stdio.h>
  110.  
  111. #include <iomanip>
  112.  
  113. #include <semaphore.h>
  114.  
  115. using namespace std;
  116.  
  117. sem_t sem;
  118.  
  119. void* func1(void* arg) {
  120.  
  121. bool* f1 = (bool*)arg;
  122.  
  123. while(*f1) {
  124.  
  125. int rc=sem_trywait(&sem);
  126.  
  127. if(rc==0){
  128.  
  129. for(int i=0; i<3;i++){
  130.  
  131. cout << "1" << flush;
  132.  
  133. sleep(1);
  134.  
  135. }
  136.  
  137. sem_post(&sem);
  138.  
  139. sleep(1);
  140.  
  141. }
  142.  
  143. }
  144.  
  145. pthread_exit(0);
  146.  
  147. }
  148.  
  149. void* func2(void* arg) {
  150.  
  151. bool* f2 = (bool*)arg;
  152.  
  153. while(*f2) {
  154.  
  155. int rc=sem_trywait(&sem);
  156.  
  157. if(rc==0){
  158.  
  159. for(int f=0; f<3;f++){
  160.  
  161. cout << "2" << flush;
  162.  
  163. sleep(1);
  164.  
  165. }
  166.  
  167. sem_post(&sem);
  168.  
  169. sleep(1);
  170.  
  171. }
  172.  
  173. }
  174.  
  175. pthread_exit(0);
  176.  
  177. }
  178.  
  179. int main() {
  180.  
  181. bool f1 = true, f2 = true;
  182.  
  183. void *status1, *status2;
  184.  
  185. pthread_t thread1;
  186.  
  187. pthread_t thread2;
  188.  
  189. sem_init(&sem, 0, 1);
  190.  
  191. pthread_create(&thread1, NULL, func1, &f1);
  192.  
  193. pthread_create(&thread2, NULL, func2, &f2);
  194.  
  195. getchar();
  196.  
  197. f1 = false;
  198.  
  199. f2 = false;
  200.  
  201. pthread_join(thread1, &status1);
  202.  
  203. pthread_join(thread2, &status2);
  204.  
  205. sem_destroy(&sem);
  206.  
  207. }
  208.  
  209. Код третьей программы:
  210.  
  211. #include <iostream>
  212.  
  213. #include <pthread.h>
  214.  
  215. #include <unistd.h>
  216.  
  217. #include <stdio.h>
  218.  
  219. #include <iomanip>
  220.  
  221. #include <semaphore.h>
  222.  
  223. using namespace std;
  224.  
  225. sem_t sem;
  226.  
  227. void* func1(void* arg) {
  228.  
  229. struct timespec tp;
  230.  
  231. bool* f1 = (bool*)arg;
  232.  
  233. while(*f1) {
  234.  
  235. clock_gettime(CLOCK_REALTIME, &tp);
  236.  
  237. tp.tv_sec+=1;
  238.  
  239. int rc=sem_timedwait(&sem,&tp);
  240.  
  241. if(rc==0){
  242.  
  243. for(int i=0; i<3;i++){
  244.  
  245. cout << "1" << flush;
  246.  
  247. sleep(1);
  248.  
  249. }
  250.  
  251. sem_post(&sem);
  252.  
  253. sleep(1);
  254.  
  255. }
  256.  
  257. }
  258.  
  259. pthread_exit(0);
  260.  
  261. }
  262.  
  263. void* func2(void* arg) {
  264.  
  265. struct timespec tp;
  266.  
  267. bool* f2 = (bool*)arg;
  268.  
  269. while(*f2) {
  270.  
  271. clock_gettime(CLOCK_REALTIME, &tp);
  272.  
  273. tp.tv_sec+=1;
  274.  
  275. int rc=sem_timedwait(&sem,&tp);
  276.  
  277. if(rc==0){
  278.  
  279. for(int f=0; f<3;f++){
  280.  
  281. cout << "2" << flush;
  282.  
  283. sleep(1);
  284.  
  285. }
  286.  
  287. sem_post(&sem);
  288.  
  289. sleep(1);
  290.  
  291. }
  292.  
  293. }
  294.  
  295. pthread_exit(0);
  296.  
  297. }
  298.  
  299. int main() {
  300.  
  301. bool f1 = true, f2 = true;
  302.  
  303. void *status1, *status2;
  304.  
  305. pthread_t thread1;
  306.  
  307. pthread_t thread2;
  308.  
  309. sem_init(&sem, 0, 1);
  310.  
  311. pthread_create(&thread1, NULL, func1, &f1);
  312.  
  313. pthread_create(&thread2, NULL, func2, &f2);
  314.  
  315. getchar();
  316.  
  317. f1 = false;
  318.  
  319. f2 = false;
  320.  
  321. pthread_join(thread1, &status1);
  322.  
  323. pthread_join(thread2, &status2);
  324.  
  325. sem_destroy(&sem);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement