Skip to content

Latest commit

 

History

History
92 lines (81 loc) · 2.13 KB

philo.md

File metadata and controls

92 lines (81 loc) · 2.13 KB

Philosophers

直すところ

  • タイムスタンプを共通化する。
  • ポインタを渡すときは、64ビットコピーされるので、あまりよくないかも

Flow

  1. check input
    1. argc is 5 or 6
    2. isnum
    3. num_of_philo is between 2 and 200
    4. time_to_die is greater than 1
    5. time_to_eat is greater than 1
    6. time_to_sleep is greater than 1
      • if argc is 5, num_of_must_eat is LONG_MAX
      • if argc is 6, num_of_must_eat is greater than 1
  2. init_var
    • input input_args
    • input time
  3. create_threads
    1. if num_of_must_eat != 0 (argc == 6) create observer
    2. create philo threads
    3. create monitor threads

  4. destroy mutex

define

# define MAX_PHILO 200

Struct

Philosophers

typedef struct s_philo
{
	struct s_arg	*arg;
	pthread_mutex_t	*fork_left_mtx;
	pthread_mutex_t	*fork_right_mtx;
	int				id;
	long			last_eat_time;
	pthread_mutex_t	last_eat_time_m;
	int				num_of_eaten;
	pthread_mutex_t	num_of_eaten_m;
	pthread_t		thread;
}	t_philo;

argument

typedef struct s_arg
{
	long				num_of_philo;
	long				time_to_die;
	long				time_to_eat;
	long				time_to_sleep;
	long				num_of_must_eat;

	bool				dead;
	pthread_mutex_t		dead_m;
	bool				finish;
	pthread_mutex_t		finish_m;
	pthread_mutex_t		fork_m[MAX_PHILO];
	t_philo				philo[MAX_PHILO];
	long				start_time;
	pthread_mutex_t		write_mutex;
}	t_arg;

Tips

  • On computer at 42
     _STRUCT_TIMEVAL
     {
     	__darwin_time_t         tv_sec;         /* seconds */
     	__darwin_suseconds_t    tv_usec;        /* and microseconds */
     };
    
     typedef long                    __darwin_time_t;        /* time() */
     typedef __int32_t       __darwin_suseconds_t;   /* [???] microseconds */
    
     typedef int                     __int32_t;