I was going through a code (a solution to dinning philosophers problem ) and comes the part where the programmer initializes his semaphores, However before he initializes each one , he first unlink it.
int sems_init(t_args *args)
{
sem_unlink("forking");
args->sem_forks = sem_open("forking", O_CREAT, 0644,
args->philo_amount);
if (args->sem_forks == SEM_FAILED)
return (1);
sem_unlink("writing");
args->sem_write = sem_open("writing", O_CREAT, 0644, 1);
if (args->sem_write == SEM_FAILED)
return (1);
sem_unlink("meal");
args->sem_meal = sem_open("meal", O_CREAT, 0644, 1);
if (args->sem_meal == SEM_FAILED)
return (1);
return (0);
}
I googled it and it seems there isn't any resource in the SO community that actually mention this one. or at least i didn't find it.
The semaphores initialization isn't called in a loop and it is not called before this initialization (this is the first time they're initialized in the code). which amplify my confusion, i tried to comment the sem_unlink()
part from the code and it stops working at it should, that is the unlink part is doing something , the question why we delete a semaphore before initialize it even though the initialization might be the first one.