Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <mutex>
  3. #include <stdio.h>
  4. #include <semaphore.h>
  5. #include <random>
  6. #include <unistd.h>
  7.  
  8. using namespace std;
  9.  
  10. sem_t f, e;
  11. std::mutex counterMutex, bufferMutex, indexMutex;
  12.  
  13. int MAX_SLEEPING_TIME_COLLECTOR = 20000000;
  14. int MAX_SLEEPING_TIME_MONITOR = 1000;
  15. int MAX_SLEEPING_TIME_COUNTER = 10;
  16.  
  17. int NUMBER_OF_THREADS = 1000;
  18. int NUMBER_OF_MONITORS = 5;
  19. int NUMBER_OF_COLLECTORS = 2;
  20.  
  21. int MAXIMUM_SIZE = 100;
  22. int counter = 0;
  23.  
  24. int monitorIndex = 0;
  25. int collectorIndex = 0;
  26.  
  27. int buffer [MAXIMUM_SIZE] = {0};
  28.  
  29. void *count(void *args);
  30. void *monitor(void *args);
  31. void *collect(void *args);
  32. void *helpCounter(void *args);
  33. void *helpMonitor(void *args);
  34. void *helpCollector(void *args);
  35.  
  36. void *helpCounter(void *args)
  37. {
  38. pthread_t counter[NUMBER_OF_THREADS];
  39. for(int i = 0; i < NUMBER_OF_THREADS; i++)
  40. {
  41. pthread_create(&counter[i],NULL,count,(void*)&(i));
  42. pthread_join(counter[i],NULL);
  43. usleep(rand() % MAX_SLEEPING_TIME_COUNTER);
  44. }
  45. return NULL;
  46. }
  47.  
  48. void *helpMonitor(void *args)
  49. {
  50. pthread_t mmonitorThread;
  51. for(int i = 0; i < NUMBER_OF_MONITORS; i++)
  52. {
  53. pthread_create(&mmonitorThread,NULL,monitor,NULL);
  54. pthread_join(mmonitorThread,NULL);
  55. usleep(rand() % 100 + MAX_SLEEPING_TIME_MONITOR);
  56.  
  57. }
  58. return NULL;
  59. }
  60.  
  61. void *helpCollector(void *args)
  62. {
  63. pthread_t collectorThread;
  64. pthread_create(&collectorThread,NULL,collect,NULL);
  65. pthread_join(collectorThread,NULL);
  66. return NULL;
  67. }
  68.  
  69. void *collect(void* args)
  70. {
  71. if(buffer[collectorIndex] == -1){
  72. cout << "NOTHING TO COLLECT";
  73. return;
  74. }
  75. std::cout << "Monitor thread : reading a count value of = " << buffer[collectorIndex] << "from index = " << collectorIndex % MAXIMUM_SIZE << endl;
  76. buffer[collectorIndex] = -1;
  77. collectorIndex++;
  78. return NULL;
  79. }
  80.  
  81. void *monitor(void *args)
  82. {
  83. counterMutex.lock();
  84. std::cout << "Monitor thread : waiting to read a counter" << endl;
  85. std::cout << "Monitor thread : reading a count value of = " << counter << endl;
  86.  
  87. bufferMutex.lock();
  88. if(monitorIndex % MAXIMUM_SIZE == -1){
  89. buffer[monitorIndex % MAXIMUM_SIZE] = counter;
  90. monitorIndex++;
  91. }
  92. else{
  93. cout << "Buffer is FULL!" << endl;
  94. }
  95. bufferMutex.unlock();
  96.  
  97. counter = 0;
  98. counterMutex.unlock();
  99. return NULL;
  100. }
  101.  
  102. void *count(void *args)
  103. {
  104. /*
  105. indexMutex.lock();
  106.  
  107. std::cout << "Counter thread " << *i << " : received a message" << endl;
  108. std::cout << "Counter thread " << *i << " : waiting to write" << endl;
  109. indexMutex.unlock();
  110. */
  111. counterMutex.lock();
  112. int* i = (int*) args;
  113. std::cout << "Counter thread " << *i << " : received a message" << endl;
  114. std::cout << "Counter thread " << *i << " : waiting to write" << endl;
  115. counter++;
  116. std::cout << "Counter thread " << *i << " : now adding to counter, counter value = "<< counter << endl;
  117. counterMutex.unlock();
  118. return NULL;
  119. }
  120.  
  121. int main()
  122. {
  123. //std::cout << "Welcome to" << args[0] << " Synchronization" << endl;
  124. // NUMBER_OF_THREADS = args[1];
  125. freopen("yaraDeadOutput.txt","w",stdout);
  126. srand(time(NULL));
  127.  
  128. for(int i = 0; i < MAXIMUM_SIZE; i++)
  129. buffer[i] = -1;
  130.  
  131. pthread_t counter,mmonitor,collector;
  132.  
  133. pthread_create(&counter,NULL,helpCounter,NULL);
  134. pthread_create(&mmonitor,NULL,helpMonitor,NULL);
  135. pthread_create(&collector,NULL,helpCollector,NULL);
  136.  
  137. pthread_join(counter,NULL);
  138. pthread_join(mmonitor,NULL);
  139. pthread_join(collector,NULL);
  140.  
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement