提问人:S214ky 提问时间:11/11/2023 最后编辑:S214ky 更新时间:11/11/2023 访问量:72
在 C 语言中监控实现
monitor implementation in C
问:
我刚刚开始用 C 语言研究监视器和条件变量,但我无法理解以下代码是如何工作的。 我有以下条件变量的 wait_condition 和 signal_condition 函数的实现
struct Monitor {
//id of the mutex semaphore for the monitor
int mutex;
int num_var_cond;
//id of the semaphores of the condition variables
int id_conds;
//array of the condition_count (they count number of processes suspended on a CV)
int *cond_counts;
//id shared memory
int id_shared;
};
void wait_condition(Monitor* M,int id_var) {
M->cond_counts[id_var]=M->cond_counts[id_var]+1;
Signal_Sem(M->mutex,0);
Wait_Sem(M->id_conds,id_var);
M->cond_counts[id_var]=M->cond_counts[id_var]-1;
}
void signal_condition(Monitor* M,int id_var){
if(M->cond_counts[id_var]>0){
Signal_Sem(M->id_conds,id_var);
}else{
Signal_Sem(M->mutex,0); }
}
我试图理解的是,当我在之前使用wait_condition暂停的进程上使用signal_condition时,不应该有另一个wait_sem(M->mutex, 0),否则互斥锁的值为 1,监视器没有被占用
我期望在wait_condition中的 wait_sem(M->id_conds, id_var) 之后成为另一个 wait_sem(M->mutex, 0),以保持显示器被占用
答: 暂无答案
评论