main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <malloc.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <string.h> // Для memset
  8. struct ThreadArgs
  9. {
  10. int hours;
  11. int minutes;
  12. int seconds;
  13. int milisec;
  14. };
  15. struct ThreadArgs globalTime;
  16. pthread_mutex_t mutex;
  17. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  18. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  19. int flagExit = 0;
  20. int flagRunTime = 1;
  21. int flag = 0;
  22. int flagSecund = 0;
  23. int flagTimer = 0;
  24. static void *PrintTimerCommon()
  25. {
  26. while (1)
  27. {
  28. if (flag)
  29. {
  30. char buffer[50];
  31. snprintf(buffer, sizeof(buffer), "Время обычное: %02d:%02d:%02d\n", globalTime.hours, globalTime.minutes, globalTime.seconds);
  32. puts(buffer);
  33. memset(buffer, 0, sizeof(buffer));
  34. flag = 0;
  35. }
  36. }
  37. }
  38. static void *TimerCommon(void *args)
  39. {
  40. while (1)
  41. {
  42. if (flagRunTime) {
  43. puts("flagRunTime");
  44. globalTime.seconds++;
  45. if (globalTime.seconds >= 60)
  46. {
  47. globalTime.seconds = 0;
  48. globalTime.minutes++;
  49. if (globalTime.minutes >= 60)
  50. {
  51. globalTime.minutes = 0;
  52. globalTime.hours++;
  53. if (globalTime.hours >= 24)
  54. {
  55. globalTime.hours = 0;
  56. }
  57. }
  58. }
  59. flag = 1;
  60. sleep(1);
  61. }
  62. }
  63. }
  64. static void *PrintTimer(void *param)
  65. {
  66. struct ThreadArgs *timerStruct = (struct ThreadArgs *)param;
  67. while (1)
  68. {
  69. char buffer[50];
  70. switch (flagTimer)
  71. {
  72. case 1:
  73. snprintf(buffer, sizeof(buffer), "Таймер: %02d:%02d:%02d\n", timerStruct->hours, timerStruct->minutes, timerStruct->seconds);
  74. puts(buffer);
  75. memset(buffer, 0, sizeof(buffer));
  76. flagTimer = 0;
  77. break;
  78. case 2:
  79. puts("ТАЙМЕР ЗАКОНЧИЛСЯ\n");
  80. pthread_exit(1);
  81. default:
  82. break;
  83. }
  84. }
  85. }
  86. static void *Timer(void *param)
  87. {
  88. struct ThreadArgs *timerStruct = (struct ThreadArgs *)param;
  89. while (1)
  90. {
  91. timerStruct->seconds--;
  92. flagTimer = 1;
  93. if (timerStruct->seconds == 0 && timerStruct->minutes == 0 && timerStruct->hours == 0)
  94. {
  95. flagTimer = 2;
  96. pthread_exit(0);
  97. }
  98. else if (timerStruct->seconds == 0)
  99. {
  100. timerStruct->seconds = 60;
  101. timerStruct->minutes--;
  102. if (timerStruct->minutes == 0)
  103. {
  104. timerStruct->minutes = 60;
  105. timerStruct->hours--;
  106. }
  107. }
  108. sleep(1);
  109. }
  110. }
  111. static void *Secundomer(void *param)
  112. {
  113. struct ThreadArgs *secundStruct = (struct ThreadArgs *)param;
  114. // Получаем начальное время
  115. while (1)
  116. {
  117. if (flagExit) {
  118. pthread_exit(1);
  119. }
  120. flagSecund = 1;
  121. secundStruct->milisec++;
  122. if (secundStruct->milisec >= 1000) {
  123. secundStruct->milisec = 0;
  124. secundStruct->seconds++;
  125. if (secundStruct->seconds >= 60) {
  126. secundStruct->seconds = 0;
  127. secundStruct->minutes++;
  128. if (secundStruct->minutes >= 60) {
  129. secundStruct->minutes = 0;
  130. secundStruct->hours++;
  131. }
  132. }
  133. }
  134. usleep(1);
  135. }
  136. }
  137. static void *PrintSecundTime(void *param)
  138. {
  139. struct ThreadArgs *secundStruct = (struct ThreadArgs *)param;
  140. while (1)
  141. {
  142. // printf("%d", flagSecund);
  143. if (flagSecund)
  144. {
  145. char buffer[50];
  146. snprintf(buffer, sizeof(buffer), "Секундомер: %d:%d:%d:%d\n", secundStruct->hours, secundStruct->minutes, secundStruct->seconds, secundStruct->milisec);
  147. puts(buffer);
  148. memset(buffer, 0, sizeof(buffer));
  149. flagSecund = 0;
  150. }
  151. }
  152. }
  153. int main()
  154. {
  155. time_t timer;
  156. struct tm *timeinfo;
  157. time(&timer);
  158. timeinfo = localtime(&timer);
  159. globalTime.hours = timeinfo->tm_hour;
  160. globalTime.minutes = timeinfo->tm_min;
  161. globalTime.seconds = timeinfo->tm_sec;
  162. pthread_t hMainTimerCommon;
  163. pthread_t hMainPrintTimerCommon;
  164. pthread_t i;
  165. pthread_t u;
  166. pthread_t hTimer;
  167. pthread_t hPrintTimer;
  168. int choise = 0;
  169. printf("Для остановки времени напишите 1\nДля возобновления времени напишите 7\nДля настройки времени нажмите 3\nДля режима секундомера нажмите 4, чтобы выйти нажмите 5\nДля режима таймера нажмите 6\n");
  170. int hMainTC = pthread_create(&hMainTimerCommon, NULL, TimerCommon, NULL);
  171. struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
  172. struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
  173. int hMainPTC = pthread_create(&hMainPrintTimerCommon, NULL, PrintTimerCommon, NULL);
  174. int hMinTC = 0;
  175. int timerC = 0;
  176. int printTimer = 0;
  177. int hMinTCe = 0;
  178. while (1)
  179. {
  180. scanf("%d", &choise);
  181. switch (choise)
  182. {
  183. case 1:
  184. flagRunTime = 0;
  185. break;
  186. case 7:
  187. pthread_cond_signal(&cond);
  188. flagRunTime = 1;
  189. break;
  190. case 4:
  191. flagExit = 0;
  192. hMinTC = pthread_create(&i, NULL, Secundomer, &secundStruct);
  193. hMinTCe = pthread_create(&u, NULL, PrintSecundTime, &secundStruct);
  194. break;
  195. case 5:
  196. flagExit = 1;
  197. printf("Вы засекли %02d:%02d:%02d:%04d\n", secundStruct.hours, secundStruct.minutes, secundStruct.seconds, secundStruct.milisec);
  198. secundStruct.hours = 0;
  199. secundStruct.minutes = 0;
  200. secundStruct.seconds = 0;
  201. secundStruct.milisec = 0;
  202. break;
  203. case 6:
  204. printf("Введите часы:\n");
  205. scanf("%d", &timerStruct.hours);
  206. printf("Введите минуты:\n");
  207. scanf("%d", &timerStruct.minutes);
  208. printf("Введите секунды:\n");
  209. scanf("%d", &timerStruct.seconds);
  210. timerC = pthread_create(&hTimer, NULL, Timer, &timerStruct);
  211. printTimer = pthread_create(&hPrintTimer, NULL, PrintTimer, &timerStruct);
  212. break;
  213. case 3:
  214. flagRunTime = 0;
  215. printf("Введите часы:\n");
  216. scanf("%d", &globalTime.hours);
  217. printf("Введите минуты:\n");
  218. scanf("%d", &globalTime.minutes);
  219. printf("Введите секунды:\n");
  220. scanf("%d", &globalTime.seconds);
  221. flagRunTime = 1;
  222. break;
  223. case 9:
  224. return 0;
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. }