开发者

how to Define a semaphore with busy waiting solution

开发者 https://www.devze.com 2023-03-07 17:11 出处:网络
How do i Define a semaphore with busy waiting solution??i got something like this wait(Semaphore s){ s=s-1;

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

精彩评论

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

关注公众号