diff --git a/philo/Makefile b/philo/Makefile index 12c482a..e8a3fd4 100644 --- a/philo/Makefile +++ b/philo/Makefile @@ -17,7 +17,16 @@ INC_PATH = inc OBJ_PATH = obj VPATH = src:src/utils -SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) +SOURCES = get_time.c ph_atoi.c print_rules.c print_status.c \ + philo_routine.c philo_sleep.c set_death.c \ + synchronize_philos.c take_forks.c think_time.c \ + unlock_forks.c check_philos.c create_mutexes.c \ + create_philos.c create_threads.c free_philos.c get_death.c \ + get_finished.c monitor_routine.c parse_arguments.c \ + philo_die.c print_usage.c set_finished.c \ + create_philo_mutexes.c destroy_mutexes.c ft_min.c \ + join_threads.c main.c one_philo.c philo_eat.c\ + OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) DEPENDS = ${OBJECTS:.o=.d} @@ -25,7 +34,7 @@ CC = cc RM = rm -rf INCLUDES = -I./$(INC_PATH) -CFLAGS = -Wall -Wextra -Werror -MMD -g +CFLAGS = -Wall -Wextra -Werror -MMD -g UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) LDLIBS := -pthread @@ -53,3 +62,4 @@ fclean: clean re: fclean all .PHONY: all clean fclean re + diff --git a/philo/inc/philo.h b/philo/inc/philo.h index a58be48..938c24c 100644 --- a/philo/inc/philo.h +++ b/philo/inc/philo.h @@ -91,5 +91,9 @@ void synchronize_philos(t_philo *philo); int philo_sleep(t_philo *philo, int time); void unlock_forks(t_philo *philo); void take_forks(t_philo *philo); +int think_time(t_rules *rules); +int create_philo_mutexes(t_philo *philo); +void one_philo(t_philo *philo); +void print_usage(void); #endif diff --git a/philo/src/check_philos.c b/philo/src/check_philos.c index d3bc241..9555b0a 100644 --- a/philo/src/check_philos.c +++ b/philo/src/check_philos.c @@ -27,6 +27,7 @@ int check_philos(t_rules *rules) pthread_mutex_lock(rules->philos[i].last_meal_lock); if (get_time() - rules->philos[i].last_meal >= rules->time_to_die) { + printf("FUCK"); philo_die(&rules->philos[i]); set_finished(rules); pthread_mutex_unlock(rules->philos[i].last_meal_lock); diff --git a/philo/src/print_usage.c b/philo/src/print_usage.c index a237475..0a53518 100644 --- a/philo/src/print_usage.c +++ b/philo/src/print_usage.c @@ -9,6 +9,7 @@ /* Updated: 2025/01/28 17:29:44 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ +#include "philo.h" void print_usage(void) { diff --git a/tester.sh b/tester.sh new file mode 100755 index 0000000..317d1d9 --- /dev/null +++ b/tester.sh @@ -0,0 +1,442 @@ +#!/bin/zsh + +echo '\e[91m _____ _ _ _ _ _____ _ +| _ | |_|_| |___ ___ ___ ___| |_ ___ ___ ___ |_ _|__ ___| |_ ___ ___ +| __| | | | . |_ -| . | . | | -_| _|_ -| | || -_|_ -| _| -_| _| +|__| |_|_|_|_|___|___|___| _|_|_|___|_| |___| |_||___|___|_| |___|_| + |_| \e[0m\n' + +if [ "$#" -ne 2 ]; then + echo "Usage: start.sh " + echo "\tType 0: test philo, and philo_bonus" + echo "\tType 1: test philo only" + echo "\tType 2: test philo_bonus only" + + exit +fi + +if [ "$2" -gt 2 -o "$2" -lt 0 ]; then + echo "[Error]: Wrong Arguments" + exit +fi + +echo "\e[92m[+] Given Folder: $1" +echo "[+] Test Type: $2\e[0m\n" +echo "\e[94m[+] In Case of a failed test, please check ./errors_log file for more information\e[0m\n" + +error_log () +{ + echo "[$1-$2]: $3" >> ./errors_log +} + +test_one () +{ + ("$2/$1/$1" 4 310 200 100 > "./log_$1")& + sleep 5 + pkill $1 + output=$(grep died -m 1 "./log_$1" | awk '{print $NF}') + if [ "$output" = "died" ];then + echo "\e[92m[+] Test #1 Succeeded !\e[0m" + else + echo "\e[91m[+] Test #1 Failed !\e[0m" + error_log $1 "Test #1" "Given 4 310 200 100 arguments to $1, a philosopher should die !" + fi + rm -rf "./log_$1" +} + +test_two () +{ + echo "\e[94m[+] Test #2: Executing your program for 180 second, please wait...\e[0m" + ("$2/$1/$1" 4 410 200 200 > /dev/null)& + i=1 + error=0 + while [ $i -lt 180 ];do + printf "\r[%d...]" $i + pgrep $1 > /dev/null + if [ "$?" -ne 0 ];then + echo "\r\e[91m[+] Test #2 Failed\e[0m" + error_log $1 "Test #2" "Given 4 410 200 200 arguments to $1, no philosopher should die !" + error=1 + break + fi + sleep 1 + i=$(( $i + 1 )) + done + sleep 1 + if [ $error -eq 0 ];then + pkill $1 + echo "\r\e[92m[+] Test #2 Succeeded\e[0m" + fi +} + +test_three () +{ + echo "\e[94m[+] Test #3: Executing your program for 180 second, please wait...\e[0m" + ("$2/$1/$1" 5 800 200 200 > /dev/null)& + i=1 + error=0 + while [ $i -lt 180 ];do + printf "\r[%d...]" $i + pgrep $1 > /dev/null + if [ "$?" -ne 0 ];then + echo "\r\e[91m[+] Test #3 Failed\e[0m" + error_log $1 "Test #3" "Given 4 800 200 200 arguments to $1, no philosopher should die !" + error=1 + break + fi + sleep 1 + i=$(( $i + 1 )) + done + sleep 1 + if [ $error -eq 0 ];then + pkill $1 + echo "\r\e[92m[+] Test #3 Succeeded\e[0m" + fi +} + +test_four () +{ + ("$2/$1/$1" 4 410 200 200 $3 > "./log_$1")& + sleep 10 + pgrep $1 > /dev/null + if [ "$?" -eq 1 ];then + lines=$(grep eating "./log_$1" | wc -l) + if [ $lines -ge $4 ];then + echo "\t\e[92m[+] Test #4-$5 Succeeded\e[0m" + else + echo "\t\e[91m[+] Test #4-$5 Failed\e[0m" + error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should only be stopped if each philosopher ate at least $3 times !" + fi + else + echo "\t\e[91m[+] Test #4-$5 Failed\e[0m" + error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should stop !" + pkill $1 + fi + rm -rf "./log_$1" +} + +test_five () +{ + echo "\e[94m[+] Test #5 on progress, please wait...\e[0m" + i=1 + t=0 + error=0 + while [ $i -le 10 ];do + ("$2/$1/$1" 2 60 60 60 > "./log_$1")& + sleep 2 + pgrep $1 > /dev/null + if [ "$?" -eq 1 ];then + printf "\r[%d/10]" $i + tmp=$(grep died -m 1 "./log_$1" | awk '{print $1}' | sed 's/[^0-9]*//g') + if [ $i -gt 1 ];then + x=$(expr $tmp - $t) + x=${x#-} + if [ $x -gt 10 ];then + printf "\r\e[91m[+] Test #5 Failed\e[0m\n" + error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, the time difference of each death shouldn't be bigger than 10ms !" + error=1 + break + fi + else + t=$tmp + fi + else + printf "\r\e[91m[+] Test #5 Failed\e[0m\n" + error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, a philosopher should die !" + pkill $1 + break + fi#!/bin/zsh + +echo '\e[91m _____ _ _ _ _ _____ _ +| _ | |_|_| |___ ___ ___ ___| |_ ___ ___ ___ |_ _|__ ___| |_ ___ ___ +| __| | | | . |_ -| . | . | | -_| _|_ -| | || -_|_ -| _| -_| _| +|__| |_|_|_|_|___|___|___| _|_|_|___|_| |___| |_||___|___|_| |___|_| + |_| \e[0m\n' + +if [ "$#" -ne 2 ]; then + echo "Usage: start.sh " + echo "\tType 0: test philo, and philo_bonus" + echo "\tType 1: test philo only" + echo "\tType 2: test philo_bonus only" + + exit +fi + +if [ "$2" -gt 2 -o "$2" -lt 0 ]; then + echo "[Error]: Wrong Arguments" + exit +fi + +echo "\e[92m[+] Given Folder: $1" +echo "[+] Test Type: $2\e[0m\n" +echo "\e[94m[+] In Case of a failed test, please check ./errors_log file for more information\e[0m\n" + +error_log () +{ + echo "[$1-$2]: $3" >> ./errors_log +} + +test_one () +{ + ("$2/$1/$1" 4 310 200 100 > "./log_$1")& + sleep 5 + pkill $1 + output=$(grep died -m 1 "./log_$1" | awk '{print $NF}') + if [ "$output" = "died" ];then + echo "\e[92m[+] Test #1 Succeeded !\e[0m" + else + echo "\e[91m[+] Test #1 Failed !\e[0m" + error_log $1 "Test #1" "Given 4 310 200 100 arguments to $1, a philosopher should die !" + fi + rm -rf "./log_$1" +} + +test_two () +{ + echo "\e[94m[+] Test #2: Executing your program for 180 second, please wait...\e[0m" + ("$2/$1/$1" 4 410 200 200 > /dev/null)& + i=1 + error=0 + while [ $i -lt 180 ];do + printf "\r[%d...]" $i + pgrep $1 > /dev/null + if [ "$?" -ne 0 ];then + echo "\r\e[91m[+] Test #2 Failed\e[0m" + error_log $1 "Test #2" "Given 4 410 200 200 arguments to $1, no philosopher should die !" + error=1 + break + fi + sleep 1 + i=$(( $i + 1 )) + done + sleep 1 + if [ $error -eq 0 ];then + pkill $1 + echo "\r\e[92m[+] Test #2 Succeeded\e[0m" + fi +} + +test_three () +{ + echo "\e[94m[+] Test #3: Executing your program for 180 second, please wait...\e[0m" + ("$2/$1/$1" 5 800 200 200 > /dev/null)& + i=1 + error=0 + while [ $i -lt 180 ];do + printf "\r[%d...]" $i + pgrep $1 > /dev/null + if [ "$?" -ne 0 ];then + echo "\r\e[91m[+] Test #3 Failed\e[0m" + error_log $1 "Test #3" "Given 4 800 200 200 arguments to $1, no philosopher should die !" + error=1 + break + fi + sleep 1 + i=$(( $i + 1 )) + done + sleep 1 + if [ $error -eq 0 ];then + pkill $1 + echo "\r\e[92m[+] Test #3 Succeeded\e[0m" + fi +} + +test_four () +{ + ("$2/$1/$1" 4 410 200 200 $3 > "./log_$1")& + sleep 10 + pgrep $1 > /dev/null + if [ "$?" -eq 1 ];then + lines=$(grep eating "./log_$1" | wc -l) + if [ $lines -ge $4 ];then + echo "\t\e[92m[+] Test #4-$5 Succeeded\e[0m" + else + echo "\t\e[91m[+] Test #4-$5 Failed\e[0m" + error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should only be stopped if each philosopher ate at least $3 times !" + fi + else + echo "\t\e[91m[+] Test #4-$5 Failed\e[0m" + error_log $1 "Test #4" "Given 4 410 200 200 $3 arguments to $1, $1 should stop !" + pkill $1 + fi + rm -rf "./log_$1" +} + +test_five () +{ + echo "\e[94m[+] Test #5 on progress, please wait...\e[0m" + i=1 + t=0 + error=0 + while [ $i -le 10 ];do + ("$2/$1/$1" 2 60 60 60 > "./log_$1")& + sleep 2 + pgrep $1 > /dev/null + if [ "$?" -eq 1 ];then + printf "\r[%d/10]" $i + tmp=$(grep died -m 1 "./log_$1" | awk '{print $1}' | sed 's/[^0-9]*//g') + if [ $i -gt 1 ];then + x=$(expr $tmp - $t) + x=${x#-} + if [ $x -gt 10 ];then + printf "\r\e[91m[+] Test #5 Failed\e[0m\n" + error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, the time difference of each death shouldn't be bigger than 10ms !" + error=1 + break + fi + else + t=$tmp + fi + else + printf "\r\e[91m[+] Test #5 Failed\e[0m\n" + error_log $1 "Test #5" "Given 2 60 60 60 arguments to $1, a philosopher should die !" + pkill $1 + break + fi + i=$(( $i + 1 )) + done + + if [ $error -eq 0 ];then + echo "\r\e[92m[+] Test #5 Succeeded\e[0m" + fi +} + +test_six () +{ + ("$2/$1/$1" 10 410 200 200 > /dev/null)& + sleep 2 + forks=$(pgrep $1 | wc -l) + if [ "$forks" -eq 11 ];then + printf "\r\e[92m[+] Test #6 Succeeded\e[0m\n" + else + printf "\r\e[91m[+] Test #6 Failed\e[0m\n" + error_log $1 "Test #6" "Given 10 410 200 200 arguments to $1, 10 processes should be forked, each process for a philosopher !" + fi + pkill $1 +} + +if [ "$2" -eq 1 -o "$2" -eq 0 ];then + + echo "[============[Testing philo]==============]\n" + + target="philo" + make -C "$1/$target" > /dev/null + + if [ "$?" -ne 0 ];then + echo "\n[+] There's a problem while compiling $target, please recheck your inputs" + exit + fi + + test_one $target $1 + + test_two $target $1 + + test_three $target $1 + + echo "\e[94m[+] Test #4 on progress, please wait...\e[0m" + test_four $target $1 7 28 1 + test_four $target $1 10 40 2 + test_four $target $1 12 48 3 + test_four $target $1 15 60 4 + + test_five $target $1 + rm -rf "./log_$target" +fi + +if [ "$2" -eq 2 -o "$2" -eq 0 ];then + + echo "\n[============[Testing philo_bonus]==============]\n" + + target="philo_bonus" + make -C "$1/$target" > /dev/null + + if [ "$?" -ne 0 ];then + echo "\n[+] There's a problem while compiling $target, please recheck your inputs" + exit + fi + + test_one $target $1 + + test_two $target $1 + + test_three $target $1 + + echo "\e[94m[+] Test #4 on progress, please wait...\e[0m" + test_four $target $1 7 28 1 + test_four $target $1 10 40 2 + test_four $target $1 12 48 3 + test_four $target $1 15 60 4 + + test_five $target $1 + + test_six $target $1 + rm -rf "./log_$target" +fi + + forks=$(pgrep $1 | wc -l) + if [ "$forks" -eq 11 ];then + printf "\r\e[92m[+] Test #6 Succeeded\e[0m\n" + else + printf "\r\e[91m[+] Test #6 Failed\e[0m\n" + error_log $1 "Test #6" "Given 10 410 200 200 arguments to $1, 10 processes should be forked, each process for a philosopher !" + fi + pkill $1 +} + +if [ "$2" -eq 1 -o "$2" -eq 0 ];then + + echo "[============[Testing philo]==============]\n" + + target="philo" + make -C "$1/$target" > /dev/null + + if [ "$?" -ne 0 ];then + echo "\n[+] There's a problem while compiling $target, please recheck your inputs" + exit + fi + + test_one $target $1 + + test_two $target $1 + + test_three $target $1 + + echo "\e[94m[+] Test #4 on progress, please wait...\e[0m" + test_four $target $1 7 28 1 + test_four $target $1 10 40 2 + test_four $target $1 12 48 3 + test_four $target $1 15 60 4 + + test_five $target $1 + rm -rf "./log_$target" +fi + +if [ "$2" -eq 2 -o "$2" -eq 0 ];then + + echo "\n[============[Testing philo_bonus]==============]\n" + + target="philo_bonus" + make -C "$1/$target" > /dev/null + + if [ "$?" -ne 0 ];then + echo "\n[+] There's a problem while compiling $target, please recheck your inputs" + exit + fi + + test_one $target $1 + + test_two $target $1 + + test_three $target $1 + + echo "\e[94m[+] Test #4 on progress, please wait...\e[0m" + test_four $target $1 7 28 1 + test_four $target $1 10 40 2 + test_four $target $1 12 48 3 + test_four $target $1 15 60 4 + + test_five $target $1 + + test_six $target $1 + rm -rf "./log_$target" +fi