Philosophers

Cover Image for Philosophers
Amine Beihaqi
Amine Beihaqi

Philosophers: Threads, Mutexes, and the Art of Dining

Introduction

Welcome to the world of philosophers, where the pursuit of wisdom becomes a deadly dance of threads and mutexes. In this project, you'll delve into the basics of threading a process, creating threads, and mastering mutexes. Are you ready to guide philosophers through the complex ritual of dining while avoiding the grim specter of starvation?

Common Instructions

Before we unravel the philosophy of dining, let's revisit some common instructions applicable to all projects:

  • Your implementation must be in C.
  • Adhere to the Norm, including bonus files/functions in the norm check.
  • Functions should not quit unexpectedly, and memory leaks are strictly forbidden.
  • Include a Makefile with essential rules: $(NAME), all, clean, fclean, and re.
  • Separate bonus parts in _bonus.{c/h} if not specified otherwise.
  • If your project allows the use of libft, include its sources and Makefile in a libft folder.

Overview: Philosophical Dining

In this section, we'll explore the dining rituals of philosophers, uncovering the intricacies of their interaction and the rules that govern their existence. Get ready to thread through the philosophical landscape!

Global Rules

Your mission, should you choose to accept it, is to write two programs: one for the mandatory part and another for the bonus part (if you're feeling adventurous). Both must adhere to the following rules:

  • Global variables are strictly forbidden.
  • Accept the following command-line arguments:
    • number_of_philosophers: The number of philosophers and forks.
    • time_to_die: Time for a philosopher to die if they haven't eaten.
    • time_to_eat: Time for a philosopher to eat.
    • time_to_sleep: Time a philosopher spends sleeping.
    • [number_of_times_each_philosopher_must_eat]: Optional argument to specify when the simulation stops.

Logging Rules

  • Any state change of a philosopher must be formatted as follows:
    • timestamp_in_ms X has taken a fork
    • timestamp_in_ms X is eating
    • timestamp_in_ms X is sleeping
    • timestamp_in_ms X is thinking
    • timestamp_in_ms X died

Safety First!

  • A message announcing a philosopher's death should be displayed no more than 10 ms after the actual death.

Mandatory Part: Threads, Forks, and Philosophers

Program Name: philo

Your journey begins with threads, forks, and philosophers. Each philosopher is a thread, and forks are your precious resources. Protect the forks' state with mutexes to avoid duplication.

Bonus Part: Processes, Semaphores, and the Ultimate Challenge

Program Name: philo_bonus

If you've mastered the mandatory part perfectly, it's time to embrace the bonus challenge. Philosophers are now processes, forks are semaphores, and the main process remains a wise observer.

Conclusion

As you embark on this philosophical journey, remember: no global variables, protect your resources, and guide philosophers through the intricate dance of life and death. May your threading and mutexes be flawless, and philosophers always avoid the fate of starvation!