save from school
This commit is contained in:
18
README.md
18
README.md
@@ -7,31 +7,37 @@ options :
|
|||||||
- n_eat -> [number_of_times_each_philosopher_must_eat]
|
- n_eat -> [number_of_times_each_philosopher_must_eat]
|
||||||
```
|
```
|
||||||
|
|
||||||
thread :
|
**thread :**
|
||||||
|
|
||||||
- parts of a process that performs code simultaneously
|
- parts of a process that performs code simultaneously
|
||||||
- thread are light-weight-process (LWP) that happens inside a process (observe with command `ps -eLf`)
|
- thread are light-weight-process (LWP) that happens inside a process (observe with command `ps -eLf`)
|
||||||
- a process can be single-threaded or multi-threaded
|
- a process can be single-threaded or multi-threaded
|
||||||
- different process have a different PID and different LWP, different thread in the same process have the same PID and different LWP
|
- different process have a different PID and different LWP, different thread in the same process have the same PID and different LWP
|
||||||
- thread vs process :
|
- thread vs process :
|
||||||
process :
|
|
||||||
|
*process :*
|
||||||
|
|
||||||
- process is isolated, it doesn't share memory with any other process
|
- process is isolated, it doesn't share memory with any other process
|
||||||
- process is less efficient in communication, it takes more times to create or terminate or switch
|
- process is less efficient in communication, it takes more times to create or terminate or switch
|
||||||
- process speed is not impacted by speed of other process (untill it reach the limit of cpu)
|
- process speed is not impacted by speed of other process (untill it reach the limit of cpu)
|
||||||
thread :
|
|
||||||
|
*thread :*
|
||||||
|
|
||||||
- thread are not isolated, they share memory with the other threads
|
- thread are not isolated, they share memory with the other threads
|
||||||
- thread is more efficient in communication, it takes less times to create or terminate or switch
|
- thread is more efficient in communication, it takes less times to create or terminate or switch
|
||||||
- thread can become slow if the process does many concurrent tasks
|
- thread can become slow if the process does many concurrent tasks
|
||||||
|
|
||||||
- ressources :
|
- ressources :
|
||||||
- https://www.baeldung.com/linux/process-vs-thread
|
- https://www.baeldung.com/linux/process-vs-thread
|
||||||
|
|
||||||
mutex :
|
**mutex :**
|
||||||
|
|
||||||
- mutual exclusion
|
- mutual exclusion
|
||||||
- to prevent a part of the code to be performed simultaneously by threads
|
- to prevent a part of the code to be performed simultaneously by threads
|
||||||
- the section of code wraped by a mutex can be access by only one thread at a time
|
- the section of code wraped by a mutex can be access by only one thread at a time
|
||||||
- the section starts by unlocking a mutex, and end by locking it
|
- the section starts by unlocking a mutex, and end by locking it
|
||||||
|
|
||||||
|
**external function :**
|
||||||
external function :
|
|
||||||
|
|
||||||
- `memset` : fill memory with a constant byte
|
- `memset` : fill memory with a constant byte
|
||||||
- `printf` : format and print data
|
- `printf` : format and print data
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ typedef struct s_philo
|
|||||||
{
|
{
|
||||||
t_params *params;
|
t_params *params;
|
||||||
int philo_nbr;
|
int philo_nbr;
|
||||||
int fork;
|
t_mtx m_fork;
|
||||||
t_mtx *m_print;
|
t_mtx *m_print;
|
||||||
struct s_philo *prev;
|
struct s_philo *prev;
|
||||||
struct s_philo *next;
|
struct s_philo *next;
|
||||||
|
|||||||
34
srcs/exec.c
34
srcs/exec.c
@@ -1,32 +1,32 @@
|
|||||||
#include "philo.h"
|
#include "philo.h"
|
||||||
|
|
||||||
int take_forks(t_philo *philo)
|
void print_message(t_philo *philo, char *msg)
|
||||||
{
|
{
|
||||||
// pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(philo->m_print);
|
||||||
while (philo->fork == 0 || philo->next->fork == 0)
|
ft_printf("%i %s\n", philo->philo_nbr, msg);
|
||||||
continue ;
|
pthread_mutex_unlock(philo->m_print);
|
||||||
if (philo->fork == 1 && philo->next->fork == 1)
|
|
||||||
{
|
|
||||||
philo->fork = 0;
|
|
||||||
philo->next->fork = 0;
|
|
||||||
ft_printf("%i has taken a fork\n", philo->philo_nbr);
|
|
||||||
return (1);
|
|
||||||
}
|
}
|
||||||
// pthread_mutex_unlock(&mutex);
|
|
||||||
return (0);
|
void take_forks(t_philo *philo)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&(philo->m_fork));
|
||||||
|
pthread_mutex_lock(&(philo->next->m_fork));
|
||||||
|
|
||||||
|
print_message(philo, "has taken a fork");
|
||||||
|
usleep(philo->params->t_eat);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&(philo->next->m_fork));
|
||||||
|
pthread_mutex_unlock(&(philo->m_fork));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *philo_exec(void *arg)
|
void *philo_exec(void *arg)
|
||||||
{
|
{
|
||||||
t_philo *philo;
|
t_philo *philo;
|
||||||
int nbr;
|
|
||||||
|
|
||||||
philo = (t_philo*)arg;
|
philo = (t_philo*)arg;
|
||||||
nbr = philo->philo_nbr;
|
print_message(philo, "is thinking");
|
||||||
|
take_forks(philo);
|
||||||
|
|
||||||
pthread_mutex_lock(philo->m_print);
|
|
||||||
ft_printf("%i is thinking\n", philo->philo_nbr);
|
|
||||||
pthread_mutex_unlock(philo->m_print);
|
|
||||||
|
|
||||||
// eat
|
// eat
|
||||||
// "has taken a fork"
|
// "has taken a fork"
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ t_philo *lst_add_philo(t_philo *philo, t_params *params, t_mtx *m_print, int i)
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
new->params = params;
|
new->params = params;
|
||||||
new->philo_nbr = i + 1;
|
new->philo_nbr = i + 1;
|
||||||
new->fork = 1;
|
if (pthread_mutex_init(&(new->m_fork), NULL) != 0)
|
||||||
|
return (NULL);
|
||||||
new->m_print = m_print;
|
new->m_print = m_print;
|
||||||
if (philo)
|
if (philo)
|
||||||
philo->next = new;
|
philo->next = new;
|
||||||
|
|||||||
Reference in New Issue
Block a user