열렬히.뛰기

사전준비 & 파싱

école 42 > philosopher > philosopher : 진행 상황 > 사전준비 & 파싱

사전준비

  • 폴더 이름이 philo와 philo_bonus이다.
  • 내가 만든 libft는 못 쓴다.
    • 미리 atoi를 만들어두자

파싱

우선 입력 값을 한 번 살펴보자.

c
./philo 1 800 200 200 7

1st : 철학자의 수
2nd : 철학자의 생존시간 (단위 : 밀리초)
3rd : 철학자의 식사시간
4th : 철학자의 수면시간
5th : 철학자의 식사 횟수 (옵션)

다음과 같은 구조체를 선언하는 것이 편하다.

c
typedef struct  s_box
{
    t_philo         *philo;
    int             total_philo;    // 철학자 수, av[1]
    int             time_to_die;    // time to die, av[2]
    int             time_to_eat;    // time to eat, av[3]
    int             time_to_sleep;  // time to sleep, av[4]
    int             total_eat;      // 식사 횟수, av[5]
    long            init_point;     // 프로그램 시작 시간
    int             died_flag;      // 종료 플래그
    pthread_mutex_t *fork;          // 뮤택스 : 포크
    pthread_mutex_t eating_mutex;   // 뮤택스 : 먹기
    pthread_mutex_t print_mutex;    // 뮤택스 : 프린트
    pthread_mutex_t flag_mutex;     // 뮤택스 : 사망여부
}   t_box;

각 철학자의 정보를 담은 구조체를 만들어준다.

c
typedef struct  s_philo
{
    int             id;             // 철학자 id
    int             eating_num;     // 철학자의 식사 횟수
    int             left;           // 왼쪽 포크
    int             right;          // 오른쪽 포크
    long            philo_begin;    // 철학자의 활동 시작 시간
    long            last_eat;       // 철학자의 최근 식사 시간
    struct s_box    *tools;         // 도구
    pthread_t       thread_id;      // 쓰레드 id
}   t_philo;

채워주는 순서

tools 안에 philo가 있고, philo 안에 tools가 있다.

좀 순서가 헷갈릴 수 있다.

  1. t_box box는 정적할당으로 선언

  2. 구조체 t_box box를 채워준다.

  3. box의 philo를 동적할당

  4. philo를 while로 순회하면서 채워줌.

    philo 안의 tools는 philo[i].tools = tools 로 해주면 된다.