为什么我们在初始化信号量之前取消它们的链接?
我正在经历一份代码(解决用餐哲学家问题的解决方案),并且是程序员初始化他的信号量的部分,但是在他初始化每个标志之前,他首先取消了链接。
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);
}
我用谷歌搜索了它,似乎SO社区中没有任何资源实际上提到了这一点。或者至少我没有找到它。
信号量初始化不会在循环中调用,并且在此初始化之前未调用(这是它们在代码中首次初始化的)。这会放大我的困惑,我试图评论sem_unlink()
零件中的零件,它停止工作应该应该,那是Unlink零件正在做某事,问题为什么我们删除A在初始化之前,信号量即使初始化可能是第一个。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为由
sem_open()
创建的命名信号量具有内核持久性。这是命名他们的重点的一部分。他们活着,直到如果要确保要创建一个新的(命名)信号量而不是打开现有的信号量,则在
sem_open()
呼叫之前将名称链接起来。但是,如果您想成为某些,则还应将o_excl
添加到标志中。这将导致sem_open()
如果已经存在具有给定名称的信号量,例如,例如在sem_unlink()
和sem_open之间创建了另一个过程。 ()
。还请注意,信号量的名称应以
/
开头(请参见 sem_overview(7))。有些图书馆没有强制执行此操作,但是您不应该通过省略来介绍便携性问题。另一方面,如果您只想使用单个过程的信号量,该过程的寿命是受主机过程的界限,那么您只是通过使用命名的信号量来使自己变得艰难。在这种情况下,您会更好地使用匿名者,称为
sem_t
的对象,并通过sem_init()
函数初始化,而不是sem_open()。那将使所有的持久性和链接问题解答。
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 eitherIf 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 addO_EXCL
to the flags. That will causesem_open()
to fail if a semaphore with the given name already exists, such as if another process created one between thesem_unlink()
and thesem_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 thesem_init()
function instead ofsem_open()
. That would moot all the persistence and unlinking questions.因为信号量与进程无关,它是在系统中创建的,所以当您进程结束时,所有创建的信号量将保留在那里,因此每次您想使用 sem_open() 创建信号量时,您应该取消链接它确保没有同名的信号量,或者只需将
O_EXCL
添加到您的标志中,在这种情况下sem_open()
将失败如果已经有同名的 sembecause 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 addO_EXCL
to your flags to in this casesem_open()
will fail if there is already sem with the same name