blob: db8cf6944485b244db7404fe26d80c1279afae30 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "codexion.h"
# include <stdio.h> // for printf()
/*
This functions deal with all log printing activities. Logs are serialized by
usage of mutex_print_log. There are 2 scenarios for printing. The first one
scenarios applies if no philo has starved, the second one only prints if a
philo has starved; that is when flag = 1.
*/
void print_log(t_philo *philo, char *msg, int flag)
{
int id;
long start;
t_sim *sim;
(void)pthread_mutex_lock(&philo->mutex_sim);
sim = philo->sim;
(void)pthread_mutex_unlock(&philo->mutex_sim);
(void)pthread_mutex_lock(&philo->mutex_id);
id = philo->id;
(void)pthread_mutex_unlock(&philo->mutex_id);
(void)pthread_mutex_lock(&philo->sim->mutex_start_time);
start = philo->sim->start_time;
(void)pthread_mutex_unlock(&philo->sim->mutex_start_time);
(void)pthread_mutex_lock(&philo->sim->mutex_print_log);
if (!check_if_philo_starved(sim) && flag == 0)
printf("%d %i %s\n", get_rel_time(sim), id, msg);
else if (flag == 1)
printf("%lu %d %d %s\n", time_ms() - start, get_rel_time(sim), id, msg);
(void)pthread_mutex_unlock(&philo->sim->mutex_print_log);
}
/*
This function prints usage when the program is ran without parameters
*/
void print_usage(void)
{
printf("Usage: $ ./codexion number_of_philos "
"time_to_starve "
"time_to_eat "
"time_to_reflect "
"time_to_think "
"number_of_eats_required "
"fork_cooldown scheduler");
}
|