This commit is contained in:
Matthos Denys 2021-05-21 00:48:58 +03:00
parent 4c5f6ad3e3
commit 421ace00b3
22 changed files with 172 additions and 329 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
.DS_Store

View File

@ -1 +0,0 @@
{"buildTargets":[],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}}

7
.vscode/dryrun.log vendored
View File

@ -1,7 +0,0 @@
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.

16
.vscode/launch.json vendored
View File

@ -1,16 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/philo_three/philo_three",
"args": ["4", "310", "200", "200"],
"cwd": "${workspaceFolder}"
}
]
}

View File

@ -1,4 +0,0 @@
{
"makefile.extensionOutputFolder": "./.vscode",
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools"
}

228
.vscode/targets.log vendored
View File

@ -1,228 +0,0 @@
make all --print-data-base --no-builtin-variables --no-builtin-rules --question
# GNU Make 3.81
# Copyright (C) 2006 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
# This program built for i386-apple-darwin11.3.0
make: *** No rule to make target `all'. Stop.
# Make data base, printed on Sun May 16 15:43:05 2021
# Variables
# automatic
<D = $(patsubst %/,%,$(dir $<))
# automatic
?F = $(notdir $?)
# environment
VSCODE_LOG_NATIVE = false
# automatic
?D = $(patsubst %/,%,$(dir $?))
# automatic
@D = $(patsubst %/,%,$(dir $@))
# automatic
@F = $(notdir $@)
# makefile
CURDIR := /Users/mdenys/Desktop/projects/philosophers
# makefile
SHELL = /bin/sh
# environment
VSCODE_NLS_CONFIG = {"locale":"en-us","availableLanguages":{},"_languagePackSupport":true}
# environment
_ = /usr/bin/make
# makefile
MAKEFILE_LIST :=
# environment
VSCODE_NODE_CACHED_DATA_DIR = /Users/mdenys/Library/Application Support/Code/CachedData/054a9295330880ed74ceaedda236253b4f39a335
# environment
VSCODE_VERBOSE_LOGGING = true
# environment
VSCODE_IPC_HOOK_EXTHOST = /var/folders/zz/zyxvpxvq6csfxvn_n003vhs000yw68/T/vscode-ipc-9805dd75-8740-4f27-af98-b26ffd7c1ffb.sock
# environment
VSCODE_CWD = /
# environment
Apple_PubSub_Socket_Render = /private/tmp/com.apple.launchd.mReJxtxopP/Render
# environment
PATH = /Users/mdenys/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki
# environment
VSCODE_LOG_STACK = false
# environment
ELECTRON_RUN_AS_NODE = 1
# default
.FEATURES := target-specific order-only second-expansion else-if archives jobserver check-symlink
# environment
SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.XfddAWpCNO/Listeners
# automatic
%F = $(notdir $%)
# environment
HOMEBREW_TEMP = /tmp/mdenys/Homebrew/Temp
# environment
VSCODE_PIPE_LOGGING = true
# environment
PWD = /Users/mdenys/Desktop/projects/philosophers
# environment
ORIGINAL_XDG_CURRENT_DESKTOP = undefined
# environment
VSCODE_AMD_ENTRYPOINT = vs/workbench/services/extensions/node/extensionHostProcess
# environment
HOME = /Users/mdenys
# default
MAKEFILEPATH := /Applications/Xcode.app/Contents/Developer/Makefiles
# environment
LOGNAME = mdenys
# environment
APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL = true
# environment
VSCODE_HANDLES_UNCAUGHT_ERRORS = true
# automatic
^D = $(patsubst %/,%,$(dir $^))
# environment
XPC_FLAGS = 0x0
# default
MAKE = $(MAKE_COMMAND)
# default
MAKECMDGOALS := all
# environment
HOMEBREW_CACHE = /tmp/mdenys/Homebrew/Caches
# environment
SHLVL = 1
# default
MAKE_VERSION := 3.81
# environment
USER = mdenys
# makefile
.DEFAULT_GOAL :=
# automatic
%D = $(patsubst %/,%,$(dir $%))
# default
MAKE_COMMAND := /Applications/Xcode.app/Contents/Developer/usr/bin/make
# default
.VARIABLES :=
# environment
TMPDIR = /var/folders/zz/zyxvpxvq6csfxvn_n003vhs000yw68/T/
# automatic
*F = $(notdir $*)
# environment
VSCODE_IPC_HOOK = /Users/mdenys/Library/Application Support/Code/1.56.2-main.sock
# makefile
MAKEFLAGS = Rrqp
# environment
MFLAGS = -Rrqp
# automatic
*D = $(patsubst %/,%,$(dir $*))
# environment
XPC_SERVICE_NAME = com.microsoft.VSCode.6236
# automatic
+D = $(patsubst %/,%,$(dir $+))
# automatic
+F = $(notdir $+)
# environment
__CF_USER_TEXT_ENCODING = 0xF70C8:0x0:0x0
# default
MAKEFILES :=
# automatic
<F = $(notdir $<)
# environment
LC_ALL = C
# automatic
^F = $(notdir $^)
# default
SUFFIXES :=
# default
.INCLUDE_DIRS = /usr/local/include
# environment
MAKELEVEL := 0
# environment
LANG = C
# environment
VSCODE_PID = 27414
# variable set hash-table stats:
# Load=63/1024=6%, Rehash=0, Collisions=1/84=1%
# Pattern-specific Variable Values
# No pattern-specific variable values.
# Directories
# . (device 16777227, inode 5034668): 9 files, no impossibilities.
# 9 files, no impossibilities in 1 directories.
# Implicit Rules
# No implicit rules.
# Files
# Not a target:
all:
# Command-line target.
# Implicit rule search has been done.
# File does not exist.
# File has not been updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%
# Not a target:
.SUFFIXES:
# Implicit rule search has not been done.
# Modification time never checked.
# File has not been updated.
# Not a target:
Makefile:
# A default, MAKEFILES, or -include/sinclude makefile.
# Implicit rule search has been done.
# File does not exist.
# File has been updated.
# Failed to be updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%
# Not a target:
makefile:
# A default, MAKEFILES, or -include/sinclude makefile.
# Implicit rule search has been done.
# File does not exist.
# File has been updated.
# Failed to be updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%
# Not a target:
.DEFAULT:
# Implicit rule search has not been done.
# Modification time never checked.
# File has not been updated.
# Not a target:
GNUmakefile:
# A default, MAKEFILES, or -include/sinclude makefile.
# Implicit rule search has been done.
# File does not exist.
# File has been updated.
# Failed to be updated.
# variable set hash-table stats:
# Load=0/32=0%, Rehash=0, Collisions=0/0=0%
# files hash-table stats:
# Load=6/1024=1%, Rehash=0, Collisions=0/17=0%
# VPATH Search Paths
# No `vpath' search paths.
# No general (`VPATH' variable) search path.
# # of strings in strcache: 0
# # of strcache buffers: 0
# 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 15:43:05 2021

View File

@ -1,16 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/philo_one",
"args": ["5", "800", "200", "200", "2"],
"cwd": "${workspaceFolder}"
}
]
}

BIN
philo_three/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
# By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/05/20 12:13:10 by mdenys #+# #+# #
# Updated: 2021/05/20 20:45:52 by mdenys ### ########.fr #
# Updated: 2021/05/20 20:51:59 by mdenys ### ########.fr #
# #
# **************************************************************************** #
@ -14,7 +14,7 @@
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 utilc2.c observer.c ft_exit.c
ft_strjoin.c utilc2.c observer.c ft_exit.c pkill_all.c obs_for_phil.c
SRC_DIR = ./srcs/

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */
/* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */
/* Updated: 2021/05/20 23:44:18 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@
# include <sys/time.h>
# include <semaphore.h>
# include <stdio.h>
# include <signal.h>
# include <string.h>
# define _SEM_NAME_FORKS "pSemForks"
# define _SEM_WRITE_LOCK "write"
@ -36,9 +37,11 @@ typedef struct s_all
time_t start_time;
t_philo_data *data_philo;
sem_t *write;
sem_t *bullet;
sem_t *bullets;
int smert;
int opt;
sem_t *bucket;
sem_t *bucket;
} t_all;
typedef struct s_philo
@ -55,6 +58,7 @@ typedef struct s_philo
t_all *all_struct;
int is_eat;
sem_t *eat_sem;
pid_t pid;
char *name;
} t_philo;
@ -77,7 +81,7 @@ void *run_trhead(t_philo *philo);
int prepare_resources(t_all *all);
int check_all_run(t_all *all);
void run_simulation(t_all *all);
void run_all_phils(t_all *all);
void *run_all_phils(t_all *all);
void messages(t_all *all, t_philo *philo, char *str);
void cycle(t_philo *philo);
void ft_msleep(time_t interval_ms);
@ -92,4 +96,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size);
int ft_strlen(char *str);
void get_data_for_philo(t_all *all);
int run_observer(t_all *all);
void pkill_all(t_all *all);
void *obs_phil(t_philo *philo);
int run_observer_return(t_all *all, int i);
#endif

BIN
philo_three/srcs/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */
/* Updated: 2021/05/20 22:29:36 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,13 +16,6 @@ void *run_trhead(t_philo *philo)
{
while (1)
{
if (philo->all_struct->opt)
{
if (philo->count_eat == philo->all_struct->etarate_meal)
{
return (NULL);
}
}
cycle(philo);
}
return (NULL);

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* obs_for_phil.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */
/* Updated: 2021/05/20 23:46:13 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo_header.h"
void *obs_phil(t_philo *philo)
{
time_t time_eat;
while (1)
{
sem_wait(philo->eat_sem);
time_eat = (ft_get_time() - \
philo->all_struct->start_time) - philo->last_eat;
if (time_eat > philo->all_struct->time_to_die)
{
run_observer_return(philo->all_struct, philo->nbr_philo);
break ;
}
else if (philo->all_struct->opt)
{
if (philo->count_eat == philo->all_struct->etarate_meal)
{
sem_post(philo->all_struct->bullets);
while (1)
;
}
}
sem_post(philo->eat_sem);
}
sem_post(philo->all_struct->bullet);
return (NULL);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
/* Updated: 2021/05/20 16:10:26 by mdenys ### ########.fr */
/* Updated: 2021/05/20 21:12:17 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,8 +17,7 @@ int run_observer_return(t_all *all, int i)
sem_wait(all->write);
printf("%ld %d died\n", \
ft_get_time() - all->start_time, i + 1);
all->smert = 42;
return (2);
return (1);
}
int run_observer(t_all *all)
@ -51,7 +50,5 @@ int runner(t_all *all)
prepare_resources(all);
all->start_time = ft_get_time();
run_all_phils(all);
if (!run_observer(all))
return (0);
return (1);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pkill_all.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:48 by mdenys #+# #+# */
/* Updated: 2021/05/20 23:04:26 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/philo_header.h"
void pkill_all(t_all *all)
{
int i;
i = 0;
while (i < all->nbr_phils)
kill(all->data_philo->philors[i++].pid, SIGKILL);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
/* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */
/* Updated: 2021/05/21 00:44:52 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,6 +28,7 @@ void get_data_f_philo(t_all *all)
all->data_philo->philors[i].name = sem_set_name("philo", i);
all->data_philo->philors[i].eat_sem = \
reopen_sem(all->data_philo->philors[i].name, 1);
sem_wait(all->bullets);
i++;
}
}
@ -41,13 +42,39 @@ int prepare_resources(t_all *all)
all->data_philo->sems = reopen_sem(_SEM_NAME_FORKS, all->nbr_phils);
all->write = reopen_sem(_SEM_WRITE_LOCK, 1);
all->bucket = reopen_sem("bucket", 1);
all->bullet = reopen_sem("bullet", 1);
all->bullets = reopen_sem("bullets", all->nbr_phils);
sem_wait(all->bullet);
all->data_philo->philors = (t_philo *)ft_calloc(all->nbr_phils \
+ 1, sizeof(t_philo));
get_data_f_philo(all);
return (0);
}
void run_all_phils(t_all *all)
int checker(t_all *all)
{
int i;
i = 0;
if (all->opt)
{
while (i < all->nbr_phils)
{
sem_wait(all->bullets);
i++;
}
pkill_all(all);
return (3);
}
else
{
sem_wait(all->bullet);
pkill_all(all);
}
return (0);
}
void *run_all_phils(t_all *all)
{
void *philo;
int i;
@ -55,12 +82,21 @@ void run_all_phils(t_all *all)
i = 0;
while (i < all->nbr_phils)
{
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);
all->data_philo->philors[i].pid = fork();
if (all->data_philo->philors[i].pid == 0)
{
philo = (void *)(&all->data_philo->philors[i]);
if (pthread_create(&all->data_philo->philors[i].thread, NULL, \
(void *)obs_phil, philo) == -1)
printf("I can't create a thread t%d\n", i);
else
pthread_detach(all->data_philo->philors[i].thread);
run_trhead(philo);
}
else
pthread_detach(all->data_philo->philors[i].thread);
i++;
i++;
}
if (checker(all))
return (0);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:33 by mdenys #+# #+# */
/* Updated: 2021/05/20 14:01:48 by mdenys ### ########.fr */
/* Updated: 2021/05/21 00:23:40 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,9 +36,9 @@ typedef struct s_all
time_t start_time;
t_philo_data *data_philo;
sem_t *write;
int smert;
int final;
int opt;
sem_t *bucket;
sem_t *bucket;
} t_all;
typedef struct s_philo
@ -56,6 +56,8 @@ typedef struct s_philo
int is_eat;
sem_t *eat_sem;
char *name;
char *end_meal_name;
sem_t *end_meal;
} t_philo;
typedef struct s_philo_data

BIN
philo_two/srcs/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:43 by mdenys #+# #+# */
/* Updated: 2021/05/20 16:18:47 by mdenys ### ########.fr */
/* Updated: 2021/05/21 00:01:15 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,7 +20,9 @@ void *run_trhead(t_philo *philo)
{
if (philo->count_eat == philo->all_struct->etarate_meal)
{
return (NULL);
philo->died = 42;
while (1)
;
}
}
cycle(philo);

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:50 by mdenys #+# #+# */
/* Updated: 2021/05/20 16:10:26 by mdenys ### ########.fr */
/* Updated: 2021/05/21 00:36:27 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,32 +17,43 @@ int run_observer_return(t_all *all, int i)
sem_wait(all->write);
printf("%ld %d died\n", \
ft_get_time() - all->start_time, i + 1);
all->smert = 42;
all->final = 42;
return (2);
}
int check_all_philors(t_all *all)
{
int i;
int j;
time_t time_eat;
j = 0;
i = 0;
while (i < all->nbr_phils)
{
sem_wait(all->data_philo->philors[i].eat_sem);
time_eat = (ft_get_time() - \
all->start_time) - all->data_philo->philors[i].last_eat;
if (all->data_philo->philors[i].died == 42)
{
j++;
if (j == all->nbr_phils)
return (ft_exit(all, 2));
}
if (time_eat > all->time_to_die)
return (run_observer_return(all, i));
sem_post(all->data_philo->philors[i].eat_sem);
if (all->final == 42)
return (ft_exit(all, 2));
i++;
}
return (0);
}
int run_observer(t_all *all)
{
int i;
time_t time_eat;
i = 0;
while (1)
{
i = 0;
while (i < all->nbr_phils)
{
sem_wait(all->data_philo->philors[i].eat_sem);
time_eat = (ft_get_time() - \
all->start_time) - all->data_philo->philors[i].last_eat;
if (time_eat > all->time_to_die)
return (run_observer_return(all, i));
sem_post(all->data_philo->philors[i].eat_sem);
if (all->smert == 42)
return (ft_exit(all, 2));
i++;
}
}
while (check_all_philors(all) == 0)
;
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:56 by mdenys #+# #+# */
/* Updated: 2021/05/20 14:11:43 by mdenys ### ########.fr */
/* Updated: 2021/05/21 00:23:40 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,7 +43,7 @@ int check_param(t_all *all, int optional)
int parse_param(t_all *all, int optional, char **argv)
{
all->smert = 0;
all->final = 0;
if (optional)
{
all->nbr_phils = ft_atoi(argv[1]);

View File

@ -6,7 +6,7 @@
/* By: mdenys <mdenys@student.21-school.ru> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/20 12:12:58 by mdenys #+# #+# */
/* Updated: 2021/05/20 14:01:31 by mdenys ### ########.fr */
/* Updated: 2021/05/20 23:24:32 by mdenys ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,8 +26,12 @@ void get_data_f_philo(t_all *all)
all->data_philo->philors[i].all_struct = all;
all->data_philo->philors[i].is_eat = 0;
all->data_philo->philors[i].name = sem_set_name("philo", i);
all->data_philo->philors[i].end_meal_name = sem_set_name("end_meal", i);
all->data_philo->philors[i].eat_sem = \
reopen_sem(all->data_philo->philors[i].name, 1);
all->data_philo->philors[i].end_meal = \
reopen_sem(all->data_philo->philors[i].end_meal_name, 1);
sem_wait(all->data_philo->philors[i].end_meal);
i++;
}
}