brixxt27 / philosophers Goto Github PK
View Code? Open in Web Editor NEWthread 를 이용하면 일어나는 여러 문제들을 철학자의 식사 문제로 풀어 해결하는 프로그램입니다.
thread 를 이용하면 일어나는 여러 문제들을 철학자의 식사 문제로 풀어 해결하는 프로그램입니다.
#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);
}
(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 을 철학자 한 명을 볼 때마다 하게 되는데 이를 빼고, 한 사이클을 돌 때마다로 바꿈.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.