信号量中semop函数一个问题
时间:2011-12-27
来源:互联网
struct sembuf 结构中的 sem_op成员有一段解释:
如果sem_op是负数,那么信号量将减去它的值。这和信号量控制的资源有关。如果没有使用IPC_NOWAIT,那么调用进程将进入睡眠状态,直到信号量控制的资源可以使用为止。如果sem_op是正数,则信号量加上它的值。这也就是进程释放信号量控制的资源。最后,如果sem_op是0,那么调用进程将调用sleep(),直到信号量的值为0。这在一个进程等待完全空闲的资源时使用。
我想问一下最后一句:"等待完全空闲的资源"是什么意思?信号量的值为0不是资源没有空闲吗?怎么又说是空闲的了
如果sem_op是负数,那么信号量将减去它的值。这和信号量控制的资源有关。如果没有使用IPC_NOWAIT,那么调用进程将进入睡眠状态,直到信号量控制的资源可以使用为止。如果sem_op是正数,则信号量加上它的值。这也就是进程释放信号量控制的资源。最后,如果sem_op是0,那么调用进程将调用sleep(),直到信号量的值为0。这在一个进程等待完全空闲的资源时使用。
我想问一下最后一句:"等待完全空闲的资源"是什么意思?信号量的值为0不是资源没有空闲吗?怎么又说是空闲的了
作者: aaasssmnbv 发布时间: 2011-12-27
相信你前面都懂,sem_op 是操作数,如果负数就相当于请求资源,如果是正数,就相当于释放资源。
信号量值就是资源数。
你可以看看一些E文的解释,如果是0,会怎么做:
If sem_op is 0 and the calling process has read permission, one of the following will occur:
If semval is 0, semop() will return immediately.
If semval is non-zero and (sem_flg&IPC_NOWAIT) is non-zero, semop() will return immediately.
If semval is non-zero and (sem_flg&IPC_NOWAIT) is 0, semop() will increment the semzcnt associated with the specified semaphore and suspend execution of the calling thread until one of the following occurs:
The value of semval becomes 0, at which time the value of semzcnt associated with the specified semaphore is decremented.
The semid for which the calling thread is awaiting action is removed from the system. When this occurs, errno is set equal to [EIDRM] and -1 is returned.
The calling thread receives a signal that is to be caught. When this occurs, the value of semzcnt associated with the specified semaphore is decremented, and the calling thread resumes execution in the manner prescribed in sigaction().
信号量值就是资源数。
你可以看看一些E文的解释,如果是0,会怎么做:
If sem_op is 0 and the calling process has read permission, one of the following will occur:
If semval is 0, semop() will return immediately.
If semval is non-zero and (sem_flg&IPC_NOWAIT) is non-zero, semop() will return immediately.
If semval is non-zero and (sem_flg&IPC_NOWAIT) is 0, semop() will increment the semzcnt associated with the specified semaphore and suspend execution of the calling thread until one of the following occurs:
The value of semval becomes 0, at which time the value of semzcnt associated with the specified semaphore is decremented.
The semid for which the calling thread is awaiting action is removed from the system. When this occurs, errno is set equal to [EIDRM] and -1 is returned.
The calling thread receives a signal that is to be caught. When this occurs, the value of semzcnt associated with the specified semaphore is decremented, and the calling thread resumes execution in the manner prescribed in sigaction().
作者: gqjjqg 发布时间: 2011-12-27
如果sem_op是负数,那么信号量将减去它的值。这和信号量控制的资源有关。如果没有使用IPC_NOWAIT,那么调用进程将进入睡眠状态,直到信号量控制的资源可以使用为止。如果sem_op是正数,则信号量加上它的值。这也就是进程释放信号量控制的资源。最后,如果sem_op是0,那么调用进程将调用sleep(),直到信号量的值为0。这在一个进程等待完全空闲的资源时使用。
--
这里面是三个概念:一个是sem_op,一个是信号量,一个是信号量控制的资源。sem_op表示的是进程要求或释放的资源数。信号量是表示资源当前的使用状态。当sem_op为0时,就表示当前进程不与其他进程共享资源,而是等待资源被完全释放之后再使用该资源,也就是信号量的值为0时使用。
--
这里面是三个概念:一个是sem_op,一个是信号量,一个是信号量控制的资源。sem_op表示的是进程要求或释放的资源数。信号量是表示资源当前的使用状态。当sem_op为0时,就表示当前进程不与其他进程共享资源,而是等待资源被完全释放之后再使用该资源,也就是信号量的值为0时使用。
作者: barech 发布时间: 2011-12-27
回2楼,抱歉了英文看不懂,能翻译下吗..
回3楼,你的解释好像是在信号量为0时使用sem_op=0,可是我的原文那里是信号量还没到0时使用呢,它是用来等待信号量减到0再返回吧.
回3楼,你的解释好像是在信号量为0时使用sem_op=0,可是我的原文那里是信号量还没到0时使用呢,它是用来等待信号量减到0再返回吧.
作者: aaasssmnbv 发布时间: 2011-12-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28