add fixes to philo_two
This commit is contained in:
parent
8285f3b0a1
commit
74404afb18
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -8,8 +8,8 @@
|
|||||||
"type": "lldb",
|
"type": "lldb",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Debug",
|
"name": "Debug",
|
||||||
"program": "${workspaceFolder}/philo_one/philo_one",
|
"program": "${workspaceFolder}/philo_two/philo_two",
|
||||||
"args": ["5", "1", "1", "1", "1"],
|
"args": ["5", "800", "200", "200"],
|
||||||
"cwd": "${workspaceFolder}"
|
"cwd": "${workspaceFolder}"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@ -13,14 +13,6 @@
|
|||||||
# define _SEM_NAME_FORKS "pSemForks"
|
# define _SEM_NAME_FORKS "pSemForks"
|
||||||
# define _SEM_WRITE_LOCK "write"
|
# define _SEM_WRITE_LOCK "write"
|
||||||
|
|
||||||
#define ANSI_COLOR_RED "\x1b[31m"
|
|
||||||
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
||||||
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
|
||||||
#define ANSI_COLOR_BLUE "\x1b[34m"
|
|
||||||
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
|
||||||
#define ANSI_COLOR_CYAN "\x1b[36m"
|
|
||||||
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
||||||
|
|
||||||
typedef struct s_philo_data t_philo_data;
|
typedef struct s_philo_data t_philo_data;
|
||||||
|
|
||||||
typedef struct s_all
|
typedef struct s_all
|
||||||
@ -32,7 +24,7 @@ typedef struct s_all
|
|||||||
int number_of_times_each_philosopher_must_eat;
|
int number_of_times_each_philosopher_must_eat;
|
||||||
time_t start_time;
|
time_t start_time;
|
||||||
t_philo_data *data_philo;
|
t_philo_data *data_philo;
|
||||||
sem_t write;
|
sem_t *write;
|
||||||
int smert;
|
int smert;
|
||||||
int opt;
|
int opt;
|
||||||
} t_all;
|
} t_all;
|
||||||
@ -82,6 +74,10 @@ void my_usleep(long time);
|
|||||||
void ft_exit(t_all *all);
|
void ft_exit(t_all *all);
|
||||||
char *ft_itoa(int n);
|
char *ft_itoa(int n);
|
||||||
char *ft_strjoin(char const *s1, char const *s2);
|
char *ft_strjoin(char const *s1, char const *s2);
|
||||||
char *spread_name_and_number(char *str, int i);
|
|
||||||
sem_t *reopen_sem(const char *name, int value);
|
sem_t *reopen_sem(const char *name, int value);
|
||||||
|
char *sem_set_name(char *str, int i);
|
||||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t size);
|
||||||
|
size_t ft_strlcat(char *dst, const char *src, size_t size);
|
||||||
|
int ft_strlen(char *str);
|
||||||
|
void get_data_for_philo(t_all *all);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -27,25 +27,27 @@ void take_forks(t_philo *philo)
|
|||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
pthread_mutex_lock(&all->data_philo->mutex[philo->less_fork]); // TODO передать на на семафор
|
sem_wait(all->data_philo->sems);
|
||||||
messages(philo->all_struct, philo, "has taken a fork");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
pthread_mutex_lock(&all->data_philo->mutex[philo->more_fork]);
|
sem_wait(all->data_philo->sems);
|
||||||
messages(philo->all_struct, philo, "has taken a fork");
|
messages(philo->all_struct, philo, "has taken a fork");
|
||||||
}
|
}
|
||||||
void eating(t_philo *philo)
|
void eating(t_philo *philo)
|
||||||
{
|
{
|
||||||
|
sem_wait(philo->eat_sem);
|
||||||
messages(philo->all_struct, philo, "eating");
|
messages(philo->all_struct, philo, "eating");
|
||||||
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
philo->last_eat = ft_get_time() - philo->all_struct->start_time;
|
||||||
ft_msleep(philo->all_struct->time_to_eat);
|
ft_msleep(philo->all_struct->time_to_eat);
|
||||||
philo->count_eat++;
|
philo->count_eat++;
|
||||||
|
sem_post(philo->eat_sem);
|
||||||
}
|
}
|
||||||
void sleeping(t_philo *philo)
|
void sleeping(t_philo *philo)
|
||||||
{
|
{
|
||||||
t_all *all;
|
t_all *all;
|
||||||
all = philo->all_struct;
|
all = philo->all_struct;
|
||||||
messages(philo->all_struct, philo, "sleeping");
|
messages(philo->all_struct, philo, "sleeping");
|
||||||
// pthread_mutex_unlock(&all->data_philo->mutex[philo->more_fork]);
|
sem_post(all->data_philo->sems);
|
||||||
// pthread_mutex_unlock(&all->data_philo->mutex[philo->less_fork]);
|
sem_post(all->data_philo->sems);
|
||||||
ft_msleep(philo->all_struct->time_to_sleep);
|
ft_msleep(philo->all_struct->time_to_sleep);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +69,7 @@ int run_observer(t_all *all)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while(i <= all->number_of_philosophers)
|
while(i <= all->number_of_philosophers)
|
||||||
{
|
{
|
||||||
|
sem_wait(all->data_philo->philors[i].eat_sem);
|
||||||
if (all->smert != 42)
|
if (all->smert != 42)
|
||||||
{
|
{
|
||||||
time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat;
|
time_eat = (ft_get_time() - all->start_time) - all->data_philo->philors[i].last_eat;
|
||||||
@ -78,6 +81,7 @@ int run_observer(t_all *all)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return(2);
|
return(2);
|
||||||
|
sem_post(all->data_philo->philors[i].eat_sem);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,7 +32,7 @@ void run_simulation(t_all *all)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_fork_for_philo(t_all *all)
|
void get_data_for_philo(t_all *all)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -64,6 +64,7 @@ int prepare_resources(t_all *all)
|
|||||||
if (all->write == SEM_FAILED)
|
if (all->write == SEM_FAILED)
|
||||||
return(1);
|
return(1);
|
||||||
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
|
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
|
||||||
|
get_data_for_philo(all);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,68 @@ t_all *structalloc_and_bzero(void)
|
|||||||
return(all);
|
return(all);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t ft_strlcpy(char *dst, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int count;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
if (!dst && !src)
|
||||||
|
return (0);
|
||||||
|
while (src[count] != '\0')
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
if (size > 0)
|
||||||
|
{
|
||||||
|
while (src[i] != '\0' && i < (size - 1) && src[i])
|
||||||
|
{
|
||||||
|
dst[i] = src[i];
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
dst[i] = '\0';
|
||||||
|
}
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_strlcat(char *dst, const char *src, size_t size)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
unsigned int len;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
if (!dst && !src)
|
||||||
|
return (0);
|
||||||
|
len = ft_strlen(dst);
|
||||||
|
i = 0;
|
||||||
|
if (size < len)
|
||||||
|
return (ft_strlen((char *)src) + size);
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
while (src[i] && len + 1 < size)
|
||||||
|
dst[len++] = src[i++];
|
||||||
|
dst[len] = '\0';
|
||||||
|
}
|
||||||
|
while (src[i])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ft_strlen(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (str[i])
|
||||||
|
i++;
|
||||||
|
return(i);
|
||||||
|
}
|
||||||
|
|
||||||
int return_big(int a, int b)
|
int return_big(int a, int b)
|
||||||
{
|
{
|
||||||
if (a > b)
|
if (a > b)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user