1

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.

interesting
  • 149
  • 1
  • 10
  • 1
    To make sure it doesn't exist already with a state/value. – जलजनक Apr 06 '22 at 10:30
  • 1
    well, how would it be possible for a semaphore with the same name to exist before. does semaphores persist between program runs?? cause otherwise no one created these semaphores other than me?? because when i run the program commenting these ```unlink``` part out , it stops working , which means according to what you said they exists already ? how is that , do semaphores persists between program runs?? – interesting Apr 06 '22 at 10:32
  • 2
    [POSIX name semaphore does not release after process exits](https://stackoverflow.com/questions/34756406/posix-name-semaphore-does-not-release-after-process-exits) – Some programmer dude Apr 06 '22 at 11:41
  • Yes, named semaphores *do* persist between runs, unless they are explicitly removed. The same is not true for anonymous ones. – John Bollinger Apr 07 '22 at 16:01
  • Cheeky answer: "because we used a named semaphore when we really wanted an anonymous one." – John Bollinger Apr 07 '22 at 18:47

2 Answers2

1

why we delete a semaphore before initialize it even though the initialization might be the first one.

Because named semaphores such as are created by sem_open() have kernel persistence. That's part of the point of naming them. They live until either

  • they are unlinked and no processes remain that have them open, OR
  • the system is shut down.

If you want to be sure you are creating a new (named) semaphore instead of opening an existing one then unlinking the name before the sem_open() call makes that pretty likely. If you want to be certain, however, then you should also add O_EXCL to the flags. That will cause sem_open() to fail if a semaphore with the given name already exists, such as if another process created one between the sem_unlink() and the sem_open().

Note also that semaphore names should begin with a / (see sem_overview(7)). Some libraries don't enforce that, but you should not gratuitously introduce a portability issue by omitting it.

On the other hand, if you want a semaphore for a single process only, whose lifetime is bounded by its host process, then you are just making life hard on yourself by using named semaphores. You would be better off in that case with anonymous ones, declared as objects of type sem_t and initialized via the sem_init() function instead of sem_open(). That would moot all the persistence and unlinking questions.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Note: provided that they are initialized appropriately, anonymous semaphores can usefully be shared among multiple processes. Details are outside the scope of this answer; I just want to be clear that the use case presented above is not the only one for anonymous semaphores. – John Bollinger Apr 08 '22 at 13:59
1

because semaphore isn't related with process , it created in the system so when you process ended all the created semaphore will stay there soo every time you want to create with sem_open() a semaphore you should unlink it to be sure there is no semaphore with the same name or just add O_EXCL to your flags to in this case sem_open() will fail if there is already sem with the same name

aerrajiy
  • 21
  • 2