使用二进制信号量实现计数信号量
Implementing Counting-Semaphore Using Binary-Semaphore
我已经实现了某种用户级线程system.I在使用二进制信号量实现计数信号量方面需要一些帮助
实现(如下所述的向上和向下功能)。
这是我实现二进制信号量的界面:
typedef enum BinSemStatus{
locked,
unlocked
} BinSemStatus;
struct semaphore {
BinSemStatus status;
int bid;
};
int bsem_alloc();//allocate a new binary semaphore,return its descriptor
void bsem_free(int id);
void bsem_down(int id);
void bsem_up(int id);
这里是计数信号量接口的接口:
struct counting_semaphore* counting_alloc(uint value);
counting_free(struct counting_semaphore* sem);
// If the value representing the count of
// the semaphore variable is not negative, decrement it by 1. If the
// semaphore variable is now
// negative, the thread executing acquire is blocked until the value is
// greater or equal to 1.
// Otherwise, the thread continues execution.
void up(struct counting_semaphore* sem);
// Increments the value of semaphore
// variable by 1.
void down(struct counting_semaphore* sem);
我尝试做的是 void up(struct counting_semaphore* sem)
锁定 value.But 如下所示,这不是 enough.I 在有问题的情况下添加了评论。
struct counting_semaphore {
int binary_descriptor;
int value;
};
void down(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor);
if (sem->value > 0){
sem->value--;
}
else{
//not sure what to do here, maybe use anather semaphore in some way?
}
bsem_up(sem->binary_descriptor);
}
void up(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor);
sem->value++;
bsem_up(sem->binary_descriptor);
}
当sem->value
到0时,线程阻塞,是时候重新调度了。你没有显示你的调度代码,所以我不能给出具体的建议。可能调度程序应该代表线程调用 bsem_up(sem->binary_descriptor);
。
临界区由 sem->binary_descriptor1
保护,每个操作 down(S)
和 up(S)
都使用它来正确更新 sem.value
的值
更新两个操作后,仅当值为正时才释放 sem->binary_descriptor2
信号量。
S.value
永远不会为负,因为任何执行 down(S)
的进程都在 sem->binary_descriptor2
.
处被阻塞
struct counting_semaphore* counting_semaphore_alloc(int value) {
struct counting_semaphore* sem = malloc(sizeof(struct counting_semaphore));
sem->value = value;
sem->binary_descriptor1= bsem_alloc();//locks the value
sem->binary_descriptor2= bsem_alloc();//locks the sing of the value
if (value <= 0) {
bsem_down(sem->binary_descriptor2);
}
return sem;
}
void down(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor2);
bsem_down(sem->binary_descriptor1);
sem->value--;
if (sem->value>0){
bsem_up(sem->binary_descriptor2);
}
bsem_up(sem->binary_descriptor1);
}
void up(struct counting_semaphore* sem) {
bsem_down(sem->binary_descriptor1);
sem->value++;
if (sem->value == 1) {
bsem_up(sem->binary_descriptor2);
}
bsem_up(sem->binary_descriptor1);
}
我已经实现了某种用户级线程system.I在使用二进制信号量实现计数信号量方面需要一些帮助 实现(如下所述的向上和向下功能)。 这是我实现二进制信号量的界面:
typedef enum BinSemStatus{
locked,
unlocked
} BinSemStatus;
struct semaphore {
BinSemStatus status;
int bid;
};
int bsem_alloc();//allocate a new binary semaphore,return its descriptor
void bsem_free(int id);
void bsem_down(int id);
void bsem_up(int id);
这里是计数信号量接口的接口:
struct counting_semaphore* counting_alloc(uint value);
counting_free(struct counting_semaphore* sem);
// If the value representing the count of
// the semaphore variable is not negative, decrement it by 1. If the
// semaphore variable is now
// negative, the thread executing acquire is blocked until the value is
// greater or equal to 1.
// Otherwise, the thread continues execution.
void up(struct counting_semaphore* sem);
// Increments the value of semaphore
// variable by 1.
void down(struct counting_semaphore* sem);
我尝试做的是 void up(struct counting_semaphore* sem) 锁定 value.But 如下所示,这不是 enough.I 在有问题的情况下添加了评论。
struct counting_semaphore {
int binary_descriptor;
int value;
};
void down(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor);
if (sem->value > 0){
sem->value--;
}
else{
//not sure what to do here, maybe use anather semaphore in some way?
}
bsem_up(sem->binary_descriptor);
}
void up(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor);
sem->value++;
bsem_up(sem->binary_descriptor);
}
当sem->value
到0时,线程阻塞,是时候重新调度了。你没有显示你的调度代码,所以我不能给出具体的建议。可能调度程序应该代表线程调用 bsem_up(sem->binary_descriptor);
。
临界区由 sem->binary_descriptor1
保护,每个操作 down(S)
和 up(S)
都使用它来正确更新 sem.value
更新两个操作后,仅当值为正时才释放 sem->binary_descriptor2
信号量。
S.value
永远不会为负,因为任何执行 down(S)
的进程都在 sem->binary_descriptor2
.
struct counting_semaphore* counting_semaphore_alloc(int value) {
struct counting_semaphore* sem = malloc(sizeof(struct counting_semaphore));
sem->value = value;
sem->binary_descriptor1= bsem_alloc();//locks the value
sem->binary_descriptor2= bsem_alloc();//locks the sing of the value
if (value <= 0) {
bsem_down(sem->binary_descriptor2);
}
return sem;
}
void down(struct counting_semaphore *sem){
bsem_down(sem->binary_descriptor2);
bsem_down(sem->binary_descriptor1);
sem->value--;
if (sem->value>0){
bsem_up(sem->binary_descriptor2);
}
bsem_up(sem->binary_descriptor1);
}
void up(struct counting_semaphore* sem) {
bsem_down(sem->binary_descriptor1);
sem->value++;
if (sem->value == 1) {
bsem_up(sem->binary_descriptor2);
}
bsem_up(sem->binary_descriptor1);
}