convert to sem_t

This commit is contained in:
Matthos Denys 2021-05-16 22:33:06 +03:00
parent 8dff2b4a80
commit 255f85c9ea
9 changed files with 110 additions and 39 deletions

1
.vscode/dryrun.log vendored
View File

@ -1,5 +1,6 @@
make --dry-run --keep-going --print-directory
make: Entering directory `/Users/mdenys/Desktop/projects/philosophers'
make: Leaving directory `/Users/mdenys/Desktop/projects/philosophers'
make: *** No targets specified and no makefile found. Stop.

8
.vscode/targets.log vendored
View File

@ -10,7 +10,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
make: *** No rule to make target `all'. Stop.
# Make data base, printed on Sun May 16 14:47:41 2021
# Make data base, printed on Sun May 16 15:43:05 2021
# Variables
@ -149,9 +149,9 @@ VSCODE_PID = 27414
# Directories
# . (device 16777227, inode 5034668): 8 files, no impossibilities.
# . (device 16777227, inode 5034668): 9 files, no impossibilities.
# 8 files, no impossibilities in 1 directories.
# 9 files, no impossibilities in 1 directories.
# Implicit Rules
@ -223,6 +223,6 @@ GNUmakefile:
# strcache size: total = 0 / max = 0 / min = 4096 / avg = 0
# strcache free: total = 0 / max = 0 / min = 4096 / avg = 0
# Finished Make data base on Sun May 16 14:47:41 2021
# Finished Make data base on Sun May 16 15:43:05 2021

View File

@ -1,6 +1,8 @@
.PHONY: all clean fclean re
SRCS = main.c utilc.c parser.c logic.c messages.c prepare_simulation.c old_utilc.c time.c
SRCS = main.c utilc.c parser.c logic.c messages.c\
prepare_simulation.c old_utilc.c time.c ft_itoa.c\
ft_strjoin.c
SRC_DIR = ./srcs/

View File

@ -6,10 +6,12 @@
# include <unistd.h>
# include <pthread.h>
# include <sys/time.h>
# include <semaphore.h>
# include <stdio.h>
# include <string.h>
# define _SEM_NAME_FORKS "pSemForks"
# define _SEM_WRITE_LOCK "write"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
@ -30,7 +32,7 @@ typedef struct s_all
int number_of_times_each_philosopher_must_eat;
time_t start_time;
t_philo_data *data_philo;
pthread_mutex_t write;
sem_t write;
int smert;
int opt;
} t_all;
@ -40,8 +42,6 @@ typedef struct s_philo
{
unsigned worked:1;
unsigned can_run_simulation:1;
int more_fork;
int less_fork;
pthread_t thread;
int count_eat;
time_t time_to_die;
@ -51,12 +51,12 @@ typedef struct s_philo
time_t limit;
t_all *all_struct;
int is_eat;
pthread_mutex_t eat_mutex;
sem_t *eat_sem;
} t_philo;
typedef struct s_philo_data
{
pthread_mutex_t *mutex;
sem_t *sems;
t_philo *philors;
} t_philo_data;
@ -79,4 +79,8 @@ void cycle(t_philo *philo);
void ft_msleep(time_t interval_ms);
void my_usleep(long time);
void ft_exit(t_all *all);
char *ft_itoa(int n);
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);
#endif

43
philo_two/srcs/ft_itoa.c Normal file
View File

@ -0,0 +1,43 @@
#include "../includes/philo_header.h"
static int nbleng(unsigned int n)
{
unsigned int i;
i = 0;
while (n >= 10)
{
n /= 10;
i++;
}
return (i + 1);
}
char *ft_itoa(int n)
{
char *dst;
unsigned int leng;
unsigned int nbr;
unsigned int i;
nbr = (n < 0 ? -n : n);
leng = nbleng(nbr);
i = 0;
if (!(dst = (char *)malloc(sizeof(char) * leng + 1 + (n < 0 ? 1 : 0))))
return (NULL);
if (n < 0)
{
dst[i] = '-';
leng++;
}
i = leng - 1;
while (nbr >= 10)
{
dst[i] = nbr % 10 + '0';
nbr /= 10;
i--;
}
dst[i] = nbr % 10 + '0';
dst[leng] = '\0';
return (dst);
}

View File

@ -0,0 +1,22 @@
#include "../includes/philo_header.h"
char *ft_strjoin(char const *s1, char const *s2)
{
int len1;
int len2;
char *new;
if (s1 && s2)
{
len1 = ft_strlen((char*)s1);
len2 = ft_strlen((char*)s2);
new = malloc(sizeof(char *) * len1 + len2 + 1);
if (new)
{
ft_strlcpy(new, s1, len1 + 1);
ft_strlcat(new, s2, len1 + len2 + 1);
return (void *)(new);
}
}
return (0);
}

View File

@ -5,8 +5,8 @@ void messages(t_all *all, t_philo *philo, char *str)
time_t timestemp = ft_get_time() - all->start_time;
if (philo->all_struct->smert != 42)
{
pthread_mutex_lock(&all->write);
sem_wait(all->write);
printf("%ld %*s%d %s\n", timestemp, philo->nbr_philo * 30, "", philo->nbr_philo + 1, str);
pthread_mutex_unlock(&all->write);
sem_post(all->write);
}
}

View File

@ -40,16 +40,6 @@ void get_fork_for_philo(t_all *all)
while(i <= all->number_of_philosophers)
{
if (i == 0)
{
all->data_philo->philors[i].more_fork = all->number_of_philosophers;
all->data_philo->philors[i].less_fork = 0;
}
else
{
all->data_philo->philors[i].more_fork = return_big(i, i + 1);
all->data_philo->philors[i].less_fork = return_less(i, i + 1);
}
all->data_philo->philors[i].died = 0;
all->data_philo->philors[i].nbr_philo = i;
all->data_philo->philors[i].worked = 0;
@ -62,18 +52,14 @@ void get_fork_for_philo(t_all *all)
int prepare_resources(t_all *all)
{
int i;
i = 0;
all->data_philo = ft_calloc(sizeof(t_philo_data), 1);
all->data_philo->mutex = (pthread_mutex_t *)ft_calloc(sizeof(pthread_mutex_t), all->number_of_philosophers);
while (i <= all->number_of_philosophers)
{
pthread_mutex_init(&all->data_philo->mutex[i], NULL);
i++;
}
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->number_of_philosophers);
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
if (all->data_philo->sems == SEM_FAILED)
return(1); // TODO выход из программы сделать
if (all->write == SEM_FAILED)
return(1);
all->data_philo->philors = (t_philo *)ft_calloc(sizeof(t_philo), all->number_of_philosophers);
get_fork_for_philo(all);
return (0);
}
@ -86,13 +72,9 @@ void run_all_phils(t_all *all)
{
philo = (void*)(&all->data_philo->philors[i]);
if (pthread_create(&all->data_philo->philors[i].thread, NULL, (void*)run_trhead, philo) == -1)
{
printf("I can't create a thread t%d\n", i);
}
else
{
pthread_detach(all->data_philo->philors[i].thread);
}
i++;
}
}

View File

@ -40,8 +40,8 @@ void ft_exit(t_all *all)
ft_msleep(200);
while (i <= all->number_of_philosophers)
{
pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex);
pthread_mutex_destroy(&all->data_philo->mutex[i]);
// pthread_mutex_destroy(&all->data_philo->philors[i].eat_mutex);
//pthread_mutex_destroy(&all->data_philo->mutex[i]); // TODO fix this
i++;
}
i = 0;
@ -51,3 +51,20 @@ void ft_exit(t_all *all)
i++;
}
}
char *sem_set_name(char *str, int i)
{
char *temp_nbr;
char *returnstr;
temp_nbr = ft_itoa(i);
returnstr = ft_strjoin(str, temp_nbr);
free(temp_nbr);
return(returnstr);
}
sem_t *reopen_sem(const char *name, int value)
{
sem_unlink(name);
return (sem_open(name, O_RDWR | O_CREAT, S_IRWXU, value));
}