信号量
请大家帮分析下,信号量这么用对吗?加锁的话不应该资源减一吗?我看代码里这么用也没报错呢?
[code=C/C++][/code]
int kb_sem_lock()
{
int rv = 0;
struct sembuf operation[2];
operation[0].sem_num = 0;
operation[0].sem_op = 0;
operation[0].sem_flg = SEM_UNDO;
operation[1].sem_num = 0;
operation[1].sem_op = 1;
operation[1].sem_flg = SEM_UNDO;
rv = (semop(kb_sem_id, &operation[0], 2));
return rv;
}
int kb_sem_unlock()
{
struct sembuf operation[1];
int rv = 0;
operation[0].sem_num = 0;
operation[0].sem_op = -1;
operation[0].sem_flg = SEM_UNDO;
rv = semop(kb_sem_id, &operation[0], 1);
return rv;
}
------解决方案--------------------
用了两个信号灯?
对于简单的同步,一般使用单个信号灯就行了
初值为1,加锁时-1,解锁时+1