ajout pdf et debut liste chainee en boucle

This commit is contained in:
hugogogo
2022-01-04 08:49:01 +01:00
parent a3821d5b15
commit 23f93e706a
5 changed files with 109 additions and 15 deletions

View File

@@ -4,7 +4,7 @@ options :
- t_die -> time_to_die
- t_eat -> time_to_eat
- t_slp -> time_to_sleep
- n_est -> [number_of_times_each_philosopher_must_eat]
- n_eat-> [number_of_times_each_philosopher_must_eat]
```
thread :

View File

@@ -8,10 +8,21 @@
# include <unistd.h>
# include <pthread.h>
typedef struct s_conditions
{
int n_phi; // number_of_philosophers
int t_die; // time_to_die
int t_eat; // time_to_eat
int t_slp; // time_to_sleep
int n_eat; // [number_of_times_each_philosopher_must_eat]
} t_conditions;
typedef struct s_philo
{
char *str;
int nbr;
} t_philo;
t_conditions *conditions;
int philo_nbr;
struct s_philo *prev;
struct s_philo *next;
} t_philo;
#endif

BIN
philosopher.pdf Normal file

Binary file not shown.

View File

@@ -1,23 +1,83 @@
#include "philo.h"
/*
void *philo_exec(void *arg)
{
t_philo *philo;
t_philo philo;
int nbr;
philo = (t_philo*)arg;
while (1)
write(1, philo->str, ft_strlen(philo->str));
philo = *(t_philo*)arg;
nbr = philo.n_philo;
ft_putnbr_fd(nbr, 1);
write(1, "\n", 1);
return (NULL);
}
*/
t_philo *init(char **av)
{
t_philo *philo;
t_philo *new;
t_conditions conditions;
int n;
int i;
n = ft_atoi(av[1]);
conditions.n_phi = n;
i = 0;
while (i < n)
{
new = malloc(sizeof(t_philo) * 1);
new->conditions = &conditions;
new->philo_nbr = i + 1;
if (i > 0)
{
new->prev = philo;
philo->next = new;
}
philo = new;
i++;
}
i = 0;
while (i < n - 1)
{
philo = philo->prev;
i++;
}
philo->prev = new;
new->next = philo;
return (philo);
}
int main(void)
int main(int ac, char **av)
{
pthread_t id;
// pthread_t id;
t_philo *philo;
philo = malloc(sizeof(philo));
philo->str = "i'm philosopher\n";
pthread_create(&id, NULL, &philo_exec, philo);
while (1)
write(1, "main function\n", 14);
if (ac == 1)
return (0);
philo = init(av);
int i; int n;
i = 0;
n = philo->conditions->n_phi;
ft_putnbr_fd(n, 1);
write(1, "\n\n", 2);
//while (i < n * 2 + n / 2)
while (i < philo->conditions->n_phi * 2 + 5)
{
ft_putnbr_fd(philo->conditions->n_phi, 1);
write(1, " - ", 3);
ft_putnbr_fd(philo->philo_nbr, 1);
write(1, "\n", 1);
philo = philo->prev;
i++;
}
// while (philo->n_philo)
// {
// pthread_create(&id, NULL, &philo_exec, philo);
// philo->n_philo--;
// }
// while (1)
// write(1, "main function\n", 14);
return 0;
}

23
srcs/main0.c Normal file
View File

@@ -0,0 +1,23 @@
#include "philo.h"
void *philo_exec(void *arg)
{
t_philo *philo;
philo = (t_philo*)arg;
while (1)
write(1, philo->str, ft_strlen(philo->str));
}
int main(void)
{
pthread_t id;
t_philo *philo;
philo = malloc(sizeof(philo));
philo->str = "i'm philosopher\n";
pthread_create(&id, NULL, &philo_exec, philo);
while (1)
write(1, "main function\n", 14);
return 0;
}