开发者

how to Define a semaphore with busy waiting solution

How do i Define a semaphore with busy waiting solution ??i got something like this

wait(Semaphore s){
s=s-1;
if (s<=0) {
    // add process to queue
    block();
}
}

signal(Semaphore s){
s=s+1;
if (s<0) {
    // remove process p from queue
    开发者_如何学运维wakeup(p);
}
}

but i don't understand the condition required in signal block

if (s<0) { // remove process p from queue wakeup(p); }

why we are checking if(s<0) here


The condition should probably detect if there is any process sleeping (blocked) in the queue. However, I think these conditions are not correct, considering behaviour of a binary semaphore (semaphore initially with s == 1) the pseudocode should be

wait(Semaphore s){ 
  s=s-1;
  if (s<0) {
    // add process to queue
    block();
  }
}

signal(Semaphore s){
  s=s+1;
  if (s<=0) {
    // remove process p from queue
    wakeup(p);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜