ClThread.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <malloc.h>
  5. struct Time
  6. {
  7. unsigned int hour;
  8. unsigned int minute;
  9. unsigned int second;
  10. };
  11. typedef struct Time Time;
  12. struct Worker
  13. {
  14. int id;
  15. Time time;
  16. };
  17. typedef struct Worker Worker;
  18. static void* showTime(void* arg);
  19. static void* workClock(void* arg);
  20. void PassTime(Time* time);
  21. pthread_t pt_watcher;
  22. pthread_t pt_clock;
  23. pthread_t pt_timer;
  24. pthread_t pt_stopwatch;
  25. int startThread(pthread_t* thread, void*(func)(void*), Worker* worker)
  26. {
  27. puts("Я стартанул часы.");
  28. int status = pthread_create(thread, NULL, func, worker);
  29. if(status != 0)
  30. {
  31. printf("Ошибка, статус = %d", status);
  32. return(-1);
  33. }
  34. return status;
  35. }
  36. int main()
  37. {
  38. int identifiers[] = { 1, 2, 3 };
  39. Time* clock = calloc(1, sizeof(Time));
  40. Time* timer = calloc(1, sizeof(Time));
  41. Time* stopwatch = calloc(1, sizeof(Time));
  42. Worker* worker = calloc(1, sizeof(Worker));
  43. worker->id = identifiers[0];
  44. //startThread(&pt_clock, workClock, worker);
  45. sleep(1);
  46. puts("Я поспал.");
  47. pthread_create(&pt_clock, NULL, workClock, worker);
  48. pthread_create(&pt_watcher, NULL, showTime, &(worker->time));
  49. void* res;
  50. pthread_join(pt_clock, &res);
  51. puts((char*)res);
  52. pthread_join(pt_watcher, &res);
  53. puts((char*)res);
  54. puts("Я все.");
  55. return 0;
  56. }
  57. void PassTime(Time* time)
  58. {
  59. puts("Вот счетчик.");
  60. while (1)
  61. {
  62. sleep(1);
  63. time->second++;
  64. if (time->second == 60)
  65. {
  66. time->second = 0;
  67. time->minute++;
  68. }
  69. if (time->minute == 60)
  70. {
  71. time->minute = 0;
  72. time->hour++;
  73. }
  74. if (time->hour == 24)
  75. {
  76. time->hour = 0;
  77. }
  78. }
  79. }
  80. static void* showTime(void* arg)
  81. {
  82. Time* time = (Time*) arg;
  83. Time* changeTime = malloc(sizeof(Time));
  84. changeTime->hour = time->hour;
  85. changeTime->minute = time->minute;
  86. changeTime->second = time->second;
  87. char* stringTime = malloc(sizeof(char) * 9);
  88. sprintf(stringTime, "%02d:%02d:%02d\0", time->hour, time->minute, time->second);
  89. puts(stringTime);
  90. for (;;)
  91. {
  92. if (changeTime->hour != time->hour || changeTime->minute != time->minute || changeTime->second != time->second)
  93. {
  94. sprintf(stringTime, "%02d:%02d:%02d\0", time->hour, time->minute, time->second);
  95. puts(stringTime);
  96. changeTime->hour = time->hour;
  97. changeTime->minute = time->minute;
  98. changeTime->second = time->second;
  99. }
  100. }
  101. pthread_exit(0);
  102. }
  103. static void* workClock(void* arg)
  104. {
  105. puts("Вот часы.");
  106. Worker* worker = (Worker*) arg;
  107. PassTime(&(worker->time));
  108. pthread_exit(0);
  109. }