Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <semaphore.h>
  4. #include <pthread.h>
  5. #include <iostream>
  6. #include <queue>
  7. #include <pthread.h>
  8. #include <time.h>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. struct car {
  14. int CarID;
  15. char direction;
  16. string arrival_time;
  17. string start_time;
  18. string end_time;
  19. };
  20.  
  21. //returns a string with Hours:minute:seconds
  22. string getTime()
  23. {
  24. time_t rawtime;
  25. struct tm *info;
  26. char buffer[80];
  27.  
  28. time( &rawtime );
  29.  
  30. info = localtime( &rawtime );
  31.  
  32. strftime(buffer,80,"%I:%M:%S", info);
  33. string output(buffer);
  34. return output;
  35. }
  36.  
  37. int pthread_sleep(int seconds)
  38. {
  39. pthread_mutex_t mutex;
  40. pthread_cond_t conditionvar;
  41. struct timespec timetoexpire;
  42. if (pthread_mutex_init(&mutex, NULL))
  43. {
  44. return -1;
  45. }
  46. if (pthread_cond_init(&conditionvar, NULL))
  47. {
  48. return -1;
  49. }
  50. timetoexpire.tv_sec = (unsigned int)time(NULL) + seconds + 1;
  51. timetoexpire.tv_nsec = 0;
  52. return pthread_cond_timedwait(&conditionvar, &mutex, &timetoexpire);
  53. }
  54.  
  55. int carCounter = 0;
  56. queue<car> northCars, southCars;
  57. sem_t countSouth, countNorth;
  58. pthread_mutex_t road;
  59. bool flagPerson = false;
  60. ofstream carFile;
  61. ofstream flagFile;
  62.  
  63.  
  64. void printToCar(car& c)
  65. {
  66. carFile << c.CarID << "\t" << c.direction << "\t\t" << c.arrival_time << "\t" << c.start_time << "\t" << c.end_time << endl;
  67. }
  68.  
  69. void printToFlag(string status)
  70. {
  71. flagFile << getTime() << "\t" << status << endl;
  72. }
  73.  
  74. void* southGenerator(void* args) {
  75. while(true)
  76. {
  77.  
  78. do {
  79. car c;
  80. c.CarID = carCounter++;
  81. c.direction = 'S';
  82. c.arrival_time = getTime();
  83. c.start_time = "";
  84. c.end_time = "";
  85.  
  86. southCars.push(c);
  87. sem_post(&countSouth);
  88. } while (rand() % 100 + 1 <= 80);
  89. pthread_sleep(20);
  90. }
  91. return 0;
  92. }
  93.  
  94. void* northGenerator(void* args) {
  95. while(true)
  96. {
  97. do {
  98. car c;
  99. c.CarID = carCounter++;
  100. c.direction = 'N';
  101. c.arrival_time = getTime();
  102. c.start_time = "";
  103. c.end_time = "";
  104.  
  105. northCars.push(c);
  106. sem_post(&countNorth);
  107. } while (rand() % 100 + 1 <= 80);
  108. pthread_sleep(20);
  109. }
  110. return 0;
  111. }
  112.  
  113. void* southHandler(void* args) {
  114. bool locked = true;
  115. while(true) {
  116. //wait for a car going south and the mutex lock
  117. sem_wait(&countSouth);
  118. if(locked)
  119. pthread_mutex_lock(&road);
  120. locked = false;
  121. if (!flagPerson) {
  122. flagPerson = true;
  123. printToFlag("woken-up");
  124. }
  125. car c = southCars.front();
  126. c.start_time = getTime();
  127. pthread_sleep(1);
  128. southCars.pop();
  129. c.end_time = getTime();
  130. printToCar(c);
  131. if (northCars.size() >= 10 || southCars.empty()) {
  132. pthread_mutex_unlock(&road);
  133. locked = true;
  134. if (southCars.empty() && northCars.empty()) {
  135. flagPerson = false;
  136. printToFlag("sleep");
  137. }
  138. }
  139. }
  140. return 0;
  141. }
  142.  
  143. void* northHandler(void* args) {
  144. bool locked = true;
  145. while(true) {
  146. //wait for a car going north and the mutex lock
  147. sem_wait(&countNorth);
  148. if(locked)
  149. pthread_mutex_lock(&road);
  150. locked = false;
  151. if (!flagPerson) {
  152. flagPerson = true;
  153. printToFlag("woken-up");
  154. }
  155. car c = northCars.front();
  156. c.start_time = getTime();
  157. pthread_sleep(1);
  158. northCars.pop();
  159. c.end_time = getTime();
  160. printToCar(c);
  161. if (northCars.size() >= 10 || northCars.empty()) {
  162. pthread_mutex_unlock(&road);
  163. locked = true;
  164. if (southCars.empty() && northCars.empty()) {
  165. flagPerson = false;
  166. printToFlag("sleep");
  167. }
  168. }
  169. }
  170. return 0;
  171. }
  172.  
  173. int main() {
  174. srand (time(NULL));
  175. sem_init(&countSouth, 0, 0);
  176. sem_init(&countNorth, 0, 0);
  177. pthread_t south, north, sHandler, nHandler;
  178. carFile.open("car.log");
  179. flagFile.open("flagperson.log");
  180. carFile << "carID\tdirection\tarrival-time\tstart-time\tend-time" << endl;
  181. flagFile << "time\t\tstate" << endl;
  182.  
  183. if ( -1 == pthread_create(&south, NULL, southGenerator, NULL) ) {
  184. perror("pthread_create");
  185. return -1;
  186. }
  187.  
  188. if ( -1 == pthread_create(&north, NULL, northGenerator, NULL) ) {
  189. perror("pthread_create");
  190. return -1;
  191. }
  192.  
  193. if ( -1 == pthread_create(&sHandler, NULL, southHandler, NULL) ) {
  194. perror("pthread_create");
  195. return -1;
  196. }
  197.  
  198. if ( -1 == pthread_create(&nHandler, NULL, northHandler, NULL) ) {
  199. perror("pthread_create");
  200. return -1;
  201. }
  202.  
  203. cout << "Press enter to stop the simulation.";
  204. cin.ignore();
  205. carFile.close();
  206. flagFile.close();
  207. sem_destroy(&countSouth);
  208. sem_destroy(&countNorth);
  209. pthread_mutex_destroy(&road);
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement