Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92f1faf36d | ||
|
|
d4d0285e3d | ||
|
|
50bbb9ae73 | ||
|
|
6596584e51 |
@@ -0,0 +1,11 @@
|
||||
# Название проекта
|
||||
|
||||
Какой-нибудь текст
|
||||
|
||||
*Текст обрамленный*
|
||||
|
||||
**Текст обрамленный**
|
||||
|
||||
***Текст обрамленный**
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include "race.h"
|
||||
|
||||
int main() {
|
||||
pthread_t participants[NUM_PARTICIPANTS];
|
||||
int participant_ids[NUM_PARTICIPANTS];
|
||||
|
||||
// Инициализация мьютекса и условных переменных
|
||||
pthread_mutex_init(info->tunnel_mutex, NULL);
|
||||
pthread_cond_init(info->start_cond, NULL);
|
||||
pthread_cond_init(info->finish_cond, NULL);
|
||||
|
||||
// Создание участников гонки
|
||||
for (int i = 0; i < NUM_PARTICIPANTS; i++) {
|
||||
participant_ids[i] = i + 1;
|
||||
pthread_create(&participants[i], NULL, participant, &participant_ids[i]);
|
||||
}
|
||||
|
||||
// Ожидание завершения гонки
|
||||
pthread_mutex_lock(info->tunnel_mutex);
|
||||
while (info->participants_finished < NUM_PARTICIPANTS) {
|
||||
// Ждем, пока все участники не закончат гонку
|
||||
pthread_cond_wait(info->finish_cond, info->tunnel_mutex);
|
||||
}
|
||||
pthread_mutex_unlock(info->tunnel_mutex);
|
||||
|
||||
printf("Гонка завершена! Победитель - участник %d\n", participant_ids[NUM_PARTICIPANTS - 1]);
|
||||
|
||||
// Очистка ресурсов
|
||||
pthread_mutex_destroy(info->tunnel_mutex);
|
||||
pthread_cond_destroy(info->start_cond);
|
||||
pthread_cond_destroy(info->finish_cond);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -2,3 +2,12 @@ Utkin Nikita Andreevich
|
||||
2020
|
||||
y.o. 02.03.2002 Omutninsk
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|
||||
sed do eiusmod tempor incididunt ut
|
||||
labore et dolore magna aliqua.
|
||||
Ut enim ad minim veniam, quis nostrud exercitation
|
||||
ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
Duis aute irure dolor in reprehenderit in
|
||||
voluptate velit esse cillum dolore eu fugiat nulla pariatur.
|
||||
Excepteur sint occaecat cupidatat non proident, sunt in culpa
|
||||
qui officia deserunt mollit anim id est laborum.
|
||||
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by mrnek on 25.05.2023.
|
||||
//
|
||||
#include "race.h"
|
||||
|
||||
void * participant(void *arg) { // Функция для участника гонки
|
||||
info->participants_in_tunnel = 0;
|
||||
info->participants_finished = 0;
|
||||
int participant_id = *(int *)arg;
|
||||
|
||||
// Подготовка участника
|
||||
printf("Участник %d готовится к гонке\n", participant_id);
|
||||
|
||||
// Ожидание начала гонки
|
||||
pthread_mutex_lock(info->tunnel_mutex);
|
||||
while (info->participants_in_tunnel >= TUNNEL_CAPACITY) {
|
||||
// Ждем, пока не освободится место в тоннеле
|
||||
pthread_cond_wait(info->start_cond, info->tunnel_mutex);
|
||||
}
|
||||
info->participants_in_tunnel++;
|
||||
pthread_mutex_unlock(info->tunnel_mutex);
|
||||
|
||||
printf("Участник %d начинает гонку\n", participant_id);
|
||||
|
||||
// Гонка
|
||||
|
||||
// Завершение гонки
|
||||
pthread_mutex_lock(info->tunnel_mutex);
|
||||
info->participants_finished++;
|
||||
if (info->participants_finished == NUM_PARTICIPANTS) {
|
||||
// Если все участники закончили гонку, уведомляем о завершении
|
||||
pthread_cond_broadcast(info->finish_cond);
|
||||
}
|
||||
pthread_mutex_unlock(info->tunnel_mutex);
|
||||
|
||||
printf("Участник %d закончил гонку\n", participant_id);
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by mrnek on 25.05.2023.
|
||||
//
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#ifndef DATA_RACE_MUTEX_H
|
||||
#define DATA_RACE_MUTEX_H
|
||||
|
||||
#define NUM_PARTICIPANTS 10
|
||||
#define TUNNEL_CAPACITY (NUM_PARTICIPANTS / 2)
|
||||
|
||||
struct race_info {
|
||||
pthread_mutex_t *tunnel_mutex; // Мьютекс для обеспечения взаимного исключения
|
||||
pthread_cond_t *start_cond, *finish_cond; // Условные переменные для ожидания начала и завершения гонки
|
||||
int participants_in_tunnel; // Количество участников в тоннеле
|
||||
int participants_finished ; // Количество закончивших гонку участников
|
||||
};
|
||||
|
||||
struct race_info *info;
|
||||
|
||||
void * participant(void *arg); // Функция для участника гонки
|
||||
|
||||
#endif //DATA_RACE_MUTEX_H
|
||||
Reference in New Issue
Block a user