Compare commits
	
		
			1 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 92f1faf36d | 
							
								
								
									
										36
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								main.c
									
									
									
									
									
										Normal file
									
								
							@ -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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								race.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								race.c
									
									
									
									
									
										Normal file
									
								
							@ -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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								race.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								race.h
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user