semaphores->forks = sem_open(FORKS_SEM, O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR, count);
semaphores->fed = sem_open(WELL_FED_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, count);
+ S_IRUSR | S_IWUSR, 0);
semaphores->death = sem_open(DEATH_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, count);
+ S_IRUSR | S_IWUSR, 0);
semaphores->term = sem_open(TERM_SEM, O_CREAT | O_EXCL,
- S_IRUSR | S_IWUSR, 1);
+ S_IRUSR | S_IWUSR, 0);
+ semaphores->end = sem_open(END_SEM, O_CREAT | O_EXCL,
+ S_IRUSR | S_IWUSR, 0);
if (semaphores->forks == SEM_FAILED || semaphores->fed == SEM_FAILED
- || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED)
+ || semaphores->death == SEM_FAILED || semaphores->term == SEM_FAILED
+ || semaphores->end == SEM_FAILED)
return (1);
return (0);
}
int init(unsigned int count, pid_t **philo_pids, t_sems *semaphores)
{
- size_t i;
-
*philo_pids = malloc(count * sizeof(**philo_pids));
if (!*philo_pids)
return (1);
if (open_semaphores(count, semaphores))
return (1);
- i = 0;
- while (i < count)
- {
- (*philo_pids)[i] = 0;
- sem_wait(semaphores->death);
- sem_wait(semaphores->fed);
- ++i;
- }
- sem_wait(semaphores->term);
return (0);
}
sem_close(semaphores->fed);
sem_close(semaphores->death);
sem_close(semaphores->term);
+ sem_close(semaphores->end);
return ;
}
sem_unlink(WELL_FED_SEM);
sem_unlink(DEATH_SEM);
sem_unlink(TERM_SEM);
+ sem_unlink(END_SEM);
free(philo_pids);
return ;
}
# define WELL_FED_SEM "philosophers_fed"
# define DEATH_SEM "philosophers_death"
# define TERM_SEM "philosophers_terminal"
+# define END_SEM "philosophers_end"
// The .start member holds the result of gettimeofday
// Every other member that holds time info counts
sem_t *fed;
sem_t *death;
sem_t *term;
+ sem_t *end;
} t_sems;
// As mutex is not available in bonus, the idea is to have