Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <unistd.h>
  5. #include <pthread.h>
  6.  
  7. #include <time.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <stdbool.h>
  15. #include <sys/stat.h>
  16. #include <sys/file.h>
  17. #include <semaphore.h>
  18. #include <sys/mman.h>
  19.  
  20. //using namespace std;
  21.  
  22. struct thread_arg{
  23.  
  24. std::vector<int>freelist;
  25.  
  26. std::vector<int>list1;
  27.  
  28. std::vector<int>list2;
  29. };
  30.  
  31. sem_t mutex1, mutex2, mutex3, freemax, freecnt, cnt1, cnt2;
  32.  
  33. struct thread_arg Args;
  34.  
  35.  
  36. void *thread1_function(void *Args){
  37.  
  38. struct thread_arg *stuff = (struct thread_arg*)Args;
  39.  
  40. int node;
  41.  
  42. while (1){
  43.  
  44. std::cout << "Thread ONE started\n";
  45.  
  46. // Lock our mutexes freemax, freecnt, mutex1
  47. sem_wait(&freecnt);
  48. sem_wait(&mutex1);
  49.  
  50. // Unlink
  51. node = stuff->freelist.back();
  52. stuff->freelist.pop_back();
  53.  
  54. // Unlock mutex 1
  55. sem_post(&mutex1);
  56.  
  57. // Do stuff/produce info/ use 'node' value
  58. std::cout << "Thread1 produce info after unlinking" << "\n";
  59.  
  60. // Lock mutex2 after producing info
  61. sem_wait(&mutex2);
  62.  
  63. // Link
  64. stuff->list1.push_back(node);
  65.  
  66. std::cout << "lock mutex2 and link list1\n";
  67.  
  68. // Free mutex2 and cnt1
  69. sem_post(&mutex2);
  70. sem_post(&cnt1);
  71.  
  72.  
  73. }
  74.  
  75. return NULL;
  76.  
  77. }
  78.  
  79.  
  80. void *thread2_function(void *Args){
  81.  
  82. struct thread_arg *stuff = (struct thread_arg*)Args;
  83.  
  84. int x,y;
  85.  
  86. while (1){
  87.  
  88. std::cout << "Thread TWO started\n";
  89.  
  90. // Lock our cnt1, mutex2
  91. sem_wait(&cnt1);
  92. sem_wait(&mutex2);
  93.  
  94. // Unlink list1
  95. x = stuff->list1.back();
  96. stuff->list1.pop_back();
  97.  
  98. // Unlock mutex2, lock freecnt
  99. sem_post(&mutex2);
  100. sem_wait(&freecnt);
  101.  
  102. // Lock mutex 1
  103. sem_wait(&mutex1);
  104.  
  105. // Unlink freelist
  106. y = stuff->freelist.back();
  107. stuff->freelist.pop_back();
  108.  
  109. // Unlock mutex1, freemax
  110. sem_post(&mutex1);
  111. sem_post(&freemax);
  112.  
  113. // Produce info/use block
  114. y= x + y;
  115.  
  116. // Lock mutex1
  117. sem_wait(&mutex1);
  118.  
  119. // Link x, freelist
  120. stuff->freelist.push_back(x);
  121.  
  122. // Unlock mutex1, freecnt
  123. sem_post(&mutex1);
  124. sem_post(&freecnt);
  125.  
  126. // Lock mutex3
  127. sem_wait(&mutex3);
  128.  
  129. // Link y, list2
  130. stuff->list2.push_back(y);
  131.  
  132. // Unlock mutex3, cnt2
  133. sem_post(&mutex3);
  134. sem_post(&cnt2);
  135.  
  136.  
  137. }
  138.  
  139. return NULL;
  140.  
  141. }
  142.  
  143.  
  144. void *thread3_function(void *Args){
  145.  
  146. struct thread_arg *stuff = (struct thread_arg*)Args;
  147.  
  148. int node;
  149.  
  150. while(1){
  151.  
  152. std::cout << "Thread THREE started\n";
  153.  
  154. // Lock cnt2, mutex3
  155. sem_wait(&cnt2);
  156. sem_wait(&mutex3);
  157.  
  158. // Unlink
  159. node = stuff->list2.back();
  160. stuff->list2.pop_back();
  161.  
  162. // Unlock mutex3
  163. sem_post(&mutex3);
  164.  
  165. //consume info in block with node
  166. std::cout << "Consuming info in block with var node\n";
  167.  
  168. // Lock mutex1
  169. sem_wait(&mutex1);
  170.  
  171. // Link freelist
  172. stuff->freelist.push_back(node);
  173.  
  174. // Unlock mutex1, freecnt
  175. sem_post(&mutex1);
  176. sem_post(&freecnt);
  177. }
  178.  
  179. return NULL;
  180.  
  181. }
  182.  
  183.  
  184.  
  185. int main(){
  186.  
  187. sem_init(&mutex1, 1, 1);
  188. sem_init(&mutex2, 1, 1);
  189. sem_init(&mutex3, 1, 1);
  190.  
  191.  
  192. Args.freelist.assign(10,0);
  193.  
  194. sem_init(&freecnt, 1, Args.freelist.size());
  195. sem_init(&freemax, 1, Args.freelist.size());
  196.  
  197. sem_init(&cnt1, 1, 0);
  198. sem_init(&cnt2, 1, 0);
  199.  
  200.  
  201.  
  202. pthread_t thread1,thread2,thread3;
  203.  
  204. int t1,t2,t3;
  205.  
  206. pthread_create(&thread1, NULL, thread1_function, &Args);
  207.  
  208. pthread_create(&thread2, NULL, thread2_function, &Args);
  209.  
  210. pthread_create(&thread3, NULL, thread3_function, &Args);
  211.  
  212.  
  213.  
  214. pthread_join(thread1,NULL);
  215. pthread_join(thread2,NULL);
  216. pthread_join(thread3,NULL);
  217.  
  218. return 0;
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement