main.c
 1  /* ************************************************************************** */
 2  /*                                                                            */
 3  /*                                                        :::      ::::::::   */
 4  /*   main.c                                             :+:      :+:    :+:   */
 5  /*                                                    +:+ +:+         +:+     */
 6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
 7  /*                                                +#+#+#+#+#+   +#+           */
 8  /*   Created: 2023/04/18 18:14:02 by gychoi            #+#    #+#             */
 9  /*   Updated: 2023/06/01 22:56:43 by gychoi           ###   ########.fr       */
10  /*                                                                            */
11  /* ************************************************************************** */
12  
13  #include "philo.h"
14  
15  static int	_abort(char *string, t_share *share, t_philo **philos)
16  {
17  	free(share->forks);
18  	free(share->fork_locks);
19  	free(*philos);
20  	printf("\x1b[31m" "Abort: " "\x1b[0m");
21  	printf("%s\n", string);
22  	return (1);
23  }
24  
25  int	main(int argc, char **argv)
26  {
27  	t_args	args;
28  	t_share	share;
29  	t_philo	*philos;
30  
31  	if (argc < 5 || argc > 6 || valid_input(argv) == FALSE)
32  		return (printf(USAGE1 USAGE2) * 0 + 1);
33  	init_struct_args(&args, argv);
34  	if (init_all_malloc(&share, &philos, args.philo_num) == FALSE)
35  		return (_abort("initialize malloc failed", &share, &philos));
36  	if (init_all_mutex(&share, args.philo_num) == FALSE)
37  		return (_abort("initialize mutex failed", &share, &philos));
38  	init_struct_share(&share, args);
39  	init_struct_philo(&philos, &share, args);
40  	if (simulate(philos, &share) == FALSE)
41  		return (_abort("execute simulation failed", &share, &philos));
42  	free(share.forks);
43  	free(share.fork_locks);
44  	free(philos);
45  	return (0);
46  }