当前位置: 技术问答>linux和unix
请教使用sem_unlink删除信号量的问题
来源: 互联网 发布时间:2016-06-14
本文导语: 两个进程之间使用同一个命名信号量进行通讯(使用sem_open创建),文档上说sem_unlink会维护一个引用计数只有当此计数为零时才真正删除此信号量,但是现在我发现只要一调用sem_unlink函数信号量就会删除,不知道为...
两个进程之间使用同一个命名信号量进行通讯(使用sem_open创建),文档上说sem_unlink会维护一个引用计数只有当此计数为零时才真正删除此信号量,但是现在我发现只要一调用sem_unlink函数信号量就会删除,不知道为什么?(开发环境:Redhat.Enterprise.Linux5.U3.i386)
|
The sem_unlink() function removes the specified named semaphore. If other process are currently referencing the specified semaphore, sem_unlink() has no effect on the state of the semaphore. If one or more processes have the specified semaphore open when you call sem_unlink(), the semaphore is not removed until all references to the semaphore has been destroyed by sem_close(), _exit(), or exec() calls. Calls to sem_open() to re-create or re-connect to the specified semaphore refer to a new semaphore after sem_unlink() is called. The sem_unlink() call does not block until all references have been destroyed; it returns immediately.
你是怎么确定被删除的时候,引用计数不为0.
你是怎么确定被删除的时候,引用计数不为0.
|
sem_getvalue - get the value of a semaphore
SYNOPSIS
#include
int sem_getvalue(sem_t *sem, int *sval);
DESCRIPTION
sem_getvalue() places the current value of the semaphore pointed to sem into the integer pointed to by sval.
If one or more processes or threads are blocked waiting to lock the semaphore with sem_wait(3), POSIX.1-2001 permits
two possibilities for the value returned in sval: either 0 is returned; or a negative number whose absolute value is
the count of the number of processes and threads currently blocked in sem_wait(3). Linux adopts the former behavior.
RETURN VALUE
sem_getvalue() returns 0 on success; on error, -1 is returned and errno is set to indicate the error.
ERRORS
EINVAL sem is not a valid semaphore.
CONFORMING TO
POSIX.1-2001.
NOTES
The value of the semaphore may already have changed by the time sem_getvalue() returns.
SYNOPSIS
#include
int sem_getvalue(sem_t *sem, int *sval);
DESCRIPTION
sem_getvalue() places the current value of the semaphore pointed to sem into the integer pointed to by sval.
If one or more processes or threads are blocked waiting to lock the semaphore with sem_wait(3), POSIX.1-2001 permits
two possibilities for the value returned in sval: either 0 is returned; or a negative number whose absolute value is
the count of the number of processes and threads currently blocked in sem_wait(3). Linux adopts the former behavior.
RETURN VALUE
sem_getvalue() returns 0 on success; on error, -1 is returned and errno is set to indicate the error.
ERRORS
EINVAL sem is not a valid semaphore.
CONFORMING TO
POSIX.1-2001.
NOTES
The value of the semaphore may already have changed by the time sem_getvalue() returns.
|
用 ipcs 看看状态先
|
你是先调用 sem_close还是先调用sem_unlink呢?
最好把你写的两个测试程序发出来看看