|
@@ -1,62 +1,238 @@
|
|
|
+#define _CRT_SECURE_NO_WARNINGS
|
|
|
+#include <malloc.h>
|
|
|
#include <pthread.h>
|
|
|
-#include <stdio.h>
|
|
|
-#include <unistd.h>
|
|
|
-#include <pthread.h>
|
|
|
-#include <stdio.h>
|
|
|
-#include <unistd.h>
|
|
|
-#include <pthread.h>
|
|
|
-#include <stdio.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 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));
|
|
|
|
|
|
-pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;//создаем и инициализируем мьютекс
|
|
|
+ flag = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+static void *TimerCommon(void *args)
|
|
|
+{
|
|
|
|
|
|
-int hour = 0;
|
|
|
-int minutes = 0;
|
|
|
-int seconds = 0;
|
|
|
+ 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* countTime(void *args) {
|
|
|
- char buffer[100];
|
|
|
+static void *PrintTimer(void *param)
|
|
|
+{
|
|
|
+ struct ThreadArgs *timerStruct = (struct ThreadArgs *)param;
|
|
|
while (1)
|
|
|
{
|
|
|
- seconds++;
|
|
|
- if(seconds == 60) {
|
|
|
- seconds = 0;
|
|
|
- minutes++;
|
|
|
+ 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;
|
|
|
}
|
|
|
- if (minutes == 60)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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)
|
|
|
{
|
|
|
- minutes = 0;
|
|
|
- hour++;
|
|
|
+ flagTimer = 2;
|
|
|
+ pthread_exit(0);
|
|
|
}
|
|
|
- if (hour == 24) {
|
|
|
- hour == 0;
|
|
|
+ else if (timerStruct->seconds == 0)
|
|
|
+ {
|
|
|
+ timerStruct->seconds = 60;
|
|
|
+ timerStruct->minutes--;
|
|
|
+ if (timerStruct->minutes == 0)
|
|
|
+ {
|
|
|
+ timerStruct->minutes = 60;
|
|
|
+ timerStruct->hours--;
|
|
|
+ }
|
|
|
}
|
|
|
- snprintf(buffer,100, "%d:%d:%d", hour, minutes, seconds);
|
|
|
+ sleep(1);
|
|
|
+ }
|
|
|
+}
|
|
|
+static void *Secundomer(void *param)
|
|
|
+{
|
|
|
+ struct timespec start, end;
|
|
|
+ struct ThreadArgs *secundStruct = (struct ThreadArgs *)param;
|
|
|
+
|
|
|
|
|
|
-puts(buffer);
|
|
|
- // printf("%d:%d:%d", hour, minutes, seconds);
|
|
|
- sleep(1);
|
|
|
+ // Получаем начальное время
|
|
|
+ clock_gettime(CLOCK_MONOTONIC, &start);
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ flagSecund = 1;
|
|
|
|
|
|
- }
|
|
|
+ clock_gettime(CLOCK_MONOTONIC, &end);
|
|
|
+ // Вычисляем время, прошедшее с начала работы секундомера
|
|
|
+ long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec);
|
|
|
|
|
|
+ secundStruct->hours = elapsed_ns / (1000000000 * 60 * 60);
|
|
|
+ secundStruct->minutes = (elapsed_ns % (1000000000 * 60 * 60)) / (1000000000 * 60);
|
|
|
+ secundStruct->seconds = (elapsed_ns % (1000000000 * 60)) / 1000000000;
|
|
|
+ secundStruct->milisec = (elapsed_ns % 1000000000) / 1000000;
|
|
|
|
|
|
- return NULL;
|
|
|
+ sleep(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()
|
|
|
-
|
|
|
{
|
|
|
- pthread_t main_flow;
|
|
|
- pthread_create(&main_flow, NULL, countTime, NULL);
|
|
|
-
|
|
|
- pthread_join(main_flow, NULL);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return 0;
|
|
|
-
|
|
|
+ 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;
|
|
|
+ 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 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:
|
|
|
+ hMinTC = pthread_create(&i, NULL, Secundomer, &secundStruct);
|
|
|
+ hMinTCe = pthread_create(&u, NULL, PrintSecundTime, &secundStruct);
|
|
|
+ break;
|
|
|
+ // case 5:
|
|
|
+ // TerminateThread(hFSecund[0], 0);
|
|
|
+ // TerminateThread(hFSecund[1], 1);
|
|
|
+ // printf("Вы засекли %02d:%02d:%02d:%04d\n", secundStruct.hours, secundStruct.minutes, secundStruct.seconds, secundStruct.milisec);
|
|
|
+ // struct ThreadArgs secundStruct = {0, 0, 0, 0};
|
|
|
+ // break;
|
|
|
+ // case 6:
|
|
|
+ // printf("Введите часы:\n");
|
|
|
+ // scanf("%d", &timerStruct.hours);
|
|
|
+ // printf("Введите минуты:\n");
|
|
|
+ // scanf("%d", &timerStruct.minutes);
|
|
|
+ // printf("Введите секунды:\n");
|
|
|
+ // scanf("%d", &timerStruct.seconds);
|
|
|
+
|
|
|
+ // hFTimer[0] = CreateThread(NULL, 0, Timer, &timerStruct, 0, 0);
|
|
|
+ // hFTimer[1] = CreateThread(NULL, 0, PrintTimer, &timerStruct, 0, 0);
|
|
|
+ // struct ThreadArgs timerStruct = {0, 0, 0, 0};
|
|
|
+
|
|
|
+ // break;
|
|
|
+ // case 3:
|
|
|
+ // SuspendThread(hFMain[1]);
|
|
|
+ // SuspendThread(hFMain[0]);
|
|
|
+ // printf("Введите часы:\n");
|
|
|
+ // scanf("%d", &globalTime.hours);
|
|
|
+ // printf("Введите минуты:\n");
|
|
|
+ // scanf("%d", &globalTime.minutes);
|
|
|
+ // printf("Введите секунды:\n");
|
|
|
+ // scanf("%d", &globalTime.seconds);
|
|
|
+ // ResumeThread(hFMain[0]);
|
|
|
+ // ResumeThread(hFMain[1]);
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|