Giter Site home page Giter Site logo

philosophers's Issues

철학자 문제 정리

  1. 시간 섞임
    시간을 구한 상태에서 컨텍스트 스위치가 일어나면 시간이 섞일 수 있다. 시간 계산하고, 출력하는 부분을 임계영역으로 잡아야 한다.
  2. 홀수 일 때는 (5 610 200 200) 죽는 시간이 먹는 시간의 최소 3배를 더 가지고 있어야 한다.
  3. 시간 밀림 해결(usleep 시간 줄이기)

./philo 3 610 200 200 일 때 죽음

#include "philo.h"
#include <stdio.h>

t_bool	put_up_a_fork(t_philo_info *info)
{
	if (check_flag_die(info))
		return (FAIL);
	pthread_mutex_lock(info->left_fork);
	pthread_mutex_lock(&info->sharing->m_print);
	printf("%lu %d %s\n", get_now_time(info), info->idx, STR_FORK);
	pthread_mutex_unlock(&info->sharing->m_print);
	pthread_mutex_lock(info->right_fork);
	pthread_mutex_lock(&info->sharing->m_print);
	printf("%lu %d %s\n", get_now_time(info), info->idx, STR_FORK);
	pthread_mutex_unlock(&info->sharing->m_print);
	return (SUCCESS);
}
#include "philo.h"

t_bool	put_down_a_fork(t_philo_info *info)
{
	if (check_flag_die(info))
		return (FAIL);
	pthread_mutex_unlock(info->left_fork);
	pthread_mutex_unlock(info->right_fork);
	return (SUCCESS);
}

시간이 지속적으로 밀리는 경우가 존재

#include "philo.h"

void	xusleep(t_philo_info *info, ssize_t state_time)
{
	ssize_t	time_to_start_xusleep;

	time_to_start_xusleep = get_now_time(info);
	while (get_now_time(info) - time_to_start_xusleep < state_time)
		usleep(300);
}

routine(think, eat, sleep) 함수 작성

고찰

  • think 부터 해야 하는 것인가에 대한 고찰
  • 바로 작동하는 프로그램 작성
  • 철학자가 한 명일 때 예외처리 => 포크를 들고 죽어야 함

구현

  • fork 올리고 내려놓기
  • fork 로직 사이에 먹기
  • fork 내려 놓은 뒤 자기
  • 잔 뒤 생각하기

[study] 220823 to do

  • mcha 님 코드 리뷰
  • mutax 관련 API 실패 시 에러 핸들링 공부
  • thread 와 process 차이 공부

두 번째 thread 가 죽지 않음

(lldb) up
frame #3: 0x0000000100002aba philo`join_thread(info=0x00007b4400000140, num_of_success_thread=5) at join_thread.c:22:3
   19  		i = 0;
   20  		while (i < num_of_success_thread)
   21  		{
-> 22  			pthread_join(info[i].tid, NULL);
   23  			i++;
   24  		}
   25  		if (num_of_success_thread == info->state->num_philo)
(lldb) p i
(int) $0 = 1

가끔 홀수의 철학자로 실패할 조건을 걸었을 때 밥을 먹었음에도 불구하고 먹고, 자는 시간을 합한 시간 보다 더 빠른 시간으로 죽는 문제가 있다.

static void	do_monitor(t_philo_info *info)
{
	int		i;

	i = 0;
	while (1)
	{
		if (i >= info->state->num_philo)
		{
			i = 0;
			usleep(500);
		}
		if (is_die(&info[i]))
		{
			info->sharing->flag_dead = DEAD;
			pthread_mutex_unlock(&info->sharing->m_flag);
			break ;
		}
		pthread_mutex_unlock(&info->sharing->m_flag);
		if (is_eat_enough(&info[i]))
		{
			if (++info->sharing->num_enough_eating == info->state->num_philo)
				break ;
		}
		i++;
	}
}

usleep 을 철학자 한 명을 볼 때마다 하게 되는데 이를 빼고, 한 사이클을 돌 때마다로 바꿈.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.