Browse Source

starting rework

master
narnaud 3 years ago
parent
commit
d772f5fb48
  1. 15
      README.md
  2. 26
      philo_bonus/Makefile
  3. 43
      philo_bonus/philo.c
  4. 64
      philo_bonus/philo.h
  5. 38
      philo_bonus/philo_utils.c

15
README.md

@ -19,17 +19,6 @@ PHILOSOPHERS BONUS
## Docs :
[Semaphores](https://sites.uclouvain.be/SyllabusC/notes/Theorie/Threads/coordination.html)
## Structure :
Main :
- parsing() ->
- create table and *philos
- setup parameters
- create sticks semaphore
- init_philos() ->
- create array of childs pids
- for each philo, create a fork() :
- philo_life() ->
## Todo :
- handle meals amount;
-
- loop

26
philo_bonus/Makefile

@ -0,0 +1,26 @@
NAME = philo
SRCS = philo.c philo_utils.c
OBJS = ${SRCS:.c=.o}
CC = clang
CFLAGS = -Werror -Wextra -Wall -D_REENTRANT
RM = rm -rf
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $(<:.c=.o)
all : $(NAME)
$(NAME) : $(OBJS)
$(CC) $(CFLAGS) -lpthread $(OBJS) -o $(NAME)
clean :
$(RM) $(OBJS)
fclean : clean
$(RM) $(NAME)
re : fclean all
.PHONY : all clean fclean re

43
philo_bonus/philo.c

@ -6,15 +6,15 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:42 by narnaud #+# #+# */
/* Updated: 2022/05/16 14:03:47 by narnaud ### ########.fr */
/* Updated: 2022/05/20 09:39:41 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
#include "philo.h"
void *check_halt(void * p)
{
size_t time;
int time;
t_philo *philo;
philo = p;
@ -22,10 +22,9 @@ void *check_halt(void * p)
{
usleep(1000);
time = gettime();
philo->table->time = time;
if (time - philo->last_meal_time > philo->table->time_to_die)
if (time - philo->last_meal_time > philo->table->param[DIE_TIME])
{
printf("%lu %d died\n", time, philo->id);
printf("%i %d died\n", time, philo->id);
exit(EXIT_SUCCESS);
}
}
@ -39,29 +38,31 @@ void philo_life(t_philo *philo)
while (1)
{
sem_wait(table->sptr);
printf("%lu %d has taken a fork\n", table->time, philo->id);
printf("%i %d has taken a fork\n", gettime(), philo->id);
sem_wait(table->sptr);
printf("%lu %d has taken a fork\n", table->time, philo->id);
philo->last_meal_time = table->time;
printf("%lu %d is eating\n", table->time, philo->id);
usleep(table->eating_time * 1000);
printf("%i %d has taken a fork\n", gettime(), philo->id);
philo->last_meal_time = gettime();
printf("%i %d is eating\n", gettime(), philo->id);
usleep(table->param[EAT_DURATION] * 1000);
sem_post(table->sptr);
sem_post(table->sptr);
printf("%lu %d is sleeping\n", table->time, philo->id);
usleep(table->sleeping_time * 1000);
printf("%lu %d is thinking\n", table->time, philo->id);
printf("%i %d is sleeping\n", gettime(), philo->id);
usleep(table->param[SLEEP_DURATION] * 1000);
printf("%i %d is thinking\n", gettime(), philo->id);
}
}
void init_philos(t_table *table)
{
size_t i;
int i;
pthread_t checks;
int amount;
table->pid = malloc(sizeof(pid_t) * table->nb_of_philos);
memset(table->pid, 0, sizeof(pid_t) * table->nb_of_philos);
amount = table->param[PHILO_AMOUNT];
table->pid = malloc(sizeof(pid_t) * amount);
//memset(table->pid, 0, sizeof(pid_t) * amount);
i = 0;
while (i < table->nb_of_philos)
while (i < amount)
{
table->pid[i] = fork();
table->philos[i].table = table;
@ -79,7 +80,7 @@ void init_philos(t_table *table)
int main(int argc, char **argv)
{
static t_table *table;
static size_t i = 0;
static int i = 0;
int status;
sem_unlink("/chopsticks");
@ -87,11 +88,11 @@ int main(int argc, char **argv)
if (argc < 5 || argc > 6)
return (1);
table = parsing(argv + 1);
table->sptr = sem_open("/chopsticks", O_CREAT, 0664, table->nb_of_philos);
table->sptr = sem_open("/chopsticks", O_CREAT, 0664, table->param[PHILO_AMOUNT]);
table->death = sem_open("/death", O_CREAT, 0664, 1);
init_philos(table);
waitpid(-1, &status, 0);
while (i < table->nb_of_philos)
while (i < table->param[PHILO_AMOUNT])
{
kill(table->pid[i], SIGINT);
i++;

64
philo_bonus/philo.h

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* philo.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:03:01 by narnaud #+# #+# */
/* Updated: 2022/05/20 09:26:20 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PHILOSOPHERS_H
# define PHILOSOPHERS_H
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
# include <pthread.h>
# include <string.h>
# include <errno.h>
# include <fcntl.h>
# include <signal.h>
# include <sys/wait.h>
# include <sys/time.h>
# include <sys/stat.h>
# include <semaphore.h>
enum e_params
{
PHILO_AMOUNT,
DIE_TIME,
EAT_DURATION,
SLEEP_DURATION,
MEALS_AMOUNT,
};
typedef struct s_table t_table;
typedef struct s_philo
{
int id;
size_t meals_done;
int last_meal_time;
t_table *table;
} t_philo;
typedef struct s_table
{
int param[5];
int i;
sem_t *sptr;
sem_t *death;
t_philo *philos;
int time;
int *pid;
} t_table;
/***************************/
t_table *parsing(char **argv);
int gettime(void);
#endif

38
philo_bonus/philo_utils.c

@ -6,11 +6,11 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/16 14:04:06 by narnaud #+# #+# */
/* Updated: 2022/05/16 14:04:11 by narnaud ### ########.fr */
/* Updated: 2022/05/20 09:40:52 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "philosophers.h"
#include "philo.h"
static int mini_atoi(char *nbr)
{
@ -30,31 +30,29 @@ t_table *parsing(char **argv)
t_table *table;
table = malloc(sizeof(t_table));
memset(table, 0, sizeof(t_table));
table->nb_of_philos = mini_atoi(argv[0]);
table->philos = malloc(sizeof(t_philo) * table->nb_of_philos);
memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
table->time_to_die = mini_atoi(argv[1]);
table->eating_time = mini_atoi(argv[2]);
table->sleeping_time = mini_atoi(argv[3]);
table->time = gettime();
//ft_bzero(table, 0, sizeof(t_table));
table->param[PHILO_AMOUNT] = mini_atoi(argv[0]);
table->param[DIE_TIME] = mini_atoi(argv[1]);
table->param[EAT_DURATION] = mini_atoi(argv[2]);
table->param[SLEEP_DURATION] = mini_atoi(argv[3]);
if (argv[4])
table->nb_of_meal = mini_atoi(argv[4]);
table->param[MEALS_AMOUNT] = mini_atoi(argv[4]);
else
table->nb_of_meal = 2147483647;
table->param[MEALS_AMOUNT] = 2147483647;
table->time = gettime();
table->philos = malloc(sizeof(t_philo) * table->param[PHILO_AMOUNT]);
//memset(table->philos, 0, sizeof(t_philo) * table->nb_of_philos);
return (table);
}
size_t gettime(void)
int gettime(void)
{
static struct timeval time = {0,0};
static struct timeval starttime = {0,0};
struct timeval actualtime;
size_t ret;
if(time.tv_sec == 0 && time.tv_usec == 0)
gettimeofday(&time, NULL);
if(starttime.tv_sec == 0 && starttime.tv_usec == 0)
gettimeofday(&starttime, NULL);
gettimeofday(&actualtime, NULL);
ret = (size_t)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\
- (time.tv_sec * 1000 + time.tv_usec / 1000));
return (ret);
return ((int)((actualtime.tv_sec * 1000 + actualtime.tv_usec / 1000)\
- (starttime.tv_sec * 1000 + starttime.tv_usec / 1000)));
}

Loading…
Cancel
Save