Implement handling of a lone philosopher
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 19 Mar 2024 12:42:49 +0000 (13:42 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 19 Mar 2024 12:42:49 +0000 (13:42 +0100)
philo/main.c

index 0d2d0dbf2749ef1f062e70caec5d1ec3b8229394..0b73accff4e430237212a84dd0e9a6ea060e1d29 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/03/05 12:49:40 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/19 11:55:01 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/19 13:39:27 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -339,6 +339,16 @@ void       check_death(t_philo *philos, t_settings *set)
        }
 }
 
+int    handle_single_philo(t_settings *set)
+{
+       set->program_start = cur_time_sus();
+       thread_print(set, 1, "is thinking");
+       thread_print(set, 1, "has taken a fork");
+       usleep(set->time_to_die);
+       thread_print(set, 1, "died");
+       return (0);
+}
+
 int    main(int argc, char **argv)
 {
        t_settings              settings;
@@ -351,6 +361,8 @@ int main(int argc, char **argv)
                printf("Invalid input arguments.\n");
                return (1);
        }
+       if (settings.philo_count == 1)
+               return (handle_single_philo(&settings));
        threads = malloc(sizeof(pthread_t) * settings.philo_count);
        forks = init_forks(settings.philo_count);
        philos = build_philos(forks, &settings);