123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <malloc.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h> // Для memset
- struct ThreadArgs
- {
- int hours;
- int minutes;
- int seconds;
- int milisec;
- };
- struct ThreadArgs globalTime;
- pthread_mutex_t mutex;
- pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
- int flagExit = 0;
- int flagRunTime = 1;
- int flag = 0;
- int flagSecund = 0;
- int flagTimer = 0;
- static void *PrintTimerCommon()
- {
- while (1)
- {
- if (flag)
- {
- char buffer[50];
- snprintf(buffer, sizeof(buffer), "Время обычное: %02d:%02d:%02d\n", globalTime.hours, globalTime.minutes, globalTime.seconds);
- puts(buffer);
- memset(buffer, 0, sizeof(buffer));
- flag = 0;
- }
- }
- }
- static void *TimerCommon(void *args)
- {
- while (1)
- {
- if (flagRunTime) {
- puts("flagRunTime");
- globalTime.seconds++;
- if (globalTime.seconds >= 60)
- {
- globalTime.seconds = 0;
- globalTime.minutes++;
- if (globalTime.minutes >= 60)
- {
- globalTime.minutes = 0;
- globalTime.hours++;
- if (globalTime.hours >= 24)
- {
- globalTime.hours = 0;
- }
- }
- }
- flag = 1;
- sleep(1);
- }
- }
- }
- static void *PrintTimer(void *param)
- {
- struct ThreadArgs *timerStruct = (struct ThreadArgs *)param;
- while (1)
- {
- char buffer[50];
- switch (flagTimer)
- {
- case 1:
- snprintf(buffer, sizeof(buffer), "Таймер: %02d:%02d:%02d\n", timerStruct->hours, timerStruct->minutes, timerStruct->seconds);
- puts(buffer);
- memset(buffer, 0, sizeof(buffer));
- flagTimer = 0;
- break;
- case 2:
- puts("ТАЙМЕР ЗАКОНЧИЛСЯ\n");
- pthread_exit(1);
- default:
- break;
- }
- }
- }
- static void *Timer(void *param)
- {
- struct ThreadArgs *timerStruct = (struct ThreadArgs *)param;
- while (1)
- {
- timerStruct->seconds--;
- flagTimer = 1;
- if (timerStruct->seconds == 0 && timerStruct->minutes == 0 && timerStruct->hours == 0)
- {
- flagTimer = 2;
- pthread_exit(0);
- }
- else if (timerStruct->seconds == 0)
- {
- timerStruct->seconds = 60;
- timerStruct->minutes--;
- if (timerStruct->minutes == 0)
- {
- timerStruct->minutes = 60;
- timerStruct->hours--;
- }
- }
- sleep(1);
- }
- }
- static void *Secundomer(void *param)
- {
- struct ThreadArgs *secundStruct = (struct ThreadArgs *)param;
- // Получаем начальное время
- while (1)
- {
- if (flagExit) {
- pthread_exit(1);
- }
- flagSecund = 1;
- secundStruct->milisec++;
- if (secundStruct->milisec >= 1000) {
- secundStruct->milisec = 0;
- secundStruct->seconds++;
- if (secundStruct->seconds >= 60) {
- secundStruct->seconds = 0;
- secundStruct->minutes++;
- if (secundStruct->minutes >= 60) {
- secundStruct->minutes = 0;
- secundStruct->hours++;
- }
- }
- }
-
- usleep(1);
- }
- }
- static void *PrintSecundTime(void *param)
- {
- struct ThreadArgs *secundStruct = (struct ThreadArgs *)param;
- while (1)
- {
- // printf("%d", flagSecund);
- if (flagSecund)
- {
- char buffer[50];
- snprintf(buffer, sizeof(buffer), "Секундомер: %d:%d:%d:%d\n", secundStruct->hours, secundStruct->minutes, secundStruct->seconds, secundStruct->milisec);
- puts(buffer);
- memset(buffer, 0, sizeof(buffer));
- flagSecund = 0;
- }
- }
- }
- int main()
- {
- time_t timer;
- struct tm *timeinfo;
- time(&timer);
- timeinfo = localtime(&timer);
- globalTime.hours = timeinfo->tm_hour;
- globalTime.minutes = timeinfo->tm_min;
- globalTime.seconds = timeinfo->tm_sec;
- pthread_t hMainTimerCommon;
- pthread_t hMainPrintTimerCommon;
- pthread_t i;
- pthread_t u;
- pthread_t hTimer;
- pthread_t hPrintTimer;
- int choise = 0;
- printf("Для остановки времени напишите 1\nДля возобновления времени напишите 7\nДля настройки времени нажмите 3\nДля режима секундомера нажмите 4, чтобы выйти нажмите 5\nДля режима таймера нажмите 6\n");
- int hMainTC = pthread_create(&hMainTimerCommon, NULL, TimerCommon, NULL);
- struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
- struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
- int hMainPTC = pthread_create(&hMainPrintTimerCommon, NULL, PrintTimerCommon, NULL);
- int hMinTC = 0;
- int timerC = 0;
- int printTimer = 0;
- int hMinTCe = 0;
- while (1)
- {
- scanf("%d", &choise);
- switch (choise)
- {
- case 1:
- flagRunTime = 0;
- break;
- case 7:
- pthread_cond_signal(&cond);
- flagRunTime = 1;
- break;
- case 4:
- flagExit = 0;
- hMinTC = pthread_create(&i, NULL, Secundomer, &secundStruct);
- hMinTCe = pthread_create(&u, NULL, PrintSecundTime, &secundStruct);
- break;
- case 5:
- flagExit = 1;
- printf("Вы засекли %02d:%02d:%02d:%04d\n", secundStruct.hours, secundStruct.minutes, secundStruct.seconds, secundStruct.milisec);
- secundStruct.hours = 0;
- secundStruct.minutes = 0;
- secundStruct.seconds = 0;
- secundStruct.milisec = 0;
- break;
- case 6:
- printf("Введите часы:\n");
- scanf("%d", &timerStruct.hours);
- printf("Введите минуты:\n");
- scanf("%d", &timerStruct.minutes);
- printf("Введите секунды:\n");
- scanf("%d", &timerStruct.seconds);
- timerC = pthread_create(&hTimer, NULL, Timer, &timerStruct);
- printTimer = pthread_create(&hPrintTimer, NULL, PrintTimer, &timerStruct);
-
- break;
- case 3:
- flagRunTime = 0;
- printf("Введите часы:\n");
- scanf("%d", &globalTime.hours);
- printf("Введите минуты:\n");
- scanf("%d", &globalTime.minutes);
- printf("Введите секунды:\n");
- scanf("%d", &globalTime.seconds);
- flagRunTime = 1;
- break;
- case 9:
- return 0;
- break;
- default:
- break;
- }
- }
- }
|