Back

GitHub


Philosophers

Summary

Is an individual project about a classic problem in concurrent programming that involves simulating a scenario where a group of philosophers sits around a table, and each philosopher alternates between thinking, eating and sleeping. The philosophers are represented as concurrent threads or processes, and they interact with shared resources (forks) to eat, implementing synchronization mechanisms to prevent conflicts and ensure proper resource sharing as mutex locks and semaphores are used to avoid data races.

pthread_mutex_lock(&table->philo[philo->left_index].fork);
state(philo, FORK);
pthread_mutex_lock(&table->philo[philo->rigth_index].fork);
state(philo, FORK);
pthread_mutex_lock(&table->eat_check);
state(philo, EAT);
(philo->times_ate)++;
philo->last_eat = get_time();
pthread_mutex_unlock(&table->eat_check);
ft_sleep(table->time_to_eat);
pthread_mutex_unlock(&table->philo[philo->left_index].fork);
pthread_mutex_unlock(&table->philo[philo->rigth_index].fork);

Introduction

In this project, you will learn the basics of threading a process. You will see how to create threads and you will discover mutexes.

Here are the things you need to know if you want to succeed this assignment:

Mandatory part

Philosophers with threads and mutexes

Bonus part

Philosophers with processes and semaphores

The specific rules for the bonus part are: