Skip to content

Instantly share code, notes, and snippets.

@nuoxoxo
Last active September 15, 2022 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuoxoxo/f31ca96be336ee6da6ec7e91c063143d to your computer and use it in GitHub Desktop.
Save nuoxoxo/f31ca96be336ee6da6ec7e91c063143d to your computer and use it in GitHub Desktop.
The Pho Eaters & the Chopsticks They Shared
  • The
  • Dining
  • Philosopers
  • Problem

๐Ÿ—พ Map

step 1 :: checks & allocate

	- check_params
	- table -> malloc
	- philo -> malloc

step 2 :: parsing

	- parse table
	- parse philo

step 3 :: simulation

	- create threads / paths, load routine
	- launch routine, run monitor
	- join paths
	- cleanup
		- free structs & mutexes

๐Ÿ“ Check params

  • same as Push_swap
    • check legitimacy of each digit
    • check integer bound

๐Ÿงต Allocate 3 things

  • threads
    • ๐Ÿงถ *a list of pthread_t
  • table
    • ๐Ÿ”’ has 4 public locks / mutexes
      • โ˜ ๏ธ Death - to detect a death
      • ๐Ÿ–จ๏ธ Print - to avoid scrambled text
      • ๐Ÿ“ˆ Meals - to count current number of meals had - if v[5]
      • โฐ Close - detect if enough meals reached - if v[5]
    • ๐ŸŽ has 2 flags
      • ๐Ÿ’€ flag of death
      • ๐Ÿฅฃ flag of enough eating
  • philo(s)
    • ๐Ÿ‘€ *a list of persons where everyone has access to:
      • ๐Ÿ”’ the same chopstick set chops ๐Ÿฅข
      • ๐Ÿฝ๏ธ the above table

๐Ÿ”’ Initialize all mutexes

  • ๐Ÿ‘ญ Initialize 2 sets of locks
    • ๐Ÿ€ a set of 4 locks from table
    • ๐Ÿฅ– the chopstick-set
      • *same amount as philosophers, each chopstick a mutex lock
  • ๐ŸŽ’ Launch philo routine
  • Create pthread_t[i] with a philo_routine

๐Ÿšฒ Routine:

  • check if someone is dead
  • check if all ate the pre-defined meals
  • Eat
    • ๐Ÿฅข Pick Up
    • Update last meal timestamp and meals eaten with mutex lock
    • check if all ate the pre-defined meals
    • Pass idle time
    • ๐Ÿฅข Put Down
    • return 0 if STOP - 1 if GO ON
  • Pick UP
    • *Principle:
      • Odd and even philos do different things
        • Odd ones go with sequence "L . R" (or "R . L")
        • Even ones go with sequence "R . L" (or "L . R")
  • Put DOWN
    • *Principle:
      • Using a reversed sequence as they have Picked Up

๐Ÿ‘ฎโ€โ™€๏ธ Monitor

  • What:
    • an arbitrator that decides when to end the game
  • 2 Checks
    • 1st check: Check_if_all_ate_enough (first bc. anyone should avoid dying)
      • Not Enough:
        • Circular check in a speed of 100ฮผs per philo
      • Enough:
        • ๐Ÿ‡ฏ๐Ÿ‡ต set flag with mutex
        • ๐Ÿ–จ๏ธ print message with mutex
        • break
    • 2nd check: Check if anyone died
      • one dies if one has been hungry for [time_to_die]ฮผs

๐Ÿค Join all threads

  • Join philos one by one
  • On Success of all philos joining up
    • Destroy all mutexes
    • Free allocated threads

๐Ÿ Clean and safe exit

# include "sys/time.h"
# include "pthread.h"
# include "unistd.h" /* sleep . usleep */
# include "stdlib.h" /* free . malloc */
# include "stdio.h" /* printf */
# include "./utils/print/print.h"
# define UP 0
# define DOWN 1
/* t */
typedef struct s_table
{
pthread_mutex_t mtx_death;
pthread_mutex_t mtx_print;
pthread_mutex_t mtx_meals;
pthread_mutex_t mtx_close;
long long t0;
int total_philo;
int time_to_eat;
int time_to_sleep;
int time_to_die;
int flag_of_death;
int flag_of_close;
int meals;
} t_table;
/* p */
typedef struct s_philo
{
pthread_mutex_t *chops;
t_table *table;
long long last_meal;
int id;
int eaten;
} t_philo;
/* setup */
void check_params(const int c, const char **v);
void clean_exit(t_philo *p, t_table *t, int exit_granted);
void void_cleaner(void *void_star);
int parse_table(t_table *t, const int c, const char **v);
int parse_philo(t_philo *p, t_table *t);
void usage_exit(void);
void ft_mutex_destroy(t_philo *p, t_table *t); // New in v7
void simulation_monitor(t_philo *p, t_table *t);
/* simulation */
void simulation_init_all_mutex(t_philo *p, t_table *t);
void simulation_launch_routine(t_philo *p, t_table *t, pthread_t *_);
void simulation_join_pthreads(t_philo *p, t_table *t, pthread_t *_);
void *philo_routine(void *tmp);
/* strategy */
int philo_eat(t_philo *p);
int philo_pick_up(t_philo *p);
int philo_put_down(t_philo *p);
/* miscellaneous */
void philo_pass_time(t_philo *p, long long n);
void philo_print_task(t_philo *p, char *message);
void philo_print_put_down(t_philo *p, int l, int r, int choice);
void philo_print_pick_up(t_philo *p, int l, int r, int choice);
// void philo_print_chops(t_philo *p, int l, int r, int choice, int side);
int everyone_ate_enough(t_philo *p);
int someone_is_dead(t_philo *p);
int check_if_all_ate_enough(t_philo *p, t_table *t);
void set_flag_of_death(t_table *t);
void set_flag_of_close(t_table *t);
void simulation_monitor(t_philo *p, t_table *t);
int ft_hierarchy(int x, int y, int choice);
/* toolkit */
long long ft_current_ms(void);
int ft_atoll(const char *_);
int ft_atoi(const char *_);
int print(const char *_, ...);
/* printer debuggers */
void print_endl(const char *_);
void print_cout(const char *_);
void print_params(const int c, const char **v);
void print_table(t_table *t);
void print_diners(t_philo *philos, int n);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment