何时调用sem_unlink()? [英] When to call sem_unlink()?

查看:151
本文介绍了何时调用sem_unlink()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Linux API sem_unlink()有点困惑,主要是什么时候或为什么调用它.我在Windows中使用信号灯已经很多年了.在Windows中,一旦关闭命名信号量的最后一个句柄,系统就会删除基础内核对象.但是它似乎在Linux中,您(开发人员)需要通过调用sem_unlink()来删除内核对象.如果不这样做,内核对象将保留在/dev/shm文件夹中.

I'm a little confused by the Linux API sem_unlink(), mainly when or why to call it. I've used semaphores in Windows for many years. In Windows once you close the last handle of a named semaphore the system removes the underlying kernel object. But it appears in Linux you, the developer, needs to remove the kernel object by calling sem_unlink(). If you don't the kernel object persists in the /dev/shm folder.

我遇到的问题,如果进程A调用sem_unlink()而进程B的信号灯已锁定,则它立即销毁该信号灯,现在/如果进程C到来,进程B不再受到该信号灯的保护".沿着.而且,手册页充其量是令人困惑的:

The problem I'm running into, if process A calls sem_unlink() while process B has the semaphore locked, it immediately destroys the semaphore and now process B is no longer "protected" by the semaphore when/if process C comes along. What's more, the man page is confusing at best:

立即删除该信号灯名称.一旦打开了该信号灯的所有其他进程将其关闭,则该信号灯将被销毁."

"The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it."

如果必须等待其他进程关闭信号量,如何立即销毁该对象?

How can it destroy the object immediately if it has to wait for other processes to close the semaphore?

很显然,我不理解Linux上信号量对象的正确使用.谢谢你的帮助.下面是一些我用来测试的示例代码.

Clearly I don't understand the proper use of semaphore objects on Linux. Thanks for any help. Below is some sample code I'm using to test this.

int main(void)
{
    sem_t *pSemaphore = sem_open("/MyName", O_CREAT, S_IRUSR | S_IWUSR, 1);
    if(pSemaphore != SEM_FAILED)
    {
        if(sem_wait(pSemaphore) == 0)
        {
            // Perform "protected" operations here

            sem_post(pSemaphore);
        }

        sem_close(pSemaphore);
        sem_unlink("/MyName");
    }

    return 0;
}

推荐答案

回答您的问题:

  1. 与Windows的信号量行为相比, 描述,POSIX信号是内核持久的.意思是 即使没有进程使用信号量,信号量仍保留其值 开了. (信号量的参考计数为0)

  1. In comparison to the semaphore behavior for windows you describe, POSIX semaphores are Kernel persistent. Meaning that the semaphore retains it's value even if no process has the semaphore opened. (the semaphore's reference count would be 0)

如果进程A调用sem_unlink(),而进程B具有信号灯 锁定.这意味着该信号量的引用计数不为0,并且将 不会被破坏.

If process A calls sem_unlink() while process B has the semaphore locked. This means the semaphore's reference count is not 0 and will not be destructed.

sem_close与sem_unlink的基本操作,我认为这将有助于全面理解:

Basic operation of sem_close vs sem_unlink, I think will help overall understanding:

sem_close::关闭是一个信号量,当进程退出时也是如此.信号灯仍保留在系统中.

sem_close: close's a semaphore, this also done when a process exits. the semaphore still remains in the system.

sem_unlink:仅在引用计数达到0时(即在所有打开它的进程打开后,调用sem_close或退出)才从系统中删除.

sem_unlink: will be removed from the system only when the reference count reaches 0 (that is after all processes that have it open, call sem_close or are exited).

参考文献: 图书-Unix网络编程-进程间通信,作者:W.Richard Stevens,第2卷,ch10

References: Book - Unix Networking Programming-Interprocess Communication by W.Richard Stevens, vol 2, ch10

这篇关于何时调用sem_unlink()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆